Skip to content

reject flags claimed by more than one option - #189

Closed
metsw24-max wants to merge 1 commit into
Taywee:masterfrom
metsw24-max:duplicate-flag-matchers
Closed

reject flags claimed by more than one option#189
metsw24-max wants to merge 1 commit into
Taywee:masterfrom
metsw24-max:duplicate-flag-matchers

Conversation

@metsw24-max

Copy link
Copy Markdown
Contributor

Duplicate flag matchers are accepted silently

Two options that register the same short or long flag currently parse without complaint (issue #117): Match() returns the first option in tree order, so the second can never be reached and any value meant for it is quietly dropped. ArgumentParser::Parse and Subparser::Parse now walk the reachable flags with GetAllFlags() and raise a UsageError (or set Error::Usage under ARGS_NOEXCEPT) naming the offending flag when one is claimed by more than one option. Going through GetAllFlags() keeps the check aware of command boundaries, so sibling subcommands may still reuse a name and a single option shared between groups does not report itself.

Regression tests cover both parsing modes and the subcommand case; they fail on master and pass with the change.

@Taywee

Taywee commented Jul 16, 2026

Copy link
Copy Markdown
Owner

This is a duplicate of #187, I'll check out both, but only one will be able to be merged.

@Taywee

Taywee commented Jul 16, 2026

Copy link
Copy Markdown
Owner

The implementation here is simple and correct, but I'd like evidence that it works across groups under the same parser (but not subcommands, as those separate their arguments).

@Taywee

Taywee commented Jul 16, 2026

Copy link
Copy Markdown
Owner

After poking at it a bit, I'm closing this in favor of the earlier one, as I've already interfaced with that one and it's been changed to handle what I wanted it to handle (which is to check on every added flag, rather than on parse).

Thanks for the PR; sorry this one didn't make it in.

@Taywee Taywee closed this Jul 16, 2026
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.

2 participants