Make OnCommandNotFound independable of SilenceErrors flag

This commit is contained in:
Milan Jovanovic 2019-09-22 15:44:44 +01:00
parent 90e9437a89
commit c5be1eed76
2 changed files with 19 additions and 5 deletions

View file

@ -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())
}

View file

@ -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}