Repository navigation
Conversation
Parse and ParseAll reset f.args for each call but left argsLenAtDash from the previous parse. Afterwards ArgsLenAtDash() keeps pointing at a -- that is not in the current argument list, contradicting its documented -1-when-absent contract and making the Args()/dash pair inconsistent. Reset argsLenAtDash to -1 alongside f.args in both entry points. Signed-off-by: simpleqt <2015900818@qq.com>
|
|
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.
Fixes #538.
Root cause
FlagSet.ParseandFlagSet.ParseAllresetf.argson every call but leavef.argsLenAtDashfrom the previous parse. After a parse that contained--(delimiter position 1), a second parse of the same FlagSet without--still returns 1 fromArgsLenAtDash(), which contradicts the field's documented "-1if there is no--" contract in the current argument list.Fix
Set
f.argsLenAtDash = -1alongside the existingf.args = make([]string, 0, len(arguments))reset, in bothParseandParseAll. That is the same reset the first field already gets.Test
TestParseRepeatedArgsLenAtDashreuses a FlagSet: first parse with--stops at index 1, second parse without--must report -1 andArgs()must match. It covers both entry points.Evidence:
flag.goreverted to base:git checkout 046b0d8 -- flag.go;go test -run TestParseRepeatedArgsLenAtDash -count=1 -v .->flag_test.go:903: expected argsLenAtDash -1 after re-parse without --, got 1;FAIL github.com/spf13/pflaggit checkout HEAD -- flag.go): same command ->PASS,ok github.com/spf13/pflag 0.042s.go build ./...exit 0;gofmt -l flag.go flag_test.golists nothing.go test .run on this Windows host (the full-binary invocation stalls in my shell harness), so I am not claiming a whole-suite result here - the focused regression plusgo build ./...are what I verified, and CI is the whole-suite validation.Signed-off-by: simpleqt 2015900818@qq.com