Skip to content

Commit 34a45f1

Browse files
leno23cursoragent
andcommitted
fix: treat lone empty StringArray flag as empty slice
Passing --flag="" no longer produces a one-element slice containing an empty string when the default value is empty. Fixes #415 Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 00f25b9 commit 34a45f1

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

string_array.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ func newStringArrayValue(val []string, p *[]string) *stringArrayValue {
1515

1616
func (s *stringArrayValue) Set(val string) error {
1717
if !s.changed {
18-
*s.value = []string{val}
18+
if val == "" {
19+
*s.value = []string{}
20+
} else {
21+
*s.value = []string{val}
22+
}
1923
s.changed = true
2024
} else {
2125
*s.value = append(*s.value, val)

string_array_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,19 @@ func TestSAWithSpecialChar(t *testing.T) {
193193
}
194194
}
195195

196+
func TestSAEmptyStringFlagValue(t *testing.T) {
197+
var sa []string
198+
f := setUpSAFlagSet(&sa)
199+
200+
err := f.Parse([]string{"--sa", ""})
201+
if err != nil {
202+
t.Fatal("expected no error; got", err)
203+
}
204+
if len(sa) != 0 {
205+
t.Fatalf("expected empty array for --sa=\"\", got len=%d %v", len(sa), sa)
206+
}
207+
}
208+
196209
func TestSAAsSliceValue(t *testing.T) {
197210
var sa []string
198211
f := setUpSAFlagSet(&sa)

0 commit comments

Comments
 (0)