forked from mirror/cobra
Finding command based on args cleaned up
This commit is contained in:
parent
74ce44d76e
commit
55ff474dee
38
command.go
38
command.go
|
@ -77,25 +77,37 @@ func (c *Command) resetChildrensParents() {
|
||||||
|
|
||||||
// find the target command given the args and command tree
|
// find the target command given the args and command tree
|
||||||
// Meant to be run on the highest node. Only searches down.
|
// Meant to be run on the highest node. Only searches down.
|
||||||
func (c *Command) Find(args []string) (cmd *Command, a []string, err error) {
|
func (c *Command) Find(arrs []string) (*Command, []string, error) {
|
||||||
if c == nil {
|
if c == nil {
|
||||||
return nil, nil, fmt.Errorf("Called find() on a nil Command")
|
return nil, nil, fmt.Errorf("Called find() on a nil Command")
|
||||||
}
|
}
|
||||||
|
|
||||||
validSubCommand := false
|
if len(arrs) == 0 {
|
||||||
if len(args) > 0 && c.HasSubCommands() {
|
return c.Commander().cmd, arrs, nil
|
||||||
for _, cmd := range c.commands {
|
|
||||||
if cmd.Name() == args[0] {
|
|
||||||
validSubCommand = true
|
|
||||||
return cmd.Find(args[1:])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !validSubCommand && c.Runnable() {
|
|
||||||
return c, args, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, nil, nil
|
var innerfind func(*Command, []string) (*Command, []string)
|
||||||
|
|
||||||
|
innerfind = func(c *Command, args []string) (*Command, []string) {
|
||||||
|
if len(args) > 0 && c.HasSubCommands() {
|
||||||
|
for _, cmd := range c.commands {
|
||||||
|
if cmd.Name() == args[0] {
|
||||||
|
return innerfind(cmd, args[1:])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return c, args
|
||||||
|
}
|
||||||
|
|
||||||
|
commandFound, a := innerfind(c, arrs)
|
||||||
|
|
||||||
|
// if commander returned and not appropriately matched return nil & error
|
||||||
|
if commandFound.Name() == c.Name() && commandFound.Name() != arrs[0] {
|
||||||
|
return nil, a, fmt.Errorf("Command not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
return commandFound, a, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Command) Root() *Command {
|
func (c *Command) Root() *Command {
|
||||||
|
|
Loading…
Reference in New Issue