diff --git a/cobra_test.go b/cobra_test.go index d7dd69f6..7cb4917d 100644 --- a/cobra_test.go +++ b/cobra_test.go @@ -768,7 +768,7 @@ func TestRootNoCommandHelp(t *testing.T) { func TestRootUnknownCommand(t *testing.T) { r := noRRSetupTest("bogus") - s := "Error: unknown command \"bogus\"\nRun 'cobra-test --help' for usage.\n" + s := "Error: unknown command \"bogus\" for \"cobra-test\"\nRun 'cobra-test --help' for usage.\n" if r.Output != s { t.Errorf("Unexpected response.\nExpecting to be:\n %q\nGot:\n %q\n", s, r.Output) diff --git a/command.go b/command.go index 0bc1693e..e82fd887 100644 --- a/command.go +++ b/command.go @@ -422,7 +422,7 @@ func (c *Command) Find(args []string) (*Command, []string, error) { } // root command with subcommands, do subcommand checking if commandFound == c && len(argsWOflags) > 0 { - return nil, a, fmt.Errorf("unknown command %q", argsWOflags[0]) + return commandFound, a, fmt.Errorf("unknown command %q for %q", argsWOflags[0], commandFound.CommandPath()) } return commandFound, a, nil @@ -539,6 +539,10 @@ func (c *Command) Execute() (err error) { cmd, flags, err := c.Find(args) if err != nil { + // If found parse to a subcommand and then failed, talk about the subcommand + if cmd != nil { + c = cmd + } c.Println("Error:", err.Error()) c.Printf("Run '%v --help' for usage.\n", c.CommandPath()) return err