提交 48c108d5 作者: Matt Bell 提交者: Juan Batiz-Benet

commands/cli: Made Parse return the resolved subcommand, even on error

上级 37f05a8b
...@@ -11,7 +11,7 @@ import ( ...@@ -11,7 +11,7 @@ import (
// Parse parses the input commandline string (cmd, flags, and args). // Parse parses the input commandline string (cmd, flags, and args).
// returns the corresponding command Request object. // returns the corresponding command Request object.
func Parse(input []string, roots ...*cmds.Command) (cmds.Request, *cmds.Command, error) { func Parse(input []string, roots ...*cmds.Command) (cmds.Request, *cmds.Command, *cmds.Command, error) {
var root, cmd *cmds.Command var root, cmd *cmds.Command
var path, stringArgs []string var path, stringArgs []string
var opts map[string]interface{} var opts map[string]interface{}
...@@ -22,7 +22,7 @@ func Parse(input []string, roots ...*cmds.Command) (cmds.Request, *cmds.Command, ...@@ -22,7 +22,7 @@ func Parse(input []string, roots ...*cmds.Command) (cmds.Request, *cmds.Command,
p, i, c := parsePath(input, r) p, i, c := parsePath(input, r)
o, s, err := parseOptions(i) o, s, err := parseOptions(i)
if err != nil { if err != nil {
return nil, nil, err return nil, root, c, err
} }
length := len(p) length := len(p)
...@@ -37,22 +37,22 @@ func Parse(input []string, roots ...*cmds.Command) (cmds.Request, *cmds.Command, ...@@ -37,22 +37,22 @@ func Parse(input []string, roots ...*cmds.Command) (cmds.Request, *cmds.Command,
} }
if maxLength == 0 { if maxLength == 0 {
return nil, nil, errors.New("Not a valid subcommand") return nil, root, nil, errors.New("Not a valid subcommand")
} }
args, err := parseArgs(stringArgs, cmd) args, err := parseArgs(stringArgs, cmd)
if err != nil { if err != nil {
return nil, nil, err return nil, root, cmd, err
} }
req := cmds.NewRequest(path, opts, args, cmd) req := cmds.NewRequest(path, opts, args, cmd)
err = cmd.CheckArguments(req) err = cmd.CheckArguments(req)
if err != nil { if err != nil {
return nil, nil, err return nil, root, cmd, err
} }
return req, root, nil return req, root, cmd, nil
} }
// parsePath separates the command path and the opts and args from a command string // parsePath separates the command path and the opts and args from a command string
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论