diff --git a/flag_float64_slice.go b/flag_float64_slice.go index f752ad7534..385732e17c 100644 --- a/flag_float64_slice.go +++ b/flag_float64_slice.go @@ -144,6 +144,9 @@ func (f *Float64SliceFlag) Apply(set *flag.FlagSet) error { } } + // Set this to false so that we reset the slice if we then set values from + // flags that have already been set by the environment. + f.Value.hasBeenSet = false f.HasBeenSet = true } } diff --git a/flag_int64_slice.go b/flag_int64_slice.go index b4c8bc1496..f5c6939639 100644 --- a/flag_int64_slice.go +++ b/flag_int64_slice.go @@ -144,6 +144,9 @@ func (f *Int64SliceFlag) Apply(set *flag.FlagSet) error { } } + // Set this to false so that we reset the slice if we then set values from + // flags that have already been set by the environment. + f.Value.hasBeenSet = false f.HasBeenSet = true } diff --git a/flag_int_slice.go b/flag_int_slice.go index d4889b382c..94c668e9f1 100644 --- a/flag_int_slice.go +++ b/flag_int_slice.go @@ -155,6 +155,9 @@ func (f *IntSliceFlag) Apply(set *flag.FlagSet) error { } } + // Set this to false so that we reset the slice if we then set values from + // flags that have already been set by the environment. + f.Value.hasBeenSet = false f.HasBeenSet = true } diff --git a/flag_test.go b/flag_test.go index e46270d034..c563d6f346 100644 --- a/flag_test.go +++ b/flag_test.go @@ -52,15 +52,21 @@ func TestBoolFlagApply_SetsAllNames(t *testing.T) { } func TestFlagsFromEnv(t *testing.T) { + newSetFloat64Slice := func(defaults ...float64) Float64Slice { + s := NewFloat64Slice(defaults...) + s.hasBeenSet = false + return *s + } + newSetIntSlice := func(defaults ...int) IntSlice { s := NewIntSlice(defaults...) - s.hasBeenSet = true + s.hasBeenSet = false return *s } newSetInt64Slice := func(defaults ...int64) Int64Slice { s := NewInt64Slice(defaults...) - s.hasBeenSet = true + s.hasBeenSet = false return *s } @@ -96,6 +102,9 @@ func TestFlagsFromEnv(t *testing.T) { {"1.2", 0, &IntFlag{Name: "seconds", EnvVars: []string{"SECONDS"}}, `could not parse "1.2" as int value for flag seconds: .*`}, {"foobar", 0, &IntFlag{Name: "seconds", EnvVars: []string{"SECONDS"}}, `could not parse "foobar" as int value for flag seconds: .*`}, + {"1.0,2", newSetFloat64Slice(1, 2), &Float64SliceFlag{Name: "seconds", EnvVars: []string{"SECONDS"}}, ""}, + {"foobar", newSetFloat64Slice(), &Float64SliceFlag{Name: "seconds", EnvVars: []string{"SECONDS"}}, `could not parse "\[\]float64{}" as float64 slice value for flag seconds: .*`}, + {"1,2", newSetIntSlice(1, 2), &IntSliceFlag{Name: "seconds", EnvVars: []string{"SECONDS"}}, ""}, {"1.2,2", newSetIntSlice(), &IntSliceFlag{Name: "seconds", EnvVars: []string{"SECONDS"}}, `could not parse "1.2,2" as int slice value for flag seconds: .*`}, {"foobar", newSetIntSlice(), &IntSliceFlag{Name: "seconds", EnvVars: []string{"SECONDS"}}, `could not parse "foobar" as int slice value for flag seconds: .*`},