Cosmos Nicolaou | 4e213d7 | 2014-10-26 22:21:52 -0700 | [diff] [blame] | 1 | package core |
| 2 | |
| 3 | import ( |
| 4 | "flag" |
| 5 | "fmt" |
| 6 | |
| 7 | "veyron.io/veyron/veyron2/ipc" |
| 8 | |
| 9 | "veyron.io/veyron/veyron/lib/flags" |
| 10 | ) |
| 11 | |
Adam Sadovsky | d21d95a | 2014-10-29 09:56:59 -0700 | [diff] [blame] | 12 | func parseFlags(fl *flags.Flags, args []string) error { |
| 13 | if len(args) == 0 { |
| 14 | return nil |
| 15 | } |
| 16 | return fl.Parse(args[1:]) |
| 17 | } |
| 18 | |
| 19 | // parseListenFlags parses the given args using just the flags and env vars |
| 20 | // defined in the veyron/lib/flags package. |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 21 | func parseListenFlags(args []string) (*flags.Flags, []string, error) { |
Cosmos Nicolaou | 4e213d7 | 2014-10-26 22:21:52 -0700 | [diff] [blame] | 22 | fs := flag.NewFlagSet("modules/core", flag.ContinueOnError) |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 23 | fl := flags.CreateAndRegister(fs, flags.Listen) |
Adam Sadovsky | d21d95a | 2014-10-29 09:56:59 -0700 | [diff] [blame] | 24 | err := parseFlags(fl, args) |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 25 | return fl, fl.Args(), err |
Cosmos Nicolaou | 4e213d7 | 2014-10-26 22:21:52 -0700 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | func initListenSpec(fl *flags.Flags) ipc.ListenSpec { |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 29 | lf := fl.ListenFlags() |
Cosmos Nicolaou | 4e213d7 | 2014-10-26 22:21:52 -0700 | [diff] [blame] | 30 | return ipc.ListenSpec{ |
Cosmos Nicolaou | d811b07 | 2014-10-28 17:46:27 -0700 | [diff] [blame] | 31 | Protocol: lf.ListenProtocol.String(), |
| 32 | Address: lf.ListenAddress.String(), |
| 33 | Proxy: lf.ListenProxy, |
Cosmos Nicolaou | 4e213d7 | 2014-10-26 22:21:52 -0700 | [diff] [blame] | 34 | } |
| 35 | } |
| 36 | |
Adam Sadovsky | d21d95a | 2014-10-29 09:56:59 -0700 | [diff] [blame] | 37 | // checkArgs checks for the expected number of args in args. A negative |
Cosmos Nicolaou | 4e213d7 | 2014-10-26 22:21:52 -0700 | [diff] [blame] | 38 | // value means at least that number of args are expected. |
| 39 | func checkArgs(args []string, expected int, usage string) error { |
| 40 | got := len(args) |
| 41 | if expected < 0 { |
| 42 | expected = -expected |
| 43 | if got < expected { |
| 44 | return fmt.Errorf("wrong # args (got %d, expected >=%d) expected: %q got: %v", got, expected, usage, args) |
| 45 | } |
| 46 | } else { |
| 47 | if got != expected { |
| 48 | return fmt.Errorf("wrong # args (got %d, expected %d) expected: %q got: %v", got, expected, usage, args) |
| 49 | } |
| 50 | } |
| 51 | return nil |
| 52 | } |