cobra icon indicating copy to clipboard operation
cobra copied to clipboard

Fix shorthand combination edge case

Open inicula opened this issue 5 months ago • 3 comments

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

inicula avatar Sep 14 '24 14:09 inicula