Skip to content

Commit

Permalink
Fix:(issue_1272) Generic flag not set from env (#1458)
Browse files Browse the repository at this point in the history
  • Loading branch information
dearchap authored Sep 1, 2022
1 parent ca9df40 commit f451dea
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
7 changes: 6 additions & 1 deletion flag_bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,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() {
Expand Down
2 changes: 1 addition & 1 deletion flag_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (f *GenericFlag) GetEnvVars() []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 {
Expand Down
4 changes: 4 additions & 0 deletions flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,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
},
}
Expand Down
28 changes: 14 additions & 14 deletions godoc-current.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ line Go applications. cli is designed to be easy to understand and write,
the most simple cli application can be written as follows:

func main() {
(&cli.App{}).Run(os.Args)
(&cli.App{}).Run(os.Args)
}

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

func main() {
app := &cli.App{
Name: "greet",
Usage: "say a greeting",
Action: func(c *cli.Context) error {
fmt.Println("Greetings")
return nil
},
}

app.Run(os.Args)
}
func main() {
app := &cli.App{
Name: "greet",
Usage: "say a greeting",
Action: func(c *cli.Context) error {
fmt.Println("Greetings")
return nil
},
}

app.Run(os.Args)
}

VARIABLES

Expand Down Expand Up @@ -1073,7 +1073,7 @@ type GenericFlag struct {
}
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
provided by the user for parsing by the flag

Expand Down

0 comments on commit f451dea

Please sign in to comment.