File tree 4 files changed +48
-1
lines changed
4 files changed +48
-1
lines changed Original file line number Diff line number Diff line change @@ -293,11 +293,19 @@ func (i *Invocation) run(state *runState) error {
293
293
return
294
294
}
295
295
296
+ // If flag was changed, we need to remove the default values.
296
297
sv , ok := f .Value .(pflag.SliceValue )
297
298
if ! ok {
298
299
panic ("defaulted array option is not a slice value" )
299
300
}
300
- err := sv .Replace (sv .GetSlice ()[i :])
301
+ ss := sv .GetSlice ()
302
+ if len (ss ) == 0 {
303
+ // Slice likely zeroed by a flag.
304
+ // E.g. "--fruit" may default to "apples,oranges" but the user
305
+ // provided "--fruit=""".
306
+ return
307
+ }
308
+ err := sv .Replace (ss [i :])
301
309
if err != nil {
302
310
panic (err )
303
311
}
Original file line number Diff line number Diff line change @@ -503,3 +503,35 @@ func TestCommand_SliceFlags(t *testing.T) {
503
503
err = cmd ("bad" , "bad" , "bad" ).Invoke ().Run ()
504
504
require .NoError (t , err )
505
505
}
506
+
507
+ func TestCommand_EmptySlice (t * testing.T ) {
508
+ t .Parallel ()
509
+
510
+ cmd := func (want ... string ) * clibase.Cmd {
511
+ var got []string
512
+ return & clibase.Cmd {
513
+ Use : "root" ,
514
+ Options : clibase.OptionSet {
515
+ {
516
+ Name : "arr" ,
517
+ Flag : "arr" ,
518
+ Default : "bad,bad,bad" ,
519
+ Env : "ARR" ,
520
+ Value : clibase .StringArrayOf (& got ),
521
+ },
522
+ },
523
+ Handler : (func (i * clibase.Invocation ) error {
524
+ require .Equal (t , want , got )
525
+ return nil
526
+ }),
527
+ }
528
+ }
529
+
530
+ // Base-case
531
+ err := cmd ("bad" , "bad" , "bad" ).Invoke ().Run ()
532
+ require .NoError (t , err )
533
+
534
+ inv := cmd ().Invoke ("--arr" , "" )
535
+ err = inv .Run ()
536
+ require .NoError (t , err )
537
+ }
Original file line number Diff line number Diff line change @@ -126,6 +126,9 @@ func (s *OptionSet) ParseEnv(vs []EnvVar) error {
126
126
// way for a user to change a Default value to an empty string from
127
127
// the environment. Unfortunately, we have old configuration files
128
128
// that rely on the faulty behavior.
129
+ //
130
+ // TODO: We should remove this hack in May 2023, when deployments
131
+ // have had months to migrate to the new behavior.
129
132
if ! ok || envVal == "" {
130
133
continue
131
134
}
Original file line number Diff line number Diff line change @@ -146,6 +146,10 @@ func writeAsCSV(vals []string) string {
146
146
}
147
147
148
148
func (s * StringArray ) Set (v string ) error {
149
+ if v == "" {
150
+ * s = nil
151
+ return nil
152
+ }
149
153
ss , err := readAsCSV (v )
150
154
if err != nil {
151
155
return err
You can’t perform that action at this time.
0 commit comments