Skip to content

fix: reset argsLenAtDash when a FlagSet is parsed again - #547

Open
simpleqt wants to merge 1 commit into
spf13:masterfrom
simpleqt:fix/argslenatdash-reset
Open

simpleqt wants to merge 1 commit into
spf13:masterfrom
simpleqt:fix/argslenatdash-reset

Conversation

@simpleqt

Copy link
Copy Markdown

Fixes #538.

Root cause

FlagSet.Parse and FlagSet.ParseAll reset f.args on every call but leave f.argsLenAtDash from the previous parse. After a parse that contained -- (delimiter position 1), a second parse of the same FlagSet without -- still returns 1 from ArgsLenAtDash(), which contradicts the field's documented "-1 if there is no --" contract in the current argument list.

Fix

Set f.argsLenAtDash = -1 alongside the existing f.args = make([]string, 0, len(arguments)) reset, in both Parse and ParseAll. That is the same reset the first field already gets.

Test

TestParseRepeatedArgsLenAtDash reuses a FlagSet: first parse with -- stops at index 1, second parse without -- must report -1 and Args() must match. It covers both entry points.

Evidence:

  • RED with only flag.go reverted 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/pflag
  • GREEN on the fixed tree (git checkout HEAD -- flag.go): same command -> PASS, ok github.com/spf13/pflag 0.042s.
  • go build ./... exit 0; gofmt -l flag.go flag_test.go lists nothing.
  • I did not complete a full 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 plus go build ./... are what I verified, and CI is the whole-suite validation.

Signed-off-by: simpleqt 2015900818@qq.com

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>
Copilot AI balanced review requested due to automatic review settings November 14, 2026 14:48
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

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.

ArgsLenAtDash retains the previous delimiter position when a FlagSet is parsed again

3 participants