Commit 97b87190 authored by Matt Bell's avatar Matt Bell Committed by Juan Batiz-Benet

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

parent 5d9fa93c
...@@ -21,8 +21,7 @@ func Parse(input []string, root *commands.Command) ([]string, []string, map[stri ...@@ -21,8 +21,7 @@ func Parse(input []string, root *commands.Command) ([]string, []string, map[stri
return path, args, opts, nil return path, args, opts, nil
} }
// parsePath gets the command path from the command line input
// path gets the command path from the command line input
func parsePath(input []string, root *commands.Command) ([]string, []string, error) { func parsePath(input []string, root *commands.Command) ([]string, []string, error) {
cmd := root cmd := root
i := 0 i := 0
...@@ -43,7 +42,7 @@ func parsePath(input []string, root *commands.Command) ([]string, []string, erro ...@@ -43,7 +42,7 @@ func parsePath(input []string, root *commands.Command) ([]string, []string, erro
return input[:i], input[i:], nil return input[:i], input[i:], nil
} }
// options parses the raw string values of the given options // parseOptions parses the raw string values of the given options
// returns the parsed options as strings, along with the CLI args // returns the parsed options as strings, along with the CLI args
func parseOptions(input, path []string, root *commands.Command) (map[string]string, []string, error) { func parseOptions(input, path []string, root *commands.Command) (map[string]string, []string, error) {
options, err := root.GetOptions(path) options, err := root.GetOptions(path)
...@@ -69,37 +68,6 @@ func parseOptions(input, path []string, root *commands.Command) (map[string]stri ...@@ -69,37 +68,6 @@ func parseOptions(input, path []string, root *commands.Command) (map[string]stri
value = split[1] value = split[1]
} }
if strings.Contains(name, "-") {
return nil, nil, fmt.Errorf("Invalid option blob: '%s' (Shouldn't contain '-')", input[i])
}
if value != "" && strings.Contains(value, "\"") {
// TODO: ignore escaped quotations (--foo="\"")
if !strings.HasPrefix(value, "\"") {
return nil, nil, fmt.Errorf("Invalid option blob: '%s' (Quotation wasn't at the start of value)", input[i])
}
value = value[1:]
for {
if strings.HasSuffix(value, "\"") {
value = value[:len(value)-1]
break
}
i++
if i >= len(input) {
return nil, nil, fmt.Errorf("Unterminated string: '%s'", value)
}
value += " " + input[i]
}
if strings.Contains(value, "\"") {
return nil, nil, fmt.Errorf("Invalid option blob: '%s' (Value contains unescaped quotation)", value)
}
}
opts[name] = value opts[name] = value
} else if strings.HasPrefix(blob, "-") { } else if strings.HasPrefix(blob, "-") {
......
...@@ -15,7 +15,7 @@ func TestOptionParsing(t *testing.T) { ...@@ -15,7 +15,7 @@ func TestOptionParsing(t *testing.T) {
} }
cmd.Register("test", &commands.Command{}) cmd.Register("test", &commands.Command{})
opts, input, err := parseOptions([]string{ "--beep", "--boop=\"5", "lol\"", "test2", "-cVb", "beep" }, opts, input, err := parseOptions([]string{ "--beep", "--boop=5 lol", "test2", "-cVb", "beep" },
[]string{"test"}, cmd) []string{"test"}, cmd)
/*for k, v := range opts { /*for k, v := range opts {
fmt.Printf("%s: %s\n", k, v) fmt.Printf("%s: %s\n", k, v)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment