Customizable command not found function

main
Chris Winslett 10 years ago
parent 3fa24ca4f3
commit 37299d4e5a

@ -25,6 +25,8 @@ type App struct {
Before func(context *Context) error
// The action to execute when no subcommands are specified
Action func(context *Context)
// Execute this function if the proper command cannot be found
CommandNotFound func(context *Context, command string)
// Compilation date
Compiled time.Time
// Author

@ -278,3 +278,26 @@ func TestAppHelpPrinter(t *testing.T) {
t.Errorf("Help printer expected to be called, but was not")
}
}
func TestAppCommandNotFound(t *testing.T) {
beforeRun, subcommandRun := false, false
app := cli.NewApp()
app.CommandNotFound = func(c *cli.Context, command string) {
beforeRun = true
}
app.Commands = []cli.Command{
cli.Command{
Name: "bar",
Action: func(c *cli.Context) {
subcommandRun = true
},
},
}
app.Run([]string{"command", "foo"})
expect(t, beforeRun, true)
expect(t, subcommandRun, false)
}

@ -73,7 +73,11 @@ func ShowCommandHelp(c *Context, command string) {
}
}
fmt.Printf("No help topic for '%v'\n", command)
if c.App.CommandNotFound != nil {
c.App.CommandNotFound(c, command)
} else {
fmt.Printf("No help topic for '%v'\n", command)
}
}
// Prints the version number of the App

Loading…
Cancel
Save