@@ -144,6 +144,11 @@ func TestFlagsFromEnv(t *testing.T) {
144
144
s .hasBeenSet = false
145
145
return * s
146
146
}
147
+ newSetStringSliceKeepSpace := func (defaults ... string ) StringSlice {
148
+ s := newSetStringSlice (defaults ... )
149
+ s .keepSpace = true
150
+ return s
151
+ }
147
152
148
153
var flagTests = []struct {
149
154
input string
@@ -198,6 +203,8 @@ func TestFlagsFromEnv(t *testing.T) {
198
203
{"path" , "path" , & PathFlag {Name : "path" , EnvVars : []string {"PATH" }}, "" },
199
204
200
205
{"foo,bar" , newSetStringSlice ("foo" , "bar" ), & StringSliceFlag {Name : "names" , EnvVars : []string {"NAMES" }}, "" },
206
+ {" space " , newSetStringSliceKeepSpace (" space " ), & StringSliceFlag {Name : "names" , KeepSpace : true , EnvVars : []string {"NAMES" }}, "" },
207
+ {" no space " , newSetStringSlice ("no space" ), & StringSliceFlag {Name : "names" , EnvVars : []string {"NAMES" }}, "" },
201
208
202
209
{"1" , uint (1 ), & UintFlag {Name : "seconds" , EnvVars : []string {"SECONDS" }}, "" },
203
210
{"08" , uint (8 ), & UintFlag {Name : "seconds" , EnvVars : []string {"SECONDS" }, Base : 10 }, "" },
@@ -774,7 +781,7 @@ func TestStringSliceFlag_MatchStringFlagBehavior(t *testing.T) {
774
781
app := App {
775
782
Flags : []Flag {
776
783
& StringFlag {Name : "string" },
777
- & StringSliceFlag {Name : "slice" },
784
+ & StringSliceFlag {Name : "slice" , KeepSpace : true },
778
785
},
779
786
Action : func (ctx * Context ) error {
780
787
f1 , f2 := ctx .String ("string" ), ctx .StringSlice ("slice" )
@@ -797,6 +804,52 @@ func TestStringSliceFlag_MatchStringFlagBehavior(t *testing.T) {
797
804
}
798
805
}
799
806
807
+ func TestStringSliceFlag_TrimSpace (t * testing.T ) {
808
+ t .Parallel ()
809
+
810
+ tests := []struct {
811
+ in , out string
812
+ }{
813
+ {" asd" , "asd" },
814
+ {"123 " , "123" },
815
+ {" asd " , "asd" },
816
+ }
817
+ for testNum , tt := range tests {
818
+ tt := tt
819
+ t .Run (fmt .Sprintf ("%d" , testNum ), func (t * testing.T ) {
820
+ t .Parallel ()
821
+
822
+ app := App {
823
+ Flags : []Flag {
824
+ & StringSliceFlag {Name : "trim" },
825
+ & StringSliceFlag {Name : "no-trim" , KeepSpace : true },
826
+ },
827
+ Action : func (ctx * Context ) error {
828
+ flagTrim , flagNoTrim := ctx .StringSlice ("trim" ), ctx .StringSlice ("no-trim" )
829
+ if l := len (flagTrim ); l != 1 {
830
+ t .Fatalf ("slice flag 'trim' should result in exactly one value, got %d" , l )
831
+ }
832
+ if l := len (flagNoTrim ); l != 1 {
833
+ t .Fatalf ("slice flag 'no-trim' should result in exactly one value, got %d" , l )
834
+ }
835
+
836
+ if v := flagTrim [0 ]; v != tt .out {
837
+ t .Errorf ("Expected trimmed value %q, got %q" , tt .out , v )
838
+ }
839
+ if v := flagNoTrim [0 ]; v != tt .in {
840
+ t .Errorf ("Expected no trimmed value%q, got %q" , tt .out , v )
841
+ }
842
+ return nil
843
+ },
844
+ }
845
+
846
+ if err := app .Run ([]string {"" , "--trim" , tt .in , "--no-trim" , tt .in }); err != nil {
847
+ t .Errorf ("app run error: %s" , err )
848
+ }
849
+ })
850
+ }
851
+ }
852
+
800
853
var intFlagTests = []struct {
801
854
name string
802
855
expected string
0 commit comments