Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix:(issue_1951) Propagate hide help to subcommands #1952

Merged
merged 4 commits into from
Sep 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading