Merge pull request #119 from eparis/bogus-bad-command

Handle grand children with the same name as the root
This commit is contained in:
Eric Paris 2015-06-22 17:06:05 -05:00
commit 66816bcd03
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) {
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)
// 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] {
return nil, a, fmt.Errorf("unknown command %q", stripFlags(args, c)[0])
argsWOflags := stripFlags(a, commandFound)
if commandFound == c && len(argsWOflags) > 0 {
return nil, a, fmt.Errorf("unknown command %q", argsWOflags[0])
}
return commandFound, a, nil