Skip to content

fix: respect Hidden flag for help command in usage output#2371

Open
ricardbejarano wants to merge 1 commit intospf13:mainfrom
bejaratommy:fix/issue-2269
Open

fix: respect Hidden flag for help command in usage output#2371
ricardbejarano wants to merge 1 commit intospf13:mainfrom
bejaratommy:fix/issue-2269

Conversation

@ricardbejarano
Copy link

Summary

Fixes #2269

The help command was hardcoded to always appear in usage listings, completely ignoring its Hidden field. This meant that setting cmd.Hidden = true on the help command had no effect, making the property meaningless for it.

What changed

Updated all locations where the help command receives special treatment to check !subcmd.Hidden before including it in output:

  • defaultUsageTemplate: template conditions updated from (eq .Name "help") to (and (eq .Name "help") (not .Hidden))
  • printOptionCmdUsages: three loop conditions updated
  • AllChildCommandsHaveGroup: condition updated to respect Hidden
  • Shell completion listing: condition updated to respect Hidden

How to hide the help command

rootCmd.InitDefaultHelpCmd()
rootCmd.helpCommand.Hidden = true

Or via SetHelpCommand:

rootCmd.SetHelpCommand(&cobra.Command{Hidden: true})

Testing

Added TestHiddenHelpCommandIsHidden which verifies the help command appears in usage by default and disappears when Hidden is set to true.

All existing tests pass.

The help command was always shown in command listings, ignoring its
Hidden field. This made it impossible to hide the help command from
usage output using the standard Hidden property.

Update all places where the help command is given special treatment
(usage template, printOptionCmdUsages, AllChildCommandsHaveGroup, and
shell completion) to check Hidden before including it in output.

Fixes spf13#2269

Signed-off-by: Tommy <tommy@bejarano.io>
@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


Tommy seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

help cmd ignores "HIdden" property

3 participants