aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorshenwei356 <[email protected]>2016-10-07 00:41:20 +0800
committershenwei356 <[email protected]>2016-10-07 00:41:20 +0800
commit13e924deb5961431f4836dcb0e247254b9cc23d4 (patch)
tree2b9a66ea31c8d882d5783cad12f02a5bc061a278
parent4bd69631f4750a9df249ee317f4536e02cbc3c65 (diff)
fix bug of string_slice with square brackets
-rw-r--r--string_slice.go2
-rw-r--r--string_slice_test.go38
2 files changed, 39 insertions, 1 deletions
diff --git a/string_slice.go b/string_slice.go
index 51e3c5d..7829cfa 100644
--- a/string_slice.go
+++ b/string_slice.go
@@ -66,7 +66,7 @@ func (s *stringSliceValue) String() string {
}
func stringSliceConv(sval string) (interface{}, error) {
- sval = strings.Trim(sval, "[]")
+ sval = sval[1 : len(sval)-1]
// An empty string would cause a slice with one (empty) string
if len(sval) == 0 {
return []string{}, nil
diff --git a/string_slice_test.go b/string_slice_test.go
index 26118c7..c41f3bd 100644
--- a/string_slice_test.go
+++ b/string_slice_test.go
@@ -213,3 +213,41 @@ func TestSSWithComma(t *testing.T) {
}
}
}
+
+func TestSSWithSquareBrackets(t *testing.T) {
+ var ss []string
+ f := setUpSSFlagSet(&ss)
+
+ in := []string{`"[a-z]"`, `"[a-z]+"`}
+ expected := []string{"[a-z]", "[a-z]+"}
+ 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)
+ }
+
+ 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)
+ }
+ }
+}