fix: handle a multi-byte flag at the end of a short option group - #2438
Open
SulimanAbdulrazzaq wants to merge 1 commit into
Open
SulimanAbdulrazzaq wants to merge 1 commit into
SulimanAbdulrazzaq wants to merge 1 commit into
Conversation
parseFlags ranges over a short option group by byte offset, but found the last flag, the only one that can take its value from the next argument, by comparing that offset with len(flagName)-1. When the group ends in a multi-byte flag that takes a value, such as a string flag named "ש", the check never matched, so "-vש value" left the flag unset, raised no error and passed "value" on as a positional argument. Compare the offset with where the last rune starts instead. Groups that end in an ASCII flag behave exactly as before. Follow-up to urfave#2434.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What type of PR is this?
What this PR does / why we need it:
With
UseShortOptionHandling,parseFlagssplits a group such as-vשinto single flags. It ranges over the group by byte offset, but it found the last flag, the only one that can take its value from the next argument, withindex == len(flagName)-1. When the group ends in a multi-byte flag such asש(the name used in #2433), that check never matches:-vש value operandleavesשunset, raises no error, and passesvalueon as a positional argument.-vשwith nothing after it does not reportflag needs an argument.Output of a small program with
UseShortOptionHandlingand three flags:v(bool),o(string) andש(string):-vo out.txt operandand-v -ש value operandalready worked before this change.command_parse.go: find where the last rune of the group starts, and compare the byte offset against that in both places that usedlen(flagName)-1. Groups that end in an ASCII flag behave exactly as before.command_test.go:TestCommand_SingleRuneUnicodeFlagEndsShortOptionGroupchecks the value, the remaining arguments, and the missing-value error.Which issue(s) this PR fixes:
There is no separate issue. This follows up #2433 / #2434, which fixed standalone single-rune Unicode flags.
Special notes for your reviewer:
Two places still choose the
-/--prefix by byte length, so they show a single-rune non-ASCII name with--. Other flags have shown-שsince #2434.BoolWithInverseFlag.String(len(bif.Name) == 1andlen(alias) == 1) shows--[no-]ש.suggestions.go(len(suggestion) == 1) suggests--ש.Both only affect output (the parser accepts either prefix), so they are left out of this PR. They can be added here or sent separately if you prefer.
Testing
main: the value is"", the arguments are["value" "operand"], and no error is returned when the value is missing. It passes with this change.go test ./...go vet ./...go run scripts/build.gowithcheck-binary-size,generate,diffcheckandv3diff-race.Release Notes