Skip to content

fix: reset ArgsLenAtDash on repeated parses - #539

Open
gyanu2507 wants to merge 2 commits into
spf13:masterfrom
gyanu2507:fix/538-reset-args-len-at-dash
Open

gyanu2507 wants to merge 2 commits into
spf13:masterfrom
gyanu2507:fix/538-reset-args-len-at-dash

Conversation

@gyanu2507

Copy link
Copy Markdown

Parse and ParseAll now reset the stored -- position before each argument list, so reusing a FlagSet cannot return a stale ArgsLenAtDash() value. Regression coverage exercises a first parse with --, followed by parses without it and with no arguments, and covers both parsing entry points.

Tests: go test -race ./...; golangci-lint run ./...

Fixes #538

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

@xiangnuans xiangnuans 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.

I independently checked a direct transition from an input containing -- to an empty reparse on Go 1.27.4 (Darwin arm64):

  1. Parse []string{"before", "--", "after"} on a new FlagSet and check that ArgsLenAtDash() is 1.
  2. Call Parse(nil) on that same FlagSet and expect ArgsLenAtDash() to be -1.

The new check fails on master at 45c23e5 (returns 1) and passes on this PR at 9453208, alongside go test -count=1 ./.... I also temporarily moved the reset below the empty-input early return: TestParseRepeated still passed, while the new check failed. The current empty subtest starts after a parse without a delimiter, so the previous call has already cleared the value.

Could the empty-input subtest start right after a parse with --, or seed the delimiter again before calling Parse(nil)? That would protect the placement of the reset. The current implementation already passes this case.

AI-assisted review. The test results above are from local execution.

@gyanu2507

Copy link
Copy Markdown
Author

Thanks for checking the placement. I added a subtest that parses before -- after and then calls Parse(nil) on the same FlagSet. It fails if the reset moves below the empty-input return and passes with the reset where it is now.

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