diff --git a/command.go b/command.go index 46923084..1d5e1219 100644 --- a/command.go +++ b/command.go @@ -904,11 +904,12 @@ func (c *Command) ExecuteC() (cmd *Command, err error) { if cmd != nil { c = cmd } - if !c.SilenceErrors { - if c.OnCommandNotFound != nil { - c.OnCommandNotFound(c, args) - return c, nil - } else { + + if c.OnCommandNotFound != nil { + c.OnCommandNotFound(c, args) + return c, nil + } else { + if !c.SilenceErrors { c.Println("Error:", err.Error()) c.Printf("Run '%v --help' for usage.\n", c.CommandPath()) } diff --git a/command_test.go b/command_test.go index 94058b93..48c805e1 100644 --- a/command_test.go +++ b/command_test.go @@ -160,6 +160,19 @@ func TestRootUnknownCommandCustomHandler(t *testing.T) { } } +func TestRootUnknownCommandCustomHandlerWithSilencedErrors(t *testing.T) { + rootCmd := &Command{Use: "root", Run: emptyRun, SilenceErrors: true} + rootCmd.OnCommandNotFound = func(cmd *Command, args []string) { + cmd.Println("Command not found. Sorry buddy :(") + } + rootCmd.AddCommand(&Command{Use: "child", Run: emptyRun}) + + output, _ := executeCommand(rootCmd, "unknown") + if output != "Command not found. Sorry buddy :(\n" { + t.Errorf("Expected custom output message, because of custom CommandNotFound handler.\nGot:\n %q\n", output) + } +} + func TestCommandAlias(t *testing.T) { var timesCmdArgs []string rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}