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: show flags that shadow parent persistent flag in child help #1776

Merged
merged 1 commit into from Aug 28, 2022

Conversation

brianpursley
Copy link
Contributor

@brianpursley brianpursley commented Aug 17, 2022

This fixes a bug where a child flag that shadows (has the same name as) a parent persistent flag would not be shown in the child command's help output and the parent flag would be shown instead under the global flags section.

This change makes the help output consistent with the observed behavior during execution, where the child flag is the one that is actually used.

This change also allows a previously skipped/failing test to be unskipped because it passes now: TestOverwrittenFlag

Fixes #1651

@github-actions github-actions bot added the size/M Denotes a PR that chanes 24-99 lines label Aug 17, 2022
@marckhouzam marckhouzam added kind/bug A bug in cobra; unintended behavior area/flags-args Changes to functionality around command line flags and args labels Aug 18, 2022
@marckhouzam marckhouzam added this to the 1.6.0 milestone Aug 18, 2022
@marckhouzam
Copy link
Collaborator

This looks good @brianpursley, thanks!
I want to give it a bit more review attention but it should be fine to merge soon.

Copy link
Collaborator

@marckhouzam marckhouzam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me. I've tested it with kubectl and it does fix the problem for the help output. It also, as far as I can see, not change the behaviour when parsing flags for actual execution, which was already working as expected.

Just a nit to follow-up on @brianpursley and we can merge this.

command_test.go Outdated Show resolved Hide resolved
This fixes a bug where a child flag that shadows (has the same
name as) a parent persistent flag would not be shown in the
child command's help output and the parent flag would be shown
instead under the global flags section.

This change makes the help output consistent with the
observed behavior during execution, where the child flag is
the one that is actually used.
Copy link
Collaborator

@marckhouzam marckhouzam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @brianpursley !

@marckhouzam marckhouzam merged commit 22b6179 into spf13:main Aug 28, 2022
jimschubert added a commit to jimschubert/cobra that referenced this pull request Oct 3, 2022
* main: (39 commits)
  Add '--version' flag to Help output (spf13#1707)
  Expose ValidateRequiredFlags and ValidateFlagGroups (spf13#1760)
  Document option to hide the default completion cmd (spf13#1779)
  ci: add workflow_dispatch (spf13#1387)
  add missing license headers (spf13#1809)
  ci: use action/setup-go's cache (spf13#1783)
  Adjustments to documentation (spf13#1656)
  Rename Powershell completion tests (spf13#1803)
  Support for case-insensitive command names (spf13#1802)
  Deprecate ExactValidArgs() and test combinations of args validators (spf13#1643)
  Use correct stale action `exempt-` yaml keys (spf13#1800)
  With go 1.18, we must use go install for a binary (spf13#1726)
  Clarify SetContext documentation (spf13#1748)
  ci: test on Golang 1.19 (spf13#1782)
  fix: show flags that shadow parent persistent flag in child help (spf13#1776)
  Update gopkg.in/yaml.v2 to gopkg.in/yaml.v3 (spf13#1766)
  fix(bash-v2): activeHelp length check syntax (spf13#1762)
  fix: correct command path in see_also for YAML doc (spf13#1771)
  build(deps): bump github.com/inconshreveable/mousetrap (spf13#1774)
  docs: add zitadel to the list (spf13#1772)
  ...
hoshsadiq pushed a commit to zulucmd/zulu that referenced this pull request Dec 31, 2022
This fixes a bug where a child flag that shadows (has the same
name as) a parent persistent flag would not be shown in the
child command's help output and the parent flag would be shown
instead under the global flags section.

This change makes the help output consistent with the
observed behavior during execution, where the child flag is
the one that is actually used.

Fixes spf13/cobra#1651

Merge spf13/cobra#1776
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/flags-args Changes to functionality around command line flags and args kind/bug A bug in cobra; unintended behavior size/M Denotes a PR that chanes 24-99 lines
Projects
None yet
Development

Successfully merging this pull request may close these issues.

When persistent flag is shadowed, help text does not show shadowing flag
2 participants