Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4311,6 +4311,9 @@ func TestCommandReadArgsFromStdIn(t *testing.T) {
},
&StringSliceFlag{
Name: "ssf",
Config: StringConfig{
TrimSpace: true,
},
},
}

Expand Down
15 changes: 14 additions & 1 deletion flag_slice_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
6 changes: 6 additions & 0 deletions flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading