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

Same flag in different groups that created by MarkFlagsRequiredTogether not work as expected #1738

Closed
aegoroff opened this issue Jun 21, 2022 · 3 comments
Labels
area/flags-args Changes to functionality around command line flags and args kind/feature A feature request for cobra; new or enhanced behavior lifecycle/needs-pr Ready for a PR from the community

Comments

@aegoroff
Copy link

aegoroff commented Jun 21, 2022

Hi all!

I would like to have different groups that created using option MarkFlagsRequiredTogether with the same flag. I.e. I would like to have this:

rootCmd.MarkFlagsRequiredTogether("cpuprofile","diag")
rootCmd.MarkFlagsRequiredTogether("memprofile", "diag")

to run memory or cpu profiling independently, like this:

--diag --memprofile

or

--diag --cpuprofile

or even altogether like this:

--diag --memprofile --cpuprofile

but independent run failed with

Error: if any flags in the group [cpuprofile diag] are set they must all be set; missing [cpuprofile]

@marckhouzam
Copy link
Collaborator

Thanks for the report @aegoroff.
Your usecase does makes sense to me, but the flag group feature does not support such a case 😞

If you specify MarkFlagsRequiredTogether() then if any of the specified flags is present on the command-line, then all others must be present also.

@marckhouzam marckhouzam added kind/feature A feature request for cobra; new or enhanced behavior area/flags-args Changes to functionality around command line flags and args lifecycle/needs-pr Ready for a PR from the community labels Jun 21, 2022
@aegoroff
Copy link
Author

Thank you for reply!

If you specify MarkFlagsRequiredTogether() then if any of the specified flags is present on the command-line, then all others must be present also.

Sure, so you cannot share an option among different groups. If so you have to use all options from all touched groups :(

It seems that MarkFlagsRequiredTogether call should create different groups that should be validated independently no matter a group option shared with another group or not

@github-actions
Copy link

The Cobra project currently lacks enough contributors to adequately respond to all issues. This bot triages issues and PRs according to the following rules:

  • After 60d of inactivity, lifecycle/stale is applied. - After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied and the issue is closed.
    You can:
  • Make a comment to remove the stale label and show your support. The 60 days reset. - If an issue has lifecycle/rotten and is closed, comment and ask maintainers if they'd be interseted in reopening

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/feature A feature request for cobra; new or enhanced behavior lifecycle/needs-pr Ready for a PR from the community
Projects
None yet
Development

No branches or pull requests

2 participants