Skip to content

Commit f451dea

Browse files
authored
Fix:(issue_1272) Generic flag not set from env (#1458)
1 parent ca9df40 commit f451dea

File tree

4 files changed

+25
-16
lines changed

4 files changed

+25
-16
lines changed

flag_bool.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,13 @@ func (f *BoolFlag) Apply(set *flag.FlagSet) error {
5151
}
5252

5353
f.Value = valBool
54-
f.HasBeenSet = true
54+
} else {
55+
// empty value implies that the env is defined but set to empty string, we have to assume that this is
56+
// what the user wants. If user doesnt want this then the env needs to be deleted or the flag removed from
57+
// file
58+
f.Value = false
5559
}
60+
f.HasBeenSet = true
5661
}
5762

5863
for _, name := range f.Names() {

flag_generic.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (f *GenericFlag) GetEnvVars() []string {
5050

5151
// Apply takes the flagset and calls Set on the generic flag with the value
5252
// provided by the user for parsing by the flag
53-
func (f GenericFlag) Apply(set *flag.FlagSet) error {
53+
func (f *GenericFlag) Apply(set *flag.FlagSet) error {
5454
if val, source, found := flagFromEnvOrFile(f.EnvVars, f.FilePath); found {
5555
if val != "" {
5656
if err := f.Value.Set(val); err != nil {

flag_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,10 @@ func TestFlagsFromEnv(t *testing.T) {
169169
if !reflect.DeepEqual(ctx.Value(test.flag.Names()[0]), test.output) {
170170
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]))
171171
}
172+
if !f.IsSet() {
173+
t.Errorf("Flag %s not set", f.Names()[0])
174+
}
175+
172176
return nil
173177
},
174178
}

godoc-current.txt

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,24 @@ line Go applications. cli is designed to be easy to understand and write,
55
the most simple cli application can be written as follows:
66

77
func main() {
8-
(&cli.App{}).Run(os.Args)
8+
(&cli.App{}).Run(os.Args)
99
}
1010

1111
Of course this application does not do much, so let's make this an actual
1212
application:
1313

14-
func main() {
15-
app := &cli.App{
16-
Name: "greet",
17-
Usage: "say a greeting",
18-
Action: func(c *cli.Context) error {
19-
fmt.Println("Greetings")
20-
return nil
21-
},
22-
}
23-
24-
app.Run(os.Args)
25-
}
14+
func main() {
15+
app := &cli.App{
16+
Name: "greet",
17+
Usage: "say a greeting",
18+
Action: func(c *cli.Context) error {
19+
fmt.Println("Greetings")
20+
return nil
21+
},
22+
}
23+
24+
app.Run(os.Args)
25+
}
2626

2727
VARIABLES
2828

@@ -1073,7 +1073,7 @@ type GenericFlag struct {
10731073
}
10741074
GenericFlag is a flag with type Generic
10751075

1076-
func (f GenericFlag) Apply(set *flag.FlagSet) error
1076+
func (f *GenericFlag) Apply(set *flag.FlagSet) error
10771077
Apply takes the flagset and calls Set on the generic flag with the value
10781078
provided by the user for parsing by the flag
10791079

0 commit comments

Comments
 (0)