From 4abc9c3642f013bf65d3a2799fe32c902b5bf8a4 Mon Sep 17 00:00:00 2001 From: Naveen Gogineni Date: Tue, 4 Mar 2025 20:25:46 -0500 Subject: [PATCH] Fix:(issue_2069) Add sep for string slice --- flag_string_slice.go | 2 +- flag_test.go | 60 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/flag_string_slice.go b/flag_string_slice.go index 28f4798f55..66bdf1afcd 100644 --- a/flag_string_slice.go +++ b/flag_string_slice.go @@ -150,8 +150,8 @@ func (f *StringSliceFlag) Apply(set *flag.FlagSet) error { setValue = f.Value.clone() default: setValue = new(StringSlice) - setValue.WithSeparatorSpec(f.separator) } + setValue.WithSeparatorSpec(f.separator) setValue.keepSpace = f.KeepSpace diff --git a/flag_test.go b/flag_test.go index b07e5aaf2c..2e130a5856 100644 --- a/flag_test.go +++ b/flag_test.go @@ -2165,6 +2165,66 @@ func TestParseMultiStringSliceWithDestination(t *testing.T) { }).Run([]string{"run", "-s", "10", "-s", "20"}) } +func TestParseMultiStringSliceDisableSeparator(t *testing.T) { + tests := []struct { + name string + val *StringSlice + dest *StringSlice + disableSep bool + input []string + expected []string + }{ + { + name: "Basic", + input: []string{"-s", "10", "-s", "20"}, + expected: []string{"10", "20"}, + }, + { + name: "Basic Sep", + input: []string{"-s", "10,20"}, + expected: []string{"10", "20"}, + }, + { + name: "Basic Disable Sep", + disableSep: true, + input: []string{"-s", "10,20"}, + expected: []string{"10,20"}, + }, + { + name: "Basic Disable Sep dest set", + disableSep: true, + dest: NewStringSlice("11", "22"), + input: []string{"-s", "10,20"}, + expected: []string{"10,20"}, + }, + { + name: "Basic Disable Sep value set", + disableSep: true, + val: NewStringSlice("11", "22"), + input: []string{"-s", "10,201"}, + expected: []string{"10,201"}, + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + inputs := []string{"run"} + inputs = append(inputs, test.input...) + _ = (&App{ + DisableSliceFlagSeparator: test.disableSep, + Flags: []Flag{ + &StringSliceFlag{Name: "serve", Aliases: []string{"s"}, Destination: test.dest, Value: test.val}, + }, + Action: func(ctx *Context) error { + if !reflect.DeepEqual(ctx.StringSlice("serve"), test.expected) { + t.Errorf("Expected: %v != %v", test.expected, ctx.StringSlice("serve")) + } + return nil + }, + }).Run(inputs) + }) + } +} + func TestParseMultiStringSliceWithDestinationAndEnv(t *testing.T) { defer resetEnv(os.Environ()) os.Clearenv()