diff --git a/app.go b/app.go index dd8f1deb39..93c7918b45 100644 --- a/app.go +++ b/app.go @@ -43,8 +43,10 @@ type App struct { Flags []Flag // Boolean to enable bash completion commands EnableBashCompletion bool - // Boolean to hide built-in help command + // Boolean to hide built-in help command and help flag HideHelp bool + // Boolean to hide built-in help command but keep help flag + HideHelpCommand bool // Boolean to hide built-in version flag and the VERSION section of help HideVersion bool // categories contains the categorized commands and is populated on app startup @@ -170,7 +172,9 @@ func (a *App) Setup() { a.Commands = newCommands if a.Command(helpCommand.Name) == nil && !a.HideHelp { - a.appendCommand(helpCommand) + if !a.HideHelpCommand { + a.appendCommand(helpCommand) + } if HelpFlag != nil { a.appendFlag(HelpFlag) @@ -333,7 +337,9 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) { // append help to commands if len(a.Commands) > 0 { if a.Command(helpCommand.Name) == nil && !a.HideHelp { - a.appendCommand(helpCommand) + if !a.HideHelpCommand { + a.appendCommand(helpCommand) + } if HelpFlag != nil { a.appendFlag(HelpFlag) diff --git a/app_test.go b/app_test.go index 61c55d8551..142dbbeb3f 100644 --- a/app_test.go +++ b/app_test.go @@ -2152,3 +2152,40 @@ func newTestApp() *App { a.Writer = ioutil.Discard return a } + +func TestHideHelpCommand(t *testing.T) { + app := &App{ + HideHelpCommand: true, + Writer: ioutil.Discard, + } + + err := app.Run([]string{"foo", "help"}) + if err == nil { + t.Fatalf("expected a non-nil error") + } + if !strings.Contains(err.Error(), "No help topic for 'help'") { + t.Errorf("Run returned unexpected error: %v", err) + } + + err = app.Run([]string{"foo", "--help"}) + if err != nil { + t.Errorf("Run returned unexpected error: %v", err) + } +} + +func TestHideHelpCommandFalse(t *testing.T) { + app := &App{ + HideHelpCommand: false, + Writer: ioutil.Discard, + } + + err := app.Run([]string{"foo", "help"}) + if err != nil { + t.Errorf("Run returned unexpected error: %v", err) + } + + err = app.Run([]string{"foo", "--help"}) + if err != nil { + t.Errorf("Run returned unexpected error: %v", err) + } +} diff --git a/command.go b/command.go index db6c8024ba..323a680abf 100644 --- a/command.go +++ b/command.go @@ -41,8 +41,10 @@ type Command struct { Flags []Flag // Treat all flags as normal arguments if true SkipFlagParsing bool - // Boolean to hide built-in help command + // Boolean to hide built-in help command and help flag HideHelp bool + // Boolean to hide built-in help command but keep help flag + HideHelpCommand bool // Boolean to hide this command from help or completion Hidden bool // Boolean to enable short-option handling so user can combine several @@ -236,6 +238,7 @@ func (c *Command) startApp(ctx *Context) error { app.Commands = c.Subcommands app.Flags = c.Flags app.HideHelp = c.HideHelp + app.HideHelpCommand = c.HideHelpCommand app.Version = ctx.App.Version app.HideVersion = ctx.App.HideVersion diff --git a/flag.go b/flag.go index 524de42f4b..6257f7d96f 100644 --- a/flag.go +++ b/flag.go @@ -36,7 +36,7 @@ var VersionFlag Flag = &BoolFlag{ // HelpFlag prints the help for all commands and subcommands. // Set to nil to disable the flag. The subcommand -// will still be added unless HideHelp is set to true. +// will still be added unless HideHelp or HideHelpCommand is set to true. var HelpFlag Flag = &BoolFlag{ Name: "help", Aliases: []string{"h"},