Merge pull request #153 from codegangsta/allow-hiding-of-help-flag

Allow hiding of help flag without hiding help subcommand
main
Dan Buch 10 years ago
commit 5b9e204508

@ -94,7 +94,9 @@ func (a *App) Run(arguments []string) error {
// append help to commands // append help to commands
if a.Command(helpCommand.Name) == nil && !a.HideHelp { if a.Command(helpCommand.Name) == nil && !a.HideHelp {
a.Commands = append(a.Commands, helpCommand) a.Commands = append(a.Commands, helpCommand)
a.appendFlag(HelpFlag) if (HelpFlag != BoolFlag{}) {
a.appendFlag(HelpFlag)
}
} }
//append version/help flags //append version/help flags
@ -174,7 +176,9 @@ func (a *App) RunAsSubcommand(ctx *Context) error {
if len(a.Commands) > 0 { if len(a.Commands) > 0 {
if a.Command(helpCommand.Name) == nil && !a.HideHelp { if a.Command(helpCommand.Name) == nil && !a.HideHelp {
a.Commands = append(a.Commands, helpCommand) a.Commands = append(a.Commands, helpCommand)
a.appendFlag(HelpFlag) if (HelpFlag != BoolFlag{}) {
a.appendFlag(HelpFlag)
}
} }
} }

@ -1,6 +1,7 @@
package cli_test package cli_test
import ( import (
"flag"
"fmt" "fmt"
"os" "os"
"testing" "testing"
@ -375,6 +376,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) { func TestAppHelpPrinter(t *testing.T) {
oldPrinter := cli.HelpPrinter oldPrinter := cli.HelpPrinter
defer func() { defer func() {

@ -40,7 +40,7 @@ func (c Command) Run(ctx *Context) error {
return c.startApp(ctx) return c.startApp(ctx)
} }
if !c.HideHelp { if !c.HideHelp && (HelpFlag != BoolFlag{}) {
// append help to flags // append help to flags
c.Flags = append( c.Flags = append(
c.Flags, c.Flags,

@ -21,6 +21,8 @@ var VersionFlag = BoolFlag{
} }
// This flag prints the help for all commands and subcommands // 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{ var HelpFlag = BoolFlag{
Name: "help, h", Name: "help, h",
Usage: "show help", Usage: "show help",

Loading…
Cancel
Save