From c5be1eed7654b8662c77b92a838f2c80b0ebb727 Mon Sep 17 00:00:00 2001 From: Milan Jovanovic Date: Sun, 22 Sep 2019 15:44:44 +0100 Subject: [PATCH] Make OnCommandNotFound independable of SilenceErrors flag --- command.go | 11 ++++++----- command_test.go | 13 +++++++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) 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}