Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions flag_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,14 @@ func (f *FlagBase[T, C, V]) PostParse() error {
continue
}

if val != "" || kind == reflect.String {
if err := f.Set(f.Name, val); err != nil {
return fmt.Errorf(
"could not parse %[1]q as %[2]T value from %[3]s for flag %[4]s: %[5]s",
val, f.Value, source, f.Name, err,
)
}
} else {
_ = f.Set(f.Name, "false")
if val == "" && kind == reflect.Bool {
val = "false"
}
if err := f.Set(f.Name, val); err != nil {
return fmt.Errorf(
"could not parse %[1]q as %[2]T value from %[3]s for flag %[4]s: %[5]s",
val, f.Value, source, f.Name, err,
)
}

f.hasBeenSet = true
Expand Down
11 changes: 11 additions & 0 deletions flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,17 @@ func TestFlagsFromEmptyEnv(t *testing.T) {
wantValue: false,
wantIsSet: true,
},
{
name: "an empty bool still runs the validator",
env: map[string]string{"DEBUG": ""},
fl: &BoolFlag{Name: "debug", Value: true, Sources: EnvVars("DEBUG"), Validator: func(b bool) error {
if !b {
return errors.New("debug must be true")
}
return nil
}},
errContains: "debug must be true",
},
}

for _, tc := range testCases {
Expand Down
Loading