Skip to content

Commit 9d76d15

Browse files
authored
Merge pull request #2070 from dearchap/issue_2069
Fix:(issue_2069) Add sep for string slice
2 parents 3b17080 + 4abc9c3 commit 9d76d15

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

flag_string_slice.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ func (f *StringSliceFlag) Apply(set *flag.FlagSet) error {
150150
setValue = f.Value.clone()
151151
default:
152152
setValue = new(StringSlice)
153-
setValue.WithSeparatorSpec(f.separator)
154153
}
154+
setValue.WithSeparatorSpec(f.separator)
155155

156156
setValue.keepSpace = f.KeepSpace
157157

flag_test.go

+60
Original file line numberDiff line numberDiff line change
@@ -2165,6 +2165,66 @@ func TestParseMultiStringSliceWithDestination(t *testing.T) {
21652165
}).Run([]string{"run", "-s", "10", "-s", "20"})
21662166
}
21672167

2168+
func TestParseMultiStringSliceDisableSeparator(t *testing.T) {
2169+
tests := []struct {
2170+
name string
2171+
val *StringSlice
2172+
dest *StringSlice
2173+
disableSep bool
2174+
input []string
2175+
expected []string
2176+
}{
2177+
{
2178+
name: "Basic",
2179+
input: []string{"-s", "10", "-s", "20"},
2180+
expected: []string{"10", "20"},
2181+
},
2182+
{
2183+
name: "Basic Sep",
2184+
input: []string{"-s", "10,20"},
2185+
expected: []string{"10", "20"},
2186+
},
2187+
{
2188+
name: "Basic Disable Sep",
2189+
disableSep: true,
2190+
input: []string{"-s", "10,20"},
2191+
expected: []string{"10,20"},
2192+
},
2193+
{
2194+
name: "Basic Disable Sep dest set",
2195+
disableSep: true,
2196+
dest: NewStringSlice("11", "22"),
2197+
input: []string{"-s", "10,20"},
2198+
expected: []string{"10,20"},
2199+
},
2200+
{
2201+
name: "Basic Disable Sep value set",
2202+
disableSep: true,
2203+
val: NewStringSlice("11", "22"),
2204+
input: []string{"-s", "10,201"},
2205+
expected: []string{"10,201"},
2206+
},
2207+
}
2208+
for _, test := range tests {
2209+
t.Run(test.name, func(t *testing.T) {
2210+
inputs := []string{"run"}
2211+
inputs = append(inputs, test.input...)
2212+
_ = (&App{
2213+
DisableSliceFlagSeparator: test.disableSep,
2214+
Flags: []Flag{
2215+
&StringSliceFlag{Name: "serve", Aliases: []string{"s"}, Destination: test.dest, Value: test.val},
2216+
},
2217+
Action: func(ctx *Context) error {
2218+
if !reflect.DeepEqual(ctx.StringSlice("serve"), test.expected) {
2219+
t.Errorf("Expected: %v != %v", test.expected, ctx.StringSlice("serve"))
2220+
}
2221+
return nil
2222+
},
2223+
}).Run(inputs)
2224+
})
2225+
}
2226+
}
2227+
21682228
func TestParseMultiStringSliceWithDestinationAndEnv(t *testing.T) {
21692229
defer resetEnv(os.Environ())
21702230
os.Clearenv()

0 commit comments

Comments
 (0)