diff options
| author | Eric Paris <[email protected]> | 2015-08-05 12:00:39 -0500 |
|---|---|---|
| committer | Eric Paris <[email protected]> | 2015-08-05 12:00:39 -0500 |
| commit | 08f04032975dbfebf7a5946d581eae0724af18f4 (patch) | |
| tree | 6655dd93a25fe82dfe6362ba8c87f874158834f4 | |
| parent | af83f852cb3533df209ffef0b0f36b65d4594848 (diff) | |
| parent | 90b831e61ee0ea159e0d00bb0c1ee3cd0c1dcdcd (diff) | |
Merge pull request #35 from eparis/slice-multi-call
String and Int slices called twice should append not overwrite
| -rw-r--r-- | int_slice.go | 5 | ||||
| -rw-r--r-- | int_slice_test.go | 20 | ||||
| -rw-r--r-- | string_slice.go | 2 | ||||
| -rw-r--r-- | string_slice_test.go | 26 |
4 files changed, 47 insertions, 6 deletions
diff --git a/int_slice.go b/int_slice.go index fb3e67b..69e7e7c 100644 --- a/int_slice.go +++ b/int_slice.go @@ -25,7 +25,7 @@ func (s *intSliceValue) Set(val string) error { } } - *s = intSliceValue(out) + *s = append(*s, out...) return nil } @@ -38,10 +38,11 @@ func (s *intSliceValue) String() string { for i, d := range *s { out[i] = fmt.Sprintf("%d", d) } - return strings.Join(out, ",") + return "[" + strings.Join(out, ",") + "]" } func intSliceConv(val string) (interface{}, error) { + val = strings.Trim(val, "[]") ss := strings.Split(val, ",") out := make([]int, len(ss)) for i, d := range ss { diff --git a/int_slice_test.go b/int_slice_test.go index d162e86..927e7f4 100644 --- a/int_slice_test.go +++ b/int_slice_test.go @@ -47,3 +47,23 @@ func TestIS(t *testing.T) { } } } + +func TestISCalledTwice(t *testing.T) { + var is []int + f := setUpISFlagSet(&is) + + in := []string{"1,2", "3"} + expected := []int{1, 2, 3} + argfmt := "--is=%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 is { + if expected[i] != v { + t.Fatalf("expected ss[%d] to be %s but got: %s", i, expected[i], v) + } + } +} diff --git a/string_slice.go b/string_slice.go index b7096e8..20d94ba 100644 --- a/string_slice.go +++ b/string_slice.go @@ -14,7 +14,7 @@ func newStringSliceValue(val []string, p *[]string) *stringSliceValue { func (s *stringSliceValue) Set(val string) error { v := strings.Split(val, ",") - *s = stringSliceValue(v) + *s = append(*s, v...) return nil } func (s *stringSliceValue) Type() string { diff --git a/string_slice_test.go b/string_slice_test.go index c37a91a..d5c0d03 100644 --- a/string_slice_test.go +++ b/string_slice_test.go @@ -28,17 +28,37 @@ func TestSS(t *testing.T) { } for i, v := range ss { if vals[i] != v { - t.Fatal("expected ss[%d] to be %s but got: %s", i, vals[i], v) + t.Fatalf("expected ss[%d] to be %s but got: %s", i, vals[i], v) } } getSS, err := f.GetStringSlice("ss") if err != nil { - t.Fatal("got an error from GetStringSlice(): %v", err) + t.Fatal("got an error from GetStringSlice():", err) } for i, v := range getSS { if vals[i] != v { - t.Fatal("expected ss[%d] to be %s from GetStringSlice but got: %s", i, vals[i], v) + t.Fatalf("expected ss[%d] to be %s from GetStringSlice but got: %s", i, vals[i], v) + } + } +} + +func TestSSCalledTwice(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) } } } |
