mirror of
https://github.com/spf13/cobra
synced 2025-05-06 13:27:26 +00:00
Make OnCommandNotFound independable of SilenceErrors flag
This commit is contained in:
parent
90e9437a89
commit
c5be1eed76
2 changed files with 19 additions and 5 deletions
|
@ -904,11 +904,12 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
|
||||||
if cmd != nil {
|
if cmd != nil {
|
||||||
c = cmd
|
c = cmd
|
||||||
}
|
}
|
||||||
if !c.SilenceErrors {
|
|
||||||
if c.OnCommandNotFound != nil {
|
if c.OnCommandNotFound != nil {
|
||||||
c.OnCommandNotFound(c, args)
|
c.OnCommandNotFound(c, args)
|
||||||
return c, nil
|
return c, nil
|
||||||
} else {
|
} else {
|
||||||
|
if !c.SilenceErrors {
|
||||||
c.Println("Error:", err.Error())
|
c.Println("Error:", err.Error())
|
||||||
c.Printf("Run '%v --help' for usage.\n", c.CommandPath())
|
c.Printf("Run '%v --help' for usage.\n", c.CommandPath())
|
||||||
}
|
}
|
||||||
|
|
|
@ -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) {
|
func TestCommandAlias(t *testing.T) {
|
||||||
var timesCmdArgs []string
|
var timesCmdArgs []string
|
||||||
rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}
|
rootCmd := &Command{Use: "root", Args: NoArgs, Run: emptyRun}
|
||||||
|
|
Loading…
Add table
Reference in a new issue