cobra icon indicating copy to clipboard operation
cobra copied to clipboard

Fix unexpected executing subcommand

Open JaySon-Huang opened this issue 3 years ago • 4 comments

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.

JaySon-Huang avatar Dec 02 '21 19:12 JaySon-Huang

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Dec 02 '21 19:12 CLAassistant

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.

marckhouzam avatar Dec 02 '21 20:12 marckhouzam

@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.

JaySon-Huang avatar Dec 03 '21 02:12 JaySon-Huang

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

github-actions[bot] avatar Feb 02 '22 00:02 github-actions[bot]