Repository navigation
Fix:(issue_2225) Make external flags more robust. - #2227
Conversation
| } | ||
|
|
||
| func (e *extFlag) IsSet() bool { | ||
| return false |
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
There was a problem hiding this comment.
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.
| 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.
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.
you dont need to define this method. interface casting will check if Flag is an ActionableFlag and call accordingly
There was a problem hiding this comment.
Thanks for the fix! I reproduced all three problems on main (e03df0f). For example, an app using klog v2.140.0 or glog v1.0.0 fails to start, even for --help, with syntax error: expect file.go:234.
I rebased on main and simplified the change. PreParse set every flag to its DefValue (added in #2094):
Lines 12 to 18 in e03df0f
The flag package already sets DefValue to the value's String(), so PreParse now only resets a value that differs from its default, e.g. on a second Run. A flag that rejects its own default, such as klog's -log_backtrace_at (:0), is not set on startup, so the Get() != nil check is gone. That check also panicked for a flag.Value without a Get method.
Lines 13 to 23 in 57e1bcb
I also dropped externalValue, RunAction, IsRequired and IsMultiValueFlag. TypeName returned "" for every flag, because flag values are pointer types, which have no name. It now uses flag.UnquoteUsage, and TakesValue is true unless it is a bool flag, so help shows --log_dir string like the flag package does. IsLocal makes the flags available in subcommands, as other flags are by default.
IsSet still returns false for these flags, which can be a follow-up. I also updated the description to match.
I rebased and rewrote the whole PR
|
I pushed a follow-up after another look. A Go flag is no longer added when a flag of the command already has its name. With glog, help listed Lines 82 to 88 in 1660a9d
Lines 62 to 64 in 1660a9d The new tests no longer print help, and I also updated the commit message and the release note: a bool Go flag is now turned off with |
With AllowExtFlags, Go flags that other packages add to flag.CommandLine did not work like flags defined with this package: - PreParse set every flag to its DefValue, which fails for a flag that does not accept its own default. klog's -log_backtrace_at prints ":0" but rejects it, so an app using klog failed to start, even for --help, with "syntax error: expect file.go:234". - A bool flag needed a value: --debug failed with "flag needs an argument". - Help showed them as blank lines, or left them out when another flag had a category. - Subcommands did not accept them. The flag package sets DefValue to the value's String(), so PreParse now only resets a value that has changed, e.g. in a previous run. extFlag now implements IsBoolFlag, DocGenerationFlag, CategorizableFlag and LocalFlag, and takes a value unless it is a bool flag, so help shows the same placeholder as the flag package. As in the flag package, a bool flag is turned off with --debug=false, not --debug false. A Go flag is no longer added when a flag of the command already has its name. That flag wins anyway, so help would list a flag that can't be set, such as glog's -v next to a user's --verbose, -v. Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
Make Go flags added with
AllowExtFlagswork like flags defined with urfave/cli:PreParseonly resets a value that differs from its default, so a flag that does not accept its own default, such as klog's-log_backtrace_at, no longer stops the app from starting.flagpackage, also when other flags have a category.What type of PR is this?
What this PR does / why we need it:
Solve:
Which issue(s) this PR fixes:
Fixes #2225
Testing
New tests in
flag_ext_test.gocover each change. Also tested with apps using klog v2.140.0 and glog v1.0.0, which fail to start on main.Release Notes