@cryptotaxi247 / kubo / commits / dbeffb6a0

commands: CLI Parse: Don't parse args until after creating request

Matt Bell committed Nov 3, 2014 at 00:42 UTC dbeffb6a0b365c8dde0b856ead8cafe6ff2e5aad
1 file changed +16 -13
commands/cli/parse.go
+16 -13
@@ -12,29 +12,27 @@ import (
12 // Parse parses the input commandline string (cmd, flags, and args).
13 // returns the corresponding command Request object.
14 func Parse(input []string, roots ...*cmds.Command) (cmds.Request, *cmds.Command, error) {
15 - var req cmds.Request
16 - var root *cmds.Command
15 + var root, cmd *cmds.Command
16 + var path, stringArgs []string
17 + var opts map[string]interface{}
18
19 // use the root that matches the longest path (most accurately matches request)
20 maxLength := 0
21 for _, r := range roots {
21 - path, input, cmd := parsePath(input, r)
22 - opts, stringArgs, err := parseOptions(input)
22 + p, i, c := parsePath(input, r)
23 + o, s, err := parseOptions(i)
24 if err != nil {
25 return nil, nil, err
26 }
27
27 - length := len(path)
28 + length := len(p)
29 if length > maxLength {
30 maxLength = length
30 -
31 - args, err := parseArgs(stringArgs, cmd)
32 - if err != nil {
33 - return nil, nil, err
34 - }
35 -
36 - req = cmds.NewRequest(path, opts, args, cmd)
31 root = r
32 + path = p
33 + cmd = c
34 + opts = o
35 + stringArgs = s
36 }
37 }
38
@@ -42,7 +40,12 @@ func Parse(input []string, roots ...*cmds.Command) (cmds.Request, *cmds.Command,
40 return nil, nil, errors.New("Not a valid subcommand")
41 }
42
45 - return req, root, nil
43 + args, err := parseArgs(stringArgs, cmd)
44 + if err != nil {
45 + return nil, nil, err
46 + }
47 +
48 + return cmds.NewRequest(path, opts, args, cmd), root, nil
49 }
50
51 // parsePath gets the command path from the command line input