@cryptotaxi247 / kubo / commits / c0d3edd4f

commands/cli: Made Parse handle multiple root commands

Matt Bell committed Oct 31, 2014 at 13:49 UTC c0d3edd4f9bb8830f7fa8a7f3ed218abfd338df0
1 file changed +20 -6
commands/cli/parse.go
+20 -6
@@ -9,17 +9,31 @@ import (
9
10 // Parse parses the input commandline string (cmd, flags, and args).
11 // returns the corresponding command Request object.
12 -func Parse(input []string, root *cmds.Command) (cmds.Request, error) {
13 - path, input, cmd := parsePath(input, root)
14 - opts, args, err := parseOptions(input)
15 - if err != nil {
16 - return nil, err
12 +func Parse(input []string, roots ...*cmds.Command) (cmds.Request, *cmds.Command, error) {
13 + var req cmds.Request
14 + var root *cmds.Command
15 +
16 + // use the root that matches the longest path (most accurately matches request)
17 + maxLength := 0
18 + for _, r := range roots {
19 + path, input, cmd := parsePath(input, r)
20 + opts, args, err := parseOptions(input)
21 + if err != nil {
22 + return nil, nil, err
23 + }
24 +
25 + length := len(path)
26 + if length > maxLength {
27 + maxLength = length
28 + req = cmds.NewRequest(path, opts, args, nil, cmd)
29 + root = r
30 + }
31 }
32
33 // TODO: figure out how to know when to read given file(s) as an input stream
34 // (instead of filename arg string)
35
22 - return cmds.NewRequest(path, opts, args, nil, cmd), nil
36 + return req, root, nil
37 }
38
39 // parsePath gets the command path from the command line input