Commit 3f603079 authored by Matt Bell's avatar Matt Bell Committed by Juan Batiz-Benet

commands/cli: Made Parse handle multiple root commands

parent 8ce67522
......@@ -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
......
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