Handle grand children with the same name as the root

This fixes a problem where if you had a root command and a grand child
with the same name, the parser would break and would not run the
grandchild. The code was special casing if the immediate child had the
same name, but didn't handle grand-children
This commit is contained in:
Eric Paris 2015-06-22 17:26:11 -04:00
parent ddd4c82b82
commit 07ad27d239
2 changed files with 21 additions and 2 deletions

View File

@ -378,6 +378,24 @@ func TestChildSameName(t *testing.T) {
} }
} }
func TestGrandChildSameName(t *testing.T) {
c := initializeWithSameName()
cmdTimes.AddCommand(cmdPrint)
c.AddCommand(cmdTimes)
c.SetArgs(strings.Split("times print one two", " "))
c.Execute()
if te != nil || tt != nil {
t.Error("Wrong command called")
}
if tp == nil {
t.Error("Wrong command called")
}
if strings.Join(tp, " ") != "one two" {
t.Error("Command didn't parse correctly")
}
}
func TestFlagLong(t *testing.T) { func TestFlagLong(t *testing.T) {
noRRSetupTest("echo --intone=13 something here") noRRSetupTest("echo --intone=13 something here")

View File

@ -423,8 +423,9 @@ func (c *Command) Find(args []string) (*Command, []string, error) {
commandFound, a := innerfind(c, args) commandFound, a := innerfind(c, args)
// If we matched on the root, but we asked for a subcommand, return an error // If we matched on the root, but we asked for a subcommand, return an error
if commandFound.Name() == c.Name() && len(stripFlags(args, c)) > 0 && commandFound.Name() != args[0] { argsWOflags := stripFlags(a, commandFound)
return nil, a, fmt.Errorf("unknown command %q", stripFlags(args, c)[0]) if commandFound == c && len(argsWOflags) > 0 {
return nil, a, fmt.Errorf("unknown command %q", argsWOflags[0])
} }
return commandFound, a, nil return commandFound, a, nil