spf13 / cobra

A Commander for modern Go CLI interactions
https://cobra.dev
Apache License 2.0
38.41k stars 2.86k forks source link

Fix shorthand combination edge case #2189

Open inicula opened 2 months ago

inicula commented 2 months ago

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. https://github.com/spf13/cobra/blob/78bfc837fe358c750faa7e7f0a8016b300044d58/command.go#L1090-L1094

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 commented 2 months ago

CLA assistant check
All committers have signed the CLA.

inicula commented 2 months ago

cc @marckhouzam

github-actions[bot] commented 2 months ago

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.