Skip to content

Fix Issue #2385 - #2386

Merged
dearchap merged 3 commits into
urfave:mainfrom
BrandonIrizarry:bci
Aug 16, 2026
Merged

Fix Issue #2385#2386
dearchap merged 3 commits into
urfave:mainfrom
BrandonIrizarry:bci

Conversation

@BrandonIrizarry

Copy link
Copy Markdown
Contributor

What type of PR is this?

bugfix

  • bug

What this PR does / why we need it:

Mutual exclusion wasn't being properly enforced when using MutuallyExclusiveFlags.

flag_mutex_test.go

  • Introduce a breaking test that illustrates the bug.

flag_mutex.go

  • Rewrite function check to pass the breaking test.

Which issue(s) this PR fixes:

Fixes #2385

Testing

I ran make test at the project root.

Release Notes

(REQUIRED)

NONE

@BrandonIrizarry
BrandonIrizarry requested a review from a team as a code owner July 8, 2026 02:26
@dearchap

Copy link
Copy Markdown
Contributor

Minor cleanup suggestion: the two scan loops in MutuallyExclusiveFlags.check (the one that finds the group where a flag is set, and the one that scans subsequent groups for a second set flag) are structurally identical. Extracting a small helper, e.g.

func (grp MutuallyExclusiveFlags) findSetFlag(i int) (name string, idx int, ok bool)

that returns the first set flag at or after group index i, would remove the duplication and make the flagGroupLoop labeled-break idiom unnecessary:

name, i, ok := grp.findSetFlag(0)
if ok {
    e.flag1Name = name
    i++
    if name2, _, ok := grp.findSetFlag(i); ok {
        e.flag2Name = name2
        return e
    }
}

Pure refactor, no behavior change. Happy to open a follow-up PR if you prefer.

This intentionally creates a failing test. We're trying to prove that,
if we have mutually exclusive flags that look like:

{ { A, B }, { C, D } }

It should be the case that any combination that draws from both sets
should result in an error. However, currently this isn't the case; for
example, the code considers the combination "--A --D" to be legal.
@BrandonIrizarry

BrandonIrizarry commented Aug 15, 2026

Copy link
Copy Markdown
Contributor Author

OK, so this should do the trick. I've added some comments (including a godoc-style comment for 'findSetFlag'); hopefully these are welcome (as I also kind of wanted to vet my understanding of the code I was writing.)

@dearchap
dearchap merged commit 339a375 into urfave:main Aug 16, 2026
9 checks passed
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.

MutuallyExclusiveFlags doesn't enforce full mutual exclusion

2 participants