Fix:(issue_1272) Generic flag not set from env (#1458)

main
dearchap 2 years ago committed by GitHub
parent ca9df40abd
commit f451dead12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -51,8 +51,13 @@ func (f *BoolFlag) Apply(set *flag.FlagSet) error {
} }
f.Value = valBool f.Value = valBool
f.HasBeenSet = true } else {
// empty value implies that the env is defined but set to empty string, we have to assume that this is
// what the user wants. If user doesnt want this then the env needs to be deleted or the flag removed from
// file
f.Value = false
} }
f.HasBeenSet = true
} }
for _, name := range f.Names() { for _, name := range f.Names() {

@ -50,7 +50,7 @@ func (f *GenericFlag) GetEnvVars() []string {
// Apply takes the flagset and calls Set on the generic flag with the value // Apply takes the flagset and calls Set on the generic flag with the value
// provided by the user for parsing by the flag // provided by the user for parsing by the flag
func (f GenericFlag) Apply(set *flag.FlagSet) error { func (f *GenericFlag) Apply(set *flag.FlagSet) error {
if val, source, found := flagFromEnvOrFile(f.EnvVars, f.FilePath); found { if val, source, found := flagFromEnvOrFile(f.EnvVars, f.FilePath); found {
if val != "" { if val != "" {
if err := f.Value.Set(val); err != nil { if err := f.Value.Set(val); err != nil {

@ -169,6 +169,10 @@ func TestFlagsFromEnv(t *testing.T) {
if !reflect.DeepEqual(ctx.Value(test.flag.Names()[0]), test.output) { if !reflect.DeepEqual(ctx.Value(test.flag.Names()[0]), test.output) {
t.Errorf("ex:%01d expected %q to be parsed as %#v, instead was %#v", i, test.input, test.output, ctx.Value(test.flag.Names()[0])) t.Errorf("ex:%01d expected %q to be parsed as %#v, instead was %#v", i, test.input, test.output, ctx.Value(test.flag.Names()[0]))
} }
if !f.IsSet() {
t.Errorf("Flag %s not set", f.Names()[0])
}
return nil return nil
}, },
} }

@ -1073,7 +1073,7 @@ type GenericFlag struct {
} }
GenericFlag is a flag with type Generic GenericFlag is a flag with type Generic
func (f GenericFlag) Apply(set *flag.FlagSet) error func (f *GenericFlag) Apply(set *flag.FlagSet) error
Apply takes the flagset and calls Set on the generic flag with the value Apply takes the flagset and calls Set on the generic flag with the value
provided by the user for parsing by the flag provided by the user for parsing by the flag

Loading…
Cancel
Save