Skip to content
Myr

Handle grand children with the same name as the root

Before adoption: this commit came in with the history the project adopted, so no landing records it.

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

authored by Eric Paris · committed by Eric Paris · parent ddd4c82 · Browse files · 2 files+21 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+21 added−2 removed

  • other:+21 added−2 removed

2 files+21 added−2 removed

modified:cobra_test.go+18 added
376 unmodified lines
377 t.Error("Command didn't parse correctly")378 }379}380381func TestGrandChildSameName(t *testing.T) {382 c := initializeWithSameName()383 cmdTimes.AddCommand(cmdPrint)384 c.AddCommand(cmdTimes)385 c.SetArgs(strings.Split("times print one two", " "))386 c.Execute()387388 if te != nil || tt != nil {389 t.Error("Wrong command called")390 }391 if tp == nil {392 t.Error("Wrong command called")393 }394 if strings.Join(tp, " ") != "one two" {395 t.Error("Command didn't parse correctly")396 }397}398399func TestFlagLong(t *testing.T) {400 noRRSetupTest("echo --intone=13 something here")401402 if strings.Join(te, " ") != "something here" {
563 unmodified lines
modified:command.go+3 added-2 removed
421 unmodified lines
422423 commandFound, a := innerfind(c, args)424425 // If we matched on the root, but we asked for a subcommand, return an error426 if commandFound.Name() == c.Name() && len(stripFlags(args, c)) > 0 && commandFound.Name() != args[0] {427 return nil, a, fmt.Errorf("unknown command %q", stripFlags(args, c)[0])426 argsWOflags := stripFlags(a, commandFound)427 if commandFound == c && len(argsWOflags) > 0 {428 return nil, a, fmt.Errorf("unknown command %q", argsWOflags[0])429 }430431 return commandFound, a, nil432}
600 unmodified lines