Skip to content

Commit

Permalink
Add HideHelpCommand
Browse files Browse the repository at this point in the history
While `HideHelp` hides both `help` command and `--help` flag, `HideHelpCommand`
only hides `help` command and leave `--help` flag as-is.

The behavior of `HideHelp` is untouched in this commit.

Fix #523
Replace #636

Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
  • Loading branch information
AkihiroSuda committed Mar 5, 2020
1 parent 1b7e4e0 commit 2426f97
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
12 changes: 9 additions & 3 deletions app.go
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
37 changes: 37 additions & 0 deletions app_test.go
Expand Up @@ -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)
}
}
5 changes: 4 additions & 1 deletion command.go
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion flag.go
Expand Up @@ -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"},
Expand Down

0 comments on commit 2426f97

Please sign in to comment.