Fix:(issue_2225) Make external flags more robust.#2227
Open
bbadour wants to merge 1 commit intourfave:mainfrom
Open
Fix:(issue_2225) Make external flags more robust.#2227bbadour wants to merge 1 commit intourfave:mainfrom
bbadour wants to merge 1 commit intourfave:mainfrom
Conversation
Use delegation to implement the Value+Flag paradigm used for other flag types. Implement all of the Flag interfaces except Count. Borrowed heavily from flag_bool.go and flag_generic.go Add context to the error message when setting an external flag's value to its default value to avoid cryptic error messages like: `"syntax error: expected file.go:234"` And suppress the error for odd-ball external flags that report the string representation of the zero-value of some structure as the default, but do not accept that string as input. Detect such odd-balls by their `Get()` method returning `nil`.
bbadour
commented
Nov 22, 2025
| } | ||
|
|
||
| func (e *extFlag) IsSet() bool { | ||
| return false |
Author
There was a problem hiding this comment.
Migrating some code from v1, I found this could be less astonishing if, instead, it were:
if e == nil || e.f == nil || e.f.Value == nil {
return false
}
return e.f.Value.String() != e.f.DefValue
Contributor
There was a problem hiding this comment.
Actually you can set a flag in the "extFlag.Set()" method and check that here.
dearchap
reviewed
Nov 22, 2025
| if e.f.DefValue != "" { | ||
| return e.Set("", e.f.DefValue) | ||
| // suppress errors for write-only external flags that always return nil | ||
| if err := e.Set("", e.f.DefValue); err != nil && e.f.Value.(flag.Getter).Get() != nil { |
Contributor
There was a problem hiding this comment.
Suggested change
| if err := e.Set("", e.f.DefValue); err != nil && e.f.Value.(flag.Getter).Get() != nil { | |
| if err := e.Set("", e.f.DefValue); err != nil { |
do you need the Get() != nil check ?
dearchap
reviewed
Nov 22, 2025
| } | ||
|
|
||
| // IsDefaultVisible returns true if the flag is not hidden, otherwise false | ||
| func (e *extFlag) IsDefaultVisible() bool { |
Contributor
There was a problem hiding this comment.
is this dependent on the DefaultValue ? similar to flag library?
dearchap
reviewed
Nov 22, 2025
| } | ||
|
|
||
| // RunAction executes flag action if set | ||
| func (e *extFlag) RunAction(ctx context.Context, cmd *Command) error { |
Contributor
There was a problem hiding this comment.
you dont need to define this method. interface casting will check if Flag is an ActionableFlag and call accordingly
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.
Use delegation to implement the Value+Flag paradigm used for other flag types.
Implement all of the Flag interfaces except Count.
Borrowed heavily from flag_bool.go and flag_generic.go
Add context to the error message when setting an external flag's value to its default value to avoid cryptic error messages like:
"syntax error: expected file.go:234"And suppress the error for odd-ball external flags that report the string representation of the zero-value of some structure as the default, but do not accept that string as input. Detect such odd-balls by their
Get()method returningnil.What type of PR is this?
What this PR does / why we need it:
Solve:
Which issue(s) this PR fixes:
Fixes #2225
Testing
Installed gfmrun and make passes. Also the same code passes the test cases for a large code base that uses the feature.
Release Notes