diff --git a/app.go b/app.go index a792705..8a5388f 100644 --- a/app.go +++ b/app.go @@ -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 diff --git a/app_test.go b/app_test.go index e5f8b83..0b9e154 100644 --- a/app_test.go +++ b/app_test.go @@ -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) +} diff --git a/help.go b/help.go index 0b81894..47a4e12 100644 --- a/help.go +++ b/help.go @@ -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