-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Fix:(issue_2225) Make external flags more robust. #2227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
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`.
| } | ||
|
|
||
| func (e *extFlag) IsSet() bool { | ||
| return false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually you can set a flag in the "extFlag.Set()" method and check that here.
| 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 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 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 ?
| } | ||
|
|
||
| // IsDefaultVisible returns true if the flag is not hidden, otherwise false | ||
| func (e *extFlag) IsDefaultVisible() bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this dependent on the DefaultValue ? similar to flag library?
| } | ||
|
|
||
| // RunAction executes flag action if set | ||
| func (e *extFlag) RunAction(ctx context.Context, cmd *Command) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you dont need to define this method. interface casting will check if Flag is an ActionableFlag and call accordingly
dearchap
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bbadour Can you add some more tests ?
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