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 unexpected executing subcommand #1550

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

JaySon-Huang
Copy link

@JaySon-Huang JaySon-Huang commented Dec 2, 2021

I've defined a subcommand with two boolean arguments and found it unexpectedly run into the subcommand.
In specific, a subcommand naming "sub" with two boolean arguments "a" and "b". If I parse the command with arguments " --a sub" or "--b sub", it returns an "unknown argument" error as expected.
But when I parse the command with arguments "--a --b sub", it unexpectedly runs into the "sub" command with those two boolean arguments set.

I trace down the codes in cobra and find that stripFlags always assume the pattern of flags is'--flag arg' or '-f arg', ignoring the pattern of a boolean flag('--bflag'). So I file a PR trying to fix this unexpected behavior.

Signed-off-by: JaySon-Huang <tshent@qq.com>
Signed-off-by: JaySon-Huang <tshent@qq.com>
Signed-off-by: JaySon-Huang <tshent@qq.com>
@CLAassistant
Copy link

CLAassistant commented Dec 2, 2021

CLA assistant check
All committers have signed the CLA.

@marckhouzam
Copy link
Collaborator

Thanks @JaySon-Huang! I can reproduce the problem. I haven't looked at the fix yet, but we will need to be very careful not to break anything.

@JaySon-Huang
Copy link
Author

@marckhouzam sure. I add some comments on the code to make my assumptions more clear. Feel free to leave any comments when you are free.

Signed-off-by: JaySon-Huang <tshent@qq.com>
@github-actions
Copy link

github-actions bot commented Feb 2, 2022

This PR is being marked as stale due to a long period of inactivity

@marckhouzam marckhouzam added kind/bug A bug in cobra; unintended behavior and removed kind/stale labels Feb 24, 2022
@marckhouzam marckhouzam added this to the 1.8.0 milestone Apr 5, 2023
@marckhouzam marckhouzam modified the milestones: 1.8.0, 1.9.0 Oct 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug A bug in cobra; unintended behavior
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants