Fix:(issue_1272) Generic flag not set from env

This commit is contained in:
Naveen Gogineni 2022-08-18 21:20:48 -04:00 committed by Dan Buch
parent 4bce542ed7
commit 68cd3e8148
Signed by: meatballhat
GPG Key ID: A12F782281063434
3 changed files with 11 additions and 2 deletions

View File

@ -31,8 +31,13 @@ func (f *BoolFlag) Apply(set *flag.FlagSet) error {
}
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() {

View File

@ -22,7 +22,7 @@ func (f *GenericFlag) GetValue() string {
// Apply takes the flagset and calls Set on the generic flag with the value
// 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 != "" {
if err := f.Value.Set(val); err != nil {

View File

@ -166,6 +166,10 @@ func TestFlagsFromEnv(t *testing.T) {
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]))
}
if !f.IsSet() {
t.Errorf("Flag %s not set", f.Names()[0])
}
return nil
},
}