@cryptotaxi247 / kubo / commits / 97b871907

commands/cli: Removed parser string handling since the go runtime handles it for us automatically

Matt Bell committed Oct 14, 2014 at 15:37 UTC 97b8719075bc5c3f568cca8fd8f74af3d0939197
2 files changed +3 -35
commands/cli/parse.go
+2 -34
@@ -21,8 +21,7 @@ func Parse(input []string, root *commands.Command) ([]string, []string, map[stri
21 return path, args, opts, nil
22 }
23
24 -
25 -// path gets the command path from the command line input
24 +// parsePath gets the command path from the command line input
25 func parsePath(input []string, root *commands.Command) ([]string, []string, error) {
26 cmd := root
27 i := 0
@@ -43,7 +42,7 @@ func parsePath(input []string, root *commands.Command) ([]string, []string, erro
42 return input[:i], input[i:], nil
43 }
44
46 -// options parses the raw string values of the given options
45 +// parseOptions parses the raw string values of the given options
46 // returns the parsed options as strings, along with the CLI args
47 func parseOptions(input, path []string, root *commands.Command) (map[string]string, []string, error) {
48 options, err := root.GetOptions(path)
@@ -69,37 +68,6 @@ func parseOptions(input, path []string, root *commands.Command) (map[string]stri
68 value = split[1]
69 }
70
72 - if strings.Contains(name, "-") {
73 - return nil, nil, fmt.Errorf("Invalid option blob: '%s' (Shouldn't contain '-')", input[i])
74 - }
75 -
76 - if value != "" && strings.Contains(value, "\"") {
77 - // TODO: ignore escaped quotations (--foo="\"")
78 - if !strings.HasPrefix(value, "\"") {
79 - return nil, nil, fmt.Errorf("Invalid option blob: '%s' (Quotation wasn't at the start of value)", input[i])
80 - }
81 -
82 - value = value[1:]
83 -
84 - for {
85 - if strings.HasSuffix(value, "\"") {
86 - value = value[:len(value)-1]
87 - break
88 - }
89 -
90 - i++
91 - if i >= len(input) {
92 - return nil, nil, fmt.Errorf("Unterminated string: '%s'", value)
93 - }
94 -
95 - value += " " + input[i]
96 - }
97 -
98 - if strings.Contains(value, "\"") {
99 - return nil, nil, fmt.Errorf("Invalid option blob: '%s' (Value contains unescaped quotation)", value)
100 - }
101 - }
102 -
71 opts[name] = value
72
73 } else if strings.HasPrefix(blob, "-") {
commands/cli/parse_test.go
+1 -1
@@ -15,7 +15,7 @@ func TestOptionParsing(t *testing.T) {
15 }
16 cmd.Register("test", &commands.Command{})
17
18 - opts, input, err := parseOptions([]string{ "--beep", "--boop=\"5", "lol\"", "test2", "-cVb", "beep" },
18 + opts, input, err := parseOptions([]string{ "--beep", "--boop=5 lol", "test2", "-cVb", "beep" },
19 []string{"test"}, cmd)
20 /*for k, v := range opts {
21 fmt.Printf("%s: %s\n", k, v)