aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Paris <[email protected]>2015-10-13 15:06:43 -0500
committerEric Paris <[email protected]>2015-10-13 15:06:43 -0500
commit08b1a584251b5b62f458943640fc8ebd4d50aaa5 (patch)
tree8c48c3816e55ca33e3e3381614292238afb70df1
parentb084184666e02084b8ccb9b704bf0d79c466eb1d (diff)
parent4e5c959ab1fe29f3e09f1177c69e6c5af3e58112 (diff)
Merge pull request #59 from eparis/csv-escaping
Use encoding/csv to parse string slices
-rw-r--r--.travis.yml1
-rw-r--r--string_slice.go8
-rw-r--r--string_slice_test.go20
3 files changed, 28 insertions, 1 deletions
diff --git a/.travis.yml b/.travis.yml
index c40fb69..df435a0 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -5,6 +5,7 @@ language: go
go:
- 1.3
- 1.4
+ - 1.5
- tip
install:
diff --git a/string_slice.go b/string_slice.go
index 0b7ddfe..b53648b 100644
--- a/string_slice.go
+++ b/string_slice.go
@@ -1,6 +1,7 @@
package pflag
import (
+ "encoding/csv"
"fmt"
"strings"
)
@@ -21,7 +22,12 @@ func newStringSliceValue(val []string, p *[]string) *stringSliceValue {
}
func (s *stringSliceValue) Set(val string) error {
- v := strings.Split(val, ",")
+ stringReader := strings.NewReader(val)
+ csvReader := csv.NewReader(stringReader)
+ v, err := csvReader.Read()
+ if err != nil {
+ return err
+ }
if !s.changed {
*s.value = v
} else {
diff --git a/string_slice_test.go b/string_slice_test.go
index 97602de..c7fdc70 100644
--- a/string_slice_test.go
+++ b/string_slice_test.go
@@ -139,3 +139,23 @@ func TestSSCalledTwice(t *testing.T) {
}
}
}
+
+func TestSSWithComma(t *testing.T) {
+ var ss []string
+ f := setUpSSFlagSet(&ss)
+
+ in := []string{`"one,two"`, `"three"`}
+ expected := []string{"one,two", "three"}
+ argfmt := "--ss=%s"
+ arg1 := fmt.Sprintf(argfmt, in[0])
+ arg2 := fmt.Sprintf(argfmt, in[1])
+ err := f.Parse([]string{arg1, arg2})
+ if err != nil {
+ t.Fatal("expected no error; got", err)
+ }
+ for i, v := range ss {
+ if expected[i] != v {
+ t.Fatalf("expected ss[%d] to be %s but got: %s", i, expected[i], v)
+ }
+ }
+}