Skip to content

Support custom FlagStringer for MutuallyExclusiveFlags - #2414

Open
SHIVANSHGARG07 wants to merge 3 commits into
urfave:mainfrom
SHIVANSHGARG07:fix-mutex-flags-stringer
Open

Support custom FlagStringer for MutuallyExclusiveFlags#2414
SHIVANSHGARG07 wants to merge 3 commits into
urfave:mainfrom
SHIVANSHGARG07:fix-mutex-flags-stringer

Conversation

@SHIVANSHGARG07

Copy link
Copy Markdown

What type of PR is this?

  • feature

What this PR does / why we need it:

Adds a Stringer field to MutuallyExclusiveFlags that lets callers
override how flags within the group are rendered in help output.
Previously, flags inside a mutex group could only use the package-level
FlagStringer, so there was no way to add group-specific context (e.g.
marking one of the flags as "required") to the help text.

  • flag.go: added the FlagStringerOverrider interface
    (SetStringer(FlagStringFunc)) that flags implement to opt in to a
    custom stringer.
  • flag_mutex.go: added Stringer FlagStringFunc field to
    MutuallyExclusiveFlags, plus propagateStringer() which pushes the
    group's stringer down to member flags (mirrors the existing
    propagateCategory() pattern used for Category).
  • flag_impl.go: FlagBase now stores an optional per-flag stringer
    and uses it in String() when set; implements SetStringer.
  • flag_bool_with_inverse.go: same treatment for BoolWithInverseFlag,
    which doesn't embed FlagBase and needed a separate implementation.
  • command_setup.go: calls grp.propagateStringer() alongside the
    existing grp.propagateCategory() call during command setup.
  • godoc-current.txt: regenerated to reflect the new public API.

Which issue(s) this PR fixes:

Fixes #2220

Special notes for your reviewer:

Followed the existing propagateCategory pattern for consistency.
BoolWithInverseFlag needed a standalone implementation since it
doesn't embed FlagBase.

Testing

Added TestMutuallyExclusiveFlags_PropagateStringer and
TestMutuallyExclusiveFlags_PropagateStringerNil in
flag_mutex_test.go. Ran the full suite with go test ./... — all
passing.

Release Notes

Added a `Stringer` field to `MutuallyExclusiveFlags`, allowing a custom
`FlagStringFunc` to override how flags within a mutually exclusive
group are displayed in help output.

Adds a Stringer field to MutuallyExclusiveFlags that lets callers
override how flags within the group are rendered in help output.
Flags opt in via the new FlagStringerOverrider interface.

Fixes urfave#2220
@SHIVANSHGARG07
SHIVANSHGARG07 requested a review from a team as a code owner August 18, 2026 16:29
FlagStringFunc is a func type and cannot be marshaled to JSON, which
broke the staticcheck SA1026 lint check via json.Marshal(cmd) in
existing tests. Tag the field with json:"-" to exclude it.
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.

Mutually exclusive flags in help output

1 participant