@cryptotaxi247 / kubo / commits / 4bd3a7777

commands/cli: Added path/args parsing

Matt Bell committed Oct 14, 2014 at 12:48 UTC 4bd3a777786a5bd486fd6b481079847d5a4df3dd
1 file changed +24 -1
commands/cli/parse.go
+24 -1
@@ -13,7 +13,12 @@ func Parse(input []string, root *commands.Command) ([]string, []string, map[stri
13 return nil, nil, nil, err
14 }
15
16 - return nil, nil, opts, nil
16 + path, args, err := path(input, root)
17 + if err != nil {
18 + return nil, nil, nil, err
19 + }
20 +
21 + return path, args, opts, nil
22 }
23
24 // options parses the raw string values of the given options
@@ -90,3 +95,21 @@ func options(input []string, root *commands.Command) (map[string]string, []strin
95
96 return opts, cleanInput, nil
97 }
98 +
99 +// path takes the command line (without options) and splits it into the command path and arguments
100 +func path(input []string, root *commands.Command) ([]string, []string, error) {
101 + cmd := root
102 + i := 0
103 +
104 + for _, blob := range input {
105 + cmd := cmd.Sub(blob)
106 +
107 + if cmd == nil {
108 + break
109 + }
110 +
111 + i++
112 + }
113 +
114 + return input[:i], input[i:], nil
115 +}