Support custom FlagStringer for MutuallyExclusiveFlags - #2414
Open
SHIVANSHGARG07 wants to merge 3 commits into
Open
Support custom FlagStringer for MutuallyExclusiveFlags#2414SHIVANSHGARG07 wants to merge 3 commits into
SHIVANSHGARG07 wants to merge 3 commits into
Conversation
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
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What type of PR is this?
What this PR does / why we need it:
Adds a
Stringerfield toMutuallyExclusiveFlagsthat lets callersoverride 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 theFlagStringerOverriderinterface(
SetStringer(FlagStringFunc)) that flags implement to opt in to acustom stringer.
flag_mutex.go: addedStringer FlagStringFuncfield toMutuallyExclusiveFlags, pluspropagateStringer()which pushes thegroup's stringer down to member flags (mirrors the existing
propagateCategory()pattern used forCategory).flag_impl.go:FlagBasenow stores an optional per-flagstringerand uses it in
String()when set; implementsSetStringer.flag_bool_with_inverse.go: same treatment forBoolWithInverseFlag,which doesn't embed
FlagBaseand needed a separate implementation.command_setup.go: callsgrp.propagateStringer()alongside theexisting
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
propagateCategorypattern for consistency.BoolWithInverseFlagneeded a standalone implementation since itdoesn't embed
FlagBase.Testing
Added
TestMutuallyExclusiveFlags_PropagateStringerandTestMutuallyExclusiveFlags_PropagateStringerNilinflag_mutex_test.go. Ran the full suite withgo test ./...— allpassing.
Release Notes