From c0d3edd4f9bb8830f7fa8a7f3ed218abfd338df0 Mon Sep 17 00:00:00 2001 From: Matt Bell Date: Fri, 31 Oct 2014 13:49:04 -0700 Subject: [PATCH] commands/cli: Made Parse handle multiple root commands --- commands/cli/parse.go | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/commands/cli/parse.go b/commands/cli/parse.go index 6e2a7ac6d..2a6aa7f95 100644 --- a/commands/cli/parse.go +++ b/commands/cli/parse.go @@ -9,17 +9,31 @@ import ( // Parse parses the input commandline string (cmd, flags, and args). // returns the corresponding command Request object. -func Parse(input []string, root *cmds.Command) (cmds.Request, error) { - path, input, cmd := parsePath(input, root) - opts, args, err := parseOptions(input) - if err != nil { - return nil, err +func Parse(input []string, roots ...*cmds.Command) (cmds.Request, *cmds.Command, error) { + var req cmds.Request + var root *cmds.Command + + // use the root that matches the longest path (most accurately matches request) + maxLength := 0 + for _, r := range roots { + path, input, cmd := parsePath(input, r) + opts, args, err := parseOptions(input) + if err != nil { + return nil, nil, err + } + + length := len(path) + if length > maxLength { + maxLength = length + req = cmds.NewRequest(path, opts, args, nil, cmd) + root = r + } } // TODO: figure out how to know when to read given file(s) as an input stream // (instead of filename arg string) - return cmds.NewRequest(path, opts, args, nil, cmd), nil + return req, root, nil } // parsePath gets the command path from the command line input