From 780f839a02f0e2c93d91fbfc54f839e9596034f4 Mon Sep 17 00:00:00 2001 From: jszwedko Date: Mon, 1 Dec 2014 23:20:21 -0500 Subject: [PATCH] Allow hiding of help flag without hiding help subcommand By utilizing struct zero value --- app.go | 8 ++++++-- app_test.go | 17 +++++++++++++++++ command.go | 2 +- flag.go | 2 ++ 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/app.go b/app.go index f4c4af8..d90400d 100644 --- a/app.go +++ b/app.go @@ -70,7 +70,9 @@ func (a *App) Run(arguments []string) error { // append help to commands if a.Command(helpCommand.Name) == nil && !a.HideHelp { a.Commands = append(a.Commands, helpCommand) - a.appendFlag(HelpFlag) + if (HelpFlag != BoolFlag{}) { + a.appendFlag(HelpFlag) + } } //append version/help flags @@ -150,7 +152,9 @@ func (a *App) RunAsSubcommand(ctx *Context) error { if len(a.Commands) > 0 { if a.Command(helpCommand.Name) == nil && !a.HideHelp { a.Commands = append(a.Commands, helpCommand) - a.appendFlag(HelpFlag) + if (HelpFlag != BoolFlag{}) { + a.appendFlag(HelpFlag) + } } } diff --git a/app_test.go b/app_test.go index 81d1174..ede76c7 100644 --- a/app_test.go +++ b/app_test.go @@ -1,6 +1,7 @@ package cli_test import ( + "flag" "fmt" "os" "testing" @@ -331,6 +332,22 @@ func TestApp_BeforeFunc(t *testing.T) { } +func TestAppNoHelpFlag(t *testing.T) { + oldFlag := cli.HelpFlag + defer func() { + cli.HelpFlag = oldFlag + }() + + cli.HelpFlag = cli.BoolFlag{} + + app := cli.NewApp() + err := app.Run([]string{"test", "-h"}) + + if err != flag.ErrHelp { + t.Errorf("expected error about missing help flag, but got: %s (%T)", err, err) + } +} + func TestAppHelpPrinter(t *testing.T) { oldPrinter := cli.HelpPrinter defer func() { diff --git a/command.go b/command.go index 5622b38..1171196 100644 --- a/command.go +++ b/command.go @@ -40,7 +40,7 @@ func (c Command) Run(ctx *Context) error { return c.startApp(ctx) } - if !c.HideHelp { + if !c.HideHelp && (HelpFlag != BoolFlag{}) { // append help to flags c.Flags = append( c.Flags, diff --git a/flag.go b/flag.go index b30bca3..3af14b7 100644 --- a/flag.go +++ b/flag.go @@ -21,6 +21,8 @@ var VersionFlag = BoolFlag{ } // This flag prints the help for all commands and subcommands +// Set to the zero value (BoolFlag{}) to disable flag -- keeps subcommand +// unless HideHelp is set to true) var HelpFlag = BoolFlag{ Name: "help, h", Usage: "show help",