-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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 shorthand combination edge case #2189
base: main
Are you sure you want to change the base?
Conversation
cc @marckhouzam |
56997f2
to
b07c5cd
Compare
This PR exceeds the recommended size of 200 lines. Please make sure you are NOT addressing multiple issues with one PR. Note this PR might be rejected due to its size. |
@@ -2253,14 +2253,70 @@ func TestTraverseWithParentFlags(t *testing.T) { | |||
if err != nil { | |||
t.Errorf("Unexpected error: %v", err) | |||
} | |||
if len(args) != 1 && args[0] != "--add" { | |||
if len(args) != 1 || args[0] != "--int" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original condition seems wrong. --add
wasn't mentioned anywhere else in the repo and from how I understand the test, ||
makes sense here, not &&
.
@@ -2312,7 +2368,7 @@ func TestTraverseWithBadChildFlag(t *testing.T) { | |||
if err != nil { | |||
t.Errorf("Unexpected error: %v", err) | |||
} | |||
if len(args) != 1 && args[0] != "--str" { | |||
if len(args) != 1 || args[0] != "--str" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See above.
Use a helper function for both code paths instead of duplicating the logic.
Fixes: #2188
Note: This PR fixes the same bug in two different code paths (
c.Find()
andc.Traverse()
). They use different helper functions for parsing the arguments, but the fix is based on the same idea.cobra/command.go
Lines 1090 to 1094 in 78bfc83
The idea is that whenever we encounter a shorthand combination, we search for the first flag in the combination that needs a value (if there are no such flags, then we just go to the next argument). If this flag is the last flag in the shorthand combination, then the value will be expected in the next argument. Otherwise, if the flag is not on the last position of the shorthand combination, the value is expected to be given in the same argument (in this case, we go to the next argument without doing anything).