We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see ourdocumentation.
There was an error while loading.Please reload this page.
1 parent773580c commit1645281Copy full SHA for 1645281
cli/clibase/cmd.go
@@ -293,11 +293,19 @@ func (i *Invocation) run(state *runState) error {
293
return
294
}
295
296
+// If flag was changed, we need to remove the default values.
297
sv,ok:=f.Value.(pflag.SliceValue)
298
if!ok {
299
panic("defaulted array option is not a slice value")
300
-err:=sv.Replace(sv.GetSlice()[i:])
301
+ss:=sv.GetSlice()
302
+iflen(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:])
309
iferr!=nil {
310
panic(err)
311
cli/clibase/cmd_test.go
@@ -503,3 +503,35 @@ func TestCommand_SliceFlags(t *testing.T) {
503
err=cmd("bad","bad","bad").Invoke().Run()
504
require.NoError(t,err)
505
506
+
507
+funcTestCommand_EmptySlice(t*testing.T) {
508
+t.Parallel()
509
510
+cmd:=func(want...string)*clibase.Cmd {
511
+vargot []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
+returnnil
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
537
cli/clibase/option.go
@@ -126,6 +126,9 @@ func (s *OptionSet) ParseEnv(vs []EnvVar) error {
126
// way for a user to change a Default value to an empty string from
127
// the environment. Unfortunately, we have old configuration files
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.
132
if!ok||envVal=="" {
133
continue
134
cli/clibase/values.go
@@ -146,6 +146,10 @@ func writeAsCSV(vals []string) string {
146
147
148
func (s*StringArray)Set(vstring)error {
149
+ifv=="" {
150
+*s=nil
151
152
153
ss,err:=readAsCSV(v)
154
155
returnerr