Skip to content

Commit

Permalink
Merge pull request #1649 from palsivertsen/issue-1648
Browse files Browse the repository at this point in the history
Fixes #1648
  • Loading branch information
dearchap authored Jan 17, 2023
2 parents c891d79 + 6bb383c commit fc28a1c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion flag_string_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (s *StringSlice) Set(value string) error {
}

for _, t := range flagSplitMultiValues(value) {
s.slice = append(s.slice, strings.TrimSpace(t))
s.slice = append(s.slice, t)
}

return nil
Expand Down
39 changes: 39 additions & 0 deletions flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,45 @@ func TestStringSliceFlagValueFromContext(t *testing.T) {
expect(t, f.Get(ctx), []string{"a", "b", "c"})
}

func TestStringSliceFlag_MatchStringFlagBehavior(t *testing.T) {
t.Parallel()

values := []string{
"asd",
"123",
" asd ", // leading and tailing space
}
for testNum, value := range values {
value := value
t.Run(fmt.Sprintf("%d", testNum), func(t *testing.T) {
t.Parallel()

app := App{
Flags: []Flag{
&StringFlag{Name: "string"},
&StringSliceFlag{Name: "slice"},
},
Action: func(ctx *Context) error {
f1, f2 := ctx.String("string"), ctx.StringSlice("slice")
if l := len(f2); l != 1 {
t.Fatalf("StringSliceFlag should result in exactly one value, got %d", l)
}

v1, v2 := f1, f2[0]
if v1 != v2 {
t.Errorf("Expected StringSliceFlag to parse the same value as StringFlag (%q), got %q", v1, v2)
}
return nil
},
}

if err := app.Run([]string{"", "--string", value, "--slice", value}); err != nil {
t.Errorf("app run error: %s", err)
}
})
}
}

var intFlagTests = []struct {
name string
expected string
Expand Down

0 comments on commit fc28a1c

Please sign in to comment.