Skip to content

Commit

Permalink
Merge pull request #1952 from dearchap/issue_1951
Browse files Browse the repository at this point in the history
Fix:(issue_1951) Propagate hide help to subcommands
  • Loading branch information
dearchap authored Sep 1, 2024
2 parents 3d76e1b + f623a80 commit 31c5c84
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,21 +338,34 @@ func (cmd *Command) setupSubcommand() {
cmd.flagCategories = newFlagCategoriesFromFlags(cmd.allFlags())
}

func (cmd *Command) hideHelp() bool {
tracef("hide help (cmd=%[1]q)", cmd.Name)
for c := cmd; c != nil; c = c.parent {
if c.HideHelp {
return true
}
}

return false
}

func (cmd *Command) ensureHelp() {
tracef("ensuring help (cmd=%[1]q)", cmd.Name)

helpCommand := buildHelpCommand(true)

if cmd.Command(helpCommand.Name) == nil && !cmd.HideHelp {
if !cmd.HideHelpCommand {
tracef("appending helpCommand (cmd=%[1]q)", cmd.Name)
cmd.appendCommand(helpCommand)
if !cmd.hideHelp() {
if cmd.Command(helpCommand.Name) == nil {
if !cmd.HideHelpCommand {
tracef("appending helpCommand (cmd=%[1]q)", cmd.Name)
cmd.appendCommand(helpCommand)
}
}
}

if HelpFlag != nil && !cmd.HideHelp {
tracef("appending HelpFlag (cmd=%[1]q)", cmd.Name)
cmd.appendFlag(HelpFlag)
if HelpFlag != nil {
tracef("appending HelpFlag (cmd=%[1]q)", cmd.Name)
cmd.appendFlag(HelpFlag)
}
}
}

Expand Down Expand Up @@ -499,7 +512,7 @@ func (cmd *Command) Run(ctx context.Context, osArgs []string) (deferErr error) {
fmt.Fprintf(cmd.Root().ErrWriter, "%s", suggestion)
}
}
if !cmd.HideHelp {
if !cmd.hideHelp() {
if cmd.parent == nil {
tracef("running ShowAppHelp")
if err := ShowAppHelp(cmd); err != nil {
Expand Down Expand Up @@ -709,7 +722,7 @@ func (cmd *Command) suggestFlagFromError(err error, commandName string) (string,
}

flags := cmd.Flags
hideHelp := cmd.HideHelp
hideHelp := cmd.hideHelp()

if commandName != "" {
subCmd := cmd.Command(commandName)
Expand Down

0 comments on commit 31c5c84

Please sign in to comment.