diff options
| author | Eric Paris <[email protected]> | 2016-08-04 10:13:50 -0400 |
|---|---|---|
| committer | GitHub <[email protected]> | 2016-08-04 10:13:50 -0400 |
| commit | f676131e2660dc8cd88de99f7486d34aa8172635 (patch) | |
| tree | e84d571ec5016798320009235365d512ac0a7b3b | |
| parent | 1560c1005499d61b80f865c04d39ca7505bf7f0b (diff) | |
| parent | 1ceb032c43c28e6511309bcd3e3e40e3026f5294 (diff) | |
Merge pull request #84 from apelisse/fix-empty-string-slice
Fix string_slice with empty value
| -rw-r--r-- | string_slice.go | 11 | ||||
| -rw-r--r-- | string_slice_test.go | 17 |
2 files changed, 26 insertions, 2 deletions
diff --git a/string_slice.go b/string_slice.go index b53648b..f2a49d9 100644 --- a/string_slice.go +++ b/string_slice.go @@ -21,10 +21,17 @@ func newStringSliceValue(val []string, p *[]string) *stringSliceValue { return ssv } -func (s *stringSliceValue) Set(val string) error { +func readAsCSV(val string) ([]string, error) { + if val == "" { + return []string{}, nil + } stringReader := strings.NewReader(val) csvReader := csv.NewReader(stringReader) - v, err := csvReader.Read() + return csvReader.Read() +} + +func (s *stringSliceValue) Set(val string) error { + v, err := readAsCSV(val) if err != nil { return err } diff --git a/string_slice_test.go b/string_slice_test.go index c7fdc70..ed7cbfc 100644 --- a/string_slice_test.go +++ b/string_slice_test.go @@ -39,6 +39,23 @@ func TestEmptySS(t *testing.T) { } } +func TestEmptySSValue(t *testing.T) { + var ss []string + f := setUpSSFlagSet(&ss) + err := f.Parse([]string{"--ss="}) + if err != nil { + t.Fatal("expected no error; got", err) + } + + getSS, err := f.GetStringSlice("ss") + if err != nil { + t.Fatal("got an error from GetStringSlice():", err) + } + if len(getSS) != 0 { + t.Fatalf("got ss %v with len=%d but expected length=0", getSS, len(getSS)) + } +} + func TestSS(t *testing.T) { var ss []string f := setUpSSFlagSet(&ss) |
