Merge pull request #75 from Winslett/command-not-found

Customizable command not found function
main
Jeremy Saenz 11 years ago
commit 5e1cde20d3

@ -29,6 +29,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

@ -346,3 +346,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)
}

@ -115,7 +115,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 help for the given subcommand

Loading…
Cancel
Save