Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix:(issue_1272) Generic flag not set from env #1458

Merged
merged 1 commit into from Sep 1, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion flag_bool.go
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
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
18 changes: 11 additions & 7 deletions flag_test.go
Expand Up @@ -157,6 +157,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 Expand Up @@ -2398,43 +2402,43 @@ func TestFlagDefaultValue(t *testing.T) {
name: "stringSclice",
flag: &StringSliceFlag{Name: "flag", Value: NewStringSlice("default1", "default2")},
toParse: []string{"--flag", "parsed"},
expect: `--flag value [ --flag value ] (default: "default1", "default2")`,
expect: `--flag value [ --flag value ] (default: "default1", "default2")`,
},
{
name: "float64Sclice",
flag: &Float64SliceFlag{Name: "flag", Value: NewFloat64Slice(1.1, 2.2)},
toParse: []string{"--flag", "13.3"},
expect: `--flag value [ --flag value ] (default: 1.1, 2.2)`,
expect: `--flag value [ --flag value ] (default: 1.1, 2.2)`,
},
{
name: "int64Sclice",
flag: &Int64SliceFlag{Name: "flag", Value: NewInt64Slice(1, 2)},
toParse: []string{"--flag", "13"},
expect: `--flag value [ --flag value ] (default: 1, 2)`,
expect: `--flag value [ --flag value ] (default: 1, 2)`,
},
{
name: "intSclice",
flag: &IntSliceFlag{Name: "flag", Value: NewIntSlice(1, 2)},
toParse: []string{"--flag", "13"},
expect: `--flag value [ --flag value ] (default: 1, 2)`,
expect: `--flag value [ --flag value ] (default: 1, 2)`,
},
{
name: "string",
flag: &StringFlag{Name: "flag", Value: "default"},
toParse: []string{"--flag", "parsed"},
expect: `--flag value (default: "default")`,
expect: `--flag value (default: "default")`,
},
{
name: "bool",
flag: &BoolFlag{Name: "flag", Value: true},
toParse: []string{"--flag", "false"},
expect: `--flag (default: true)`,
expect: `--flag (default: true)`,
},
{
name: "uint64",
flag: &Uint64Flag{Name: "flag", Value: 1},
toParse: []string{"--flag", "13"},
expect: `--flag value (default: 1)`,
expect: `--flag value (default: 1)`,
},
}
for i, v := range cases {
Expand Down
28 changes: 14 additions & 14 deletions godoc-current.txt
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