diff --git a/command_test.go b/command_test.go index 6df83cdb75..6a2c0998f2 100644 --- a/command_test.go +++ b/command_test.go @@ -4311,6 +4311,9 @@ func TestCommandReadArgsFromStdIn(t *testing.T) { }, &StringSliceFlag{ Name: "ssf", + Config: StringConfig{ + TrimSpace: true, + }, }, } diff --git a/flag_slice_base.go b/flag_slice_base.go index b97c4ff4f2..3e7b049ea7 100644 --- a/flag_slice_base.go +++ b/flag_slice_base.go @@ -47,8 +47,21 @@ func (i *SliceBase[T, C, VC]) Set(value string) error { return nil } + trimSpace := true + // hack. How do we know if we should trim spaces? + // it makes sense only for string slice flags which have + // an option to not trim spaces. So by default we trim spaces + // otherwise we let the underlying value type handle it. + var t T + if reflect.TypeOf(t).Kind() == reflect.String { + trimSpace = false + } + for _, s := range flagSplitMultiValues(value) { - if err := i.value.Set(strings.TrimSpace(s)); err != nil { + if trimSpace { + s = strings.TrimSpace(s) + } + if err := i.value.Set(s); err != nil { return err } *i.slice = append(*i.slice, i.value.Get().(T)) diff --git a/flag_test.go b/flag_test.go index f6b0578334..24c7411b43 100644 --- a/flag_test.go +++ b/flag_test.go @@ -350,6 +350,12 @@ func TestFlagsFromEnv(t *testing.T) { output: []string{"foo", "bar"}, fl: &StringSliceFlag{Name: "names", Sources: EnvVars("NAMES"), Config: StringConfig{TrimSpace: true}}, }, + { + name: "StringSliceFlag valid without TrimSpace", + input: "foo , bar ", + output: []string{"foo ", " bar "}, + fl: &StringSliceFlag{Name: "names", Sources: EnvVars("NAMES")}, + }, { name: "StringMapFlag valid",