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
}, },
} }

@ -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: the most simple cli application can be written as follows:
func main() { 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 Of course this application does not do much, so let's make this an actual
application: application:
func main() { func main() {
app := &cli.App{ app := &cli.App{
Name: "greet", Name: "greet",
Usage: "say a greeting", Usage: "say a greeting",
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
fmt.Println("Greetings") fmt.Println("Greetings")
return nil return nil
}, },
} }
app.Run(os.Args) app.Run(os.Args)
} }
VARIABLES VARIABLES
@ -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