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 shorthand combination edge case #2189

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

Conversation

inicula
Copy link

@inicula inicula commented Sep 14, 2024

Fixes: #2188

Note: This PR fixes the same bug in two different code paths (c.Find() and c.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

if c.TraverseChildren {
cmd, flags, err = c.Traverse(args)
} else {
cmd, flags, err = c.Find(args)
}

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

@CLAassistant
Copy link

CLAassistant commented Sep 14, 2024

CLA assistant check
All committers have signed the CLA.

@inicula
Copy link
Author

inicula commented Sep 14, 2024

cc @marckhouzam

@inicula inicula force-pushed the fix-shorthand-combination-bug branch from 56997f2 to b07c5cd Compare September 14, 2024 17:36
Copy link

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" {
Copy link
Author

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" {
Copy link
Author

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Combining shorthand flags before command names does not work
2 participants