From d68ef7c38c75152f9dac79bc635bd070b70d0913 Mon Sep 17 00:00:00 2001 From: wu8685 Date: Fri, 19 Aug 2016 10:41:12 +0800 Subject: Fix bug in FlagSet.GetStringSlice when a comma in a string slice value --- string_slice.go | 12 +++++++++--- string_slice_test.go | 43 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/string_slice.go b/string_slice.go index f2a49d9..927a440 100644 --- a/string_slice.go +++ b/string_slice.go @@ -1,6 +1,7 @@ package pflag import ( + "bytes" "encoding/csv" "fmt" "strings" @@ -48,7 +49,13 @@ func (s *stringSliceValue) Type() string { return "stringSlice" } -func (s *stringSliceValue) String() string { return "[" + strings.Join(*s.value, ",") + "]" } +func (s *stringSliceValue) String() string { + b := &bytes.Buffer{} + w := csv.NewWriter(b) + w.Write(*s.value) + w.Flush() + return "[" + strings.TrimSuffix(b.String(), fmt.Sprintln()) + "]" +} func stringSliceConv(sval string) (interface{}, error) { sval = strings.Trim(sval, "[]") @@ -56,8 +63,7 @@ func stringSliceConv(sval string) (interface{}, error) { if len(sval) == 0 { return []string{}, nil } - v := strings.Split(sval, ",") - return v, nil + return readAsCSV(sval) } // GetStringSlice return the []string value of a flag with the given name diff --git a/string_slice_test.go b/string_slice_test.go index ed7cbfc..26118c7 100644 --- a/string_slice_test.go +++ b/string_slice_test.go @@ -150,29 +150,66 @@ func TestSSCalledTwice(t *testing.T) { if err != nil { t.Fatal("expected no error; got", err) } + + if len(expected) != len(ss) { + t.Fatalf("expected number of ss to be %d but got: %d", len(expected), len(ss)) + } for i, v := range ss { if expected[i] != v { t.Fatalf("expected ss[%d] to be %s but got: %s", i, expected[i], v) } } + + values, err := f.GetStringSlice("ss") + if err != nil { + t.Fatal("expected no error; got", err) + } + + if len(expected) != len(values) { + t.Fatalf("expected number of values to be %d but got: %d", len(expected), len(ss)) + } + for i, v := range values { + if expected[i] != v { + t.Fatalf("expected got ss[%d] to be %s but got: %s", i, expected[i], v) + } + } } func TestSSWithComma(t *testing.T) { var ss []string f := setUpSSFlagSet(&ss) - in := []string{`"one,two"`, `"three"`} - expected := []string{"one,two", "three"} + in := []string{`"one,two"`, `"three"`, `"four,five",six`} + expected := []string{"one,two", "three", "four,five", "six"} argfmt := "--ss=%s" arg1 := fmt.Sprintf(argfmt, in[0]) arg2 := fmt.Sprintf(argfmt, in[1]) - err := f.Parse([]string{arg1, arg2}) + arg3 := fmt.Sprintf(argfmt, in[2]) + err := f.Parse([]string{arg1, arg2, arg3}) if err != nil { t.Fatal("expected no error; got", err) } + + if len(expected) != len(ss) { + t.Fatalf("expected number of ss to be %d but got: %d", len(expected), len(ss)) + } for i, v := range ss { if expected[i] != v { t.Fatalf("expected ss[%d] to be %s but got: %s", i, expected[i], v) } } + + values, err := f.GetStringSlice("ss") + if err != nil { + t.Fatal("expected no error; got", err) + } + + if len(expected) != len(values) { + t.Fatalf("expected number of values to be %d but got: %d", len(expected), len(values)) + } + for i, v := range values { + if expected[i] != v { + t.Fatalf("expected got ss[%d] to be %s but got: %s", i, expected[i], v) + } + } } -- cgit v1.2.3