From c6c0f0fd30b9b96d1c9a1cbf90799706bd501458 Mon Sep 17 00:00:00 2001 From: therealmitchconnors Date: Fri, 16 Aug 2019 12:07:51 -0700 Subject: Add first SliceValue implementations --- flag.go | 6 ++++++ int_slice.go | 30 ++++++++++++++++++++++++++++++ string_slice.go | 14 ++++++++++++++ string_slice_test.go | 23 +++++++++++++++++++++++ 4 files changed, 73 insertions(+) diff --git a/flag.go b/flag.go index 9beeda8..60c2038 100644 --- a/flag.go +++ b/flag.go @@ -190,6 +190,12 @@ type Value interface { Type() string } +type SliceValue interface { + Append(string) error + Replace([]string) error + GetSlice() []string +} + // sortFlags returns the flags as a slice in lexicographical sorted order. func sortFlags(flags map[NormalizedName]*Flag) []*Flag { list := make(sort.StringSlice, len(flags)) diff --git a/int_slice.go b/int_slice.go index 1e7c9ed..e71c39d 100644 --- a/int_slice.go +++ b/int_slice.go @@ -51,6 +51,36 @@ func (s *intSliceValue) String() string { return "[" + strings.Join(out, ",") + "]" } +func (s *intSliceValue) Append(val string) error { + i, err := strconv.Atoi(val) + if err != nil { + return err + } + *s.value = append(*s.value, i) + return nil +} + +func (s *intSliceValue) Replace(val []string) error { + out := make([]int, len(val)) + for i, d := range val { + var err error + out[i], err = strconv.Atoi(d) + if err != nil { + return err + } + } + *s.value = out + return nil +} + +func (s *intSliceValue) GetSlice() []string { + out := make([]string, len(*s.value)) + for i, d := range *s.value { + out[i] = strconv.Itoa(d) + } + return out +} + func intSliceConv(val string) (interface{}, error) { val = strings.Trim(val, "[]") // Empty string would cause a slice with one (empty) entry diff --git a/string_slice.go b/string_slice.go index 0cd3ccc..74b934b 100644 --- a/string_slice.go +++ b/string_slice.go @@ -62,6 +62,20 @@ func (s *stringSliceValue) String() string { return "[" + str + "]" } +func (s *stringSliceValue) Append(val string) error { + *s.value = append(*s.value, val) + return nil +} + +func (s *stringSliceValue) Replace(val []string) error { + *s.value = val + return nil +} + +func (s *stringSliceValue) GetSlice() []string { + return *s.value +} + func stringSliceConv(sval string) (interface{}, error) { sval = sval[1 : len(sval)-1] // An empty string would cause a slice with one (empty) string diff --git a/string_slice_test.go b/string_slice_test.go index c41f3bd..9a7e246 100644 --- a/string_slice_test.go +++ b/string_slice_test.go @@ -251,3 +251,26 @@ func TestSSWithSquareBrackets(t *testing.T) { } } } + +func TestSSAsSliceValue(t *testing.T) { + var ss []string + f := setUpSSFlagSet(&ss) + + in := []string{"one", "two"} + 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) + } + + f.VisitAll(func(f *Flag) { + if val, ok := f.Value.(SliceValue); ok { + _ = val.Replace([]string{"three"}) + } + }) + if len(ss) != 1 || ss[0]!= "three" { + t.Fatalf("Expected ss to be overwritten with 'three', but got: %s", ss) + } +} \ No newline at end of file -- cgit v1.2.3 From b22fc706c39eab8ef626dad18f21609425cc8714 Mon Sep 17 00:00:00 2001 From: therealmitchconnors Date: Thu, 5 Sep 2019 11:27:43 -0700 Subject: Expand SliceValue support to all slice and array types. --- bool_slice.go | 38 ++++++++++++++++++++++++++++++++++++++ bool_slice_test.go | 23 +++++++++++++++++++++++ duration_slice.go | 38 ++++++++++++++++++++++++++++++++++++++ duration_slice_test.go | 23 +++++++++++++++++++++++ float32_slice.go | 42 ++++++++++++++++++++++++++++++++++++++++++ float32_slice_test.go | 23 +++++++++++++++++++++++ float64_slice.go | 38 ++++++++++++++++++++++++++++++++++++++ float64_slice_test.go | 23 +++++++++++++++++++++++ go.mod | 5 +++++ go.sum | 2 ++ int32_slice.go | 42 ++++++++++++++++++++++++++++++++++++++++++ int32_slice_test.go | 23 +++++++++++++++++++++++ int64_slice.go | 38 ++++++++++++++++++++++++++++++++++++++ int64_slice_test.go | 23 +++++++++++++++++++++++ ip_slice.go | 38 ++++++++++++++++++++++++++++++++++++++ ip_slice_test.go | 23 +++++++++++++++++++++++ string_array.go | 26 ++++++++++++++++++++++++++ string_array_test.go | 23 +++++++++++++++++++++++ uint_slice.go | 42 ++++++++++++++++++++++++++++++++++++++++++ uint_slice_test.go | 23 +++++++++++++++++++++++ 20 files changed, 556 insertions(+) create mode 100644 go.mod create mode 100644 go.sum diff --git a/bool_slice.go b/bool_slice.go index 5af02f1..3731370 100644 --- a/bool_slice.go +++ b/bool_slice.go @@ -71,6 +71,44 @@ func (s *boolSliceValue) String() string { return "[" + out + "]" } +func (s *boolSliceValue) fromString(val string) (bool, error) { + return strconv.ParseBool(val) +} + +func (s *boolSliceValue) toString(val bool) string { + return strconv.FormatBool(val) +} + +func (s *boolSliceValue) Append(val string) error { + i, err := s.fromString(val) + if err != nil { + return err + } + *s.value = append(*s.value, i) + return nil +} + +func (s *boolSliceValue) Replace(val []string) error { + out := make([]bool, len(val)) + for i, d := range val { + var err error + out[i], err = s.fromString(d) + if err != nil { + return err + } + } + *s.value = out + return nil +} + +func (s *boolSliceValue) GetSlice() []string { + out := make([]string, len(*s.value)) + for i, d := range *s.value { + out[i] = s.toString(d) + } + return out +} + func boolSliceConv(val string) (interface{}, error) { val = strings.Trim(val, "[]") // Empty string would cause a slice with one (empty) entry diff --git a/bool_slice_test.go b/bool_slice_test.go index b617dd2..826bc39 100644 --- a/bool_slice_test.go +++ b/bool_slice_test.go @@ -160,6 +160,29 @@ func TestBSCalledTwice(t *testing.T) { } } +func TestBSAsSliceValue(t *testing.T) { + var bs []bool + f := setUpBSFlagSet(&bs) + + in := []string{"true", "false"} + argfmt := "--bs=%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) + } + + f.VisitAll(func(f *Flag) { + if val, ok := f.Value.(SliceValue); ok { + _ = val.Replace([]string{"false"}) + } + }) + if len(bs) != 1 || bs[0]!= false { + t.Fatalf("Expected ss to be overwritten with 'false', but got: %v", bs) + } +} + func TestBSBadQuoting(t *testing.T) { tests := []struct { diff --git a/duration_slice.go b/duration_slice.go index 52c6b6d..badadda 100644 --- a/duration_slice.go +++ b/duration_slice.go @@ -51,6 +51,44 @@ func (s *durationSliceValue) String() string { return "[" + strings.Join(out, ",") + "]" } +func (s *durationSliceValue) fromString(val string) (time.Duration, error) { + return time.ParseDuration(val) +} + +func (s *durationSliceValue) toString(val time.Duration) string { + return fmt.Sprintf("%s", val) +} + +func (s *durationSliceValue) Append(val string) error { + i, err := s.fromString(val) + if err != nil { + return err + } + *s.value = append(*s.value, i) + return nil +} + +func (s *durationSliceValue) Replace(val []string) error { + out := make([]time.Duration, len(val)) + for i, d := range val { + var err error + out[i], err = s.fromString(d) + if err != nil { + return err + } + } + *s.value = out + return nil +} + +func (s *durationSliceValue) GetSlice() []string { + out := make([]string, len(*s.value)) + for i, d := range *s.value { + out[i] = s.toString(d) + } + return out +} + func durationSliceConv(val string) (interface{}, error) { val = strings.Trim(val, "[]") // Empty string would cause a slice with one (empty) entry diff --git a/duration_slice_test.go b/duration_slice_test.go index 489b012..22bca93 100644 --- a/duration_slice_test.go +++ b/duration_slice_test.go @@ -144,6 +144,29 @@ func TestDSWithDefault(t *testing.T) { } } +func TestDSAsSliceValue(t *testing.T) { + var ds []time.Duration + f := setUpDSFlagSet(&ds) + + in := []string{"1ns", "2ns"} + argfmt := "--ds=%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) + } + + f.VisitAll(func(f *Flag) { + if val, ok := f.Value.(SliceValue); ok { + _ = val.Replace([]string{"3ns"}) + } + }) + if len(ds) != 1 || ds[0]!= time.Duration(3) { + t.Fatalf("Expected ss to be overwritten with '3ns', but got: %v", ds) + } +} + func TestDSCalledTwice(t *testing.T) { var ds []time.Duration f := setUpDSFlagSet(&ds) diff --git a/float32_slice.go b/float32_slice.go index a80848a..caa3527 100644 --- a/float32_slice.go +++ b/float32_slice.go @@ -53,6 +53,48 @@ func (s *float32SliceValue) String() string { return "[" + strings.Join(out, ",") + "]" } +func (s *float32SliceValue) fromString(val string) (float32, error) { + t64, err := strconv.ParseFloat(val, 32) + if err != nil { + return 0, err + } + return float32(t64), nil +} + +func (s *float32SliceValue) toString(val float32) string { + return fmt.Sprintf("%f", val) +} + +func (s *float32SliceValue) Append(val string) error { + i, err := s.fromString(val) + if err != nil { + return err + } + *s.value = append(*s.value, i) + return nil +} + +func (s *float32SliceValue) Replace(val []string) error { + out := make([]float32, len(val)) + for i, d := range val { + var err error + out[i], err = s.fromString(d) + if err != nil { + return err + } + } + *s.value = out + return nil +} + +func (s *float32SliceValue) GetSlice() []string { + out := make([]string, len(*s.value)) + for i, d := range *s.value { + out[i] = s.toString(d) + } + return out +} + func float32SliceConv(val string) (interface{}, error) { val = strings.Trim(val, "[]") // Empty string would cause a slice with one (empty) entry diff --git a/float32_slice_test.go b/float32_slice_test.go index 2429c8f..1e9a942 100644 --- a/float32_slice_test.go +++ b/float32_slice_test.go @@ -156,6 +156,29 @@ func TestF32SWithDefault(t *testing.T) { } } +func TestF32SAsSliceValue(t *testing.T) { + var f32s []float32 + f := setUpF32SFlagSet(&f32s) + + in := []string{"1.0", "2.0"} + argfmt := "--f32s=%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) + } + + f.VisitAll(func(f *Flag) { + if val, ok := f.Value.(SliceValue); ok { + _ = val.Replace([]string{"3.1"}) + } + }) + if len(f32s) != 1 || f32s[0]!= 3.1 { + t.Fatalf("Expected ss to be overwritten with '3.1', but got: %v", f32s) + } +} + func TestF32SCalledTwice(t *testing.T) { var f32s []float32 f := setUpF32SFlagSet(&f32s) diff --git a/float64_slice.go b/float64_slice.go index 45d12b8..85bf307 100644 --- a/float64_slice.go +++ b/float64_slice.go @@ -51,6 +51,44 @@ func (s *float64SliceValue) String() string { return "[" + strings.Join(out, ",") + "]" } +func (s *float64SliceValue) fromString(val string) (float64, error) { + return strconv.ParseFloat(val, 64) +} + +func (s *float64SliceValue) toString(val float64) string { + return fmt.Sprintf("%f", val) +} + +func (s *float64SliceValue) Append(val string) error { + i, err := s.fromString(val) + if err != nil { + return err + } + *s.value = append(*s.value, i) + return nil +} + +func (s *float64SliceValue) Replace(val []string) error { + out := make([]float64, len(val)) + for i, d := range val { + var err error + out[i], err = s.fromString(d) + if err != nil { + return err + } + } + *s.value = out + return nil +} + +func (s *float64SliceValue) GetSlice() []string { + out := make([]string, len(*s.value)) + for i, d := range *s.value { + out[i] = s.toString(d) + } + return out +} + func float64SliceConv(val string) (interface{}, error) { val = strings.Trim(val, "[]") // Empty string would cause a slice with one (empty) entry diff --git a/float64_slice_test.go b/float64_slice_test.go index fe9ede4..011bbdd 100644 --- a/float64_slice_test.go +++ b/float64_slice_test.go @@ -144,6 +144,29 @@ func TestF64SWithDefault(t *testing.T) { } } +func TestF64SAsSliceValue(t *testing.T) { + var f64s []float64 + f := setUpF64SFlagSet(&f64s) + + in := []string{"1.0", "2.0"} + argfmt := "--f64s=%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) + } + + f.VisitAll(func(f *Flag) { + if val, ok := f.Value.(SliceValue); ok { + _ = val.Replace([]string{"3.1"}) + } + }) + if len(f64s) != 1 || f64s[0]!= 3.1 { + t.Fatalf("Expected ss to be overwritten with '3.1', but got: %v", f64s) + } +} + func TestF64SCalledTwice(t *testing.T) { var f64s []float64 f := setUpF64SFlagSet(&f64s) diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..7eb892e --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/spf13/pflags + +go 1.12 + +require github.com/spf13/pflag v1.0.3 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..edd0bcf --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg= +github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= diff --git a/int32_slice.go b/int32_slice.go index 6ccedcc..ff128ff 100644 --- a/int32_slice.go +++ b/int32_slice.go @@ -53,6 +53,48 @@ func (s *int32SliceValue) String() string { return "[" + strings.Join(out, ",") + "]" } +func (s *int32SliceValue) fromString(val string) (int32, error) { + t64, err := strconv.ParseInt(val, 0, 32) + if err != nil { + return 0, err + } + return int32(t64), nil +} + +func (s *int32SliceValue) toString(val int32) string { + return fmt.Sprintf("%d", val) +} + +func (s *int32SliceValue) Append(val string) error { + i, err := s.fromString(val) + if err != nil { + return err + } + *s.value = append(*s.value, i) + return nil +} + +func (s *int32SliceValue) Replace(val []string) error { + out := make([]int32, len(val)) + for i, d := range val { + var err error + out[i], err = s.fromString(d) + if err != nil { + return err + } + } + *s.value = out + return nil +} + +func (s *int32SliceValue) GetSlice() []string { + out := make([]string, len(*s.value)) + for i, d := range *s.value { + out[i] = s.toString(d) + } + return out +} + func int32SliceConv(val string) (interface{}, error) { val = strings.Trim(val, "[]") // Empty string would cause a slice with one (empty) entry diff --git a/int32_slice_test.go b/int32_slice_test.go index 69aaf9b..d2b49e4 100644 --- a/int32_slice_test.go +++ b/int32_slice_test.go @@ -150,6 +150,29 @@ func TestI32SWithDefault(t *testing.T) { } } +func TestI32SAsSliceValue(t *testing.T) { + var i32s []int32 + f := setUpI32SFlagSet(&i32s) + + in := []string{"1", "2"} + 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) + } + + f.VisitAll(func(f *Flag) { + if val, ok := f.Value.(SliceValue); ok { + _ = val.Replace([]string{"3"}) + } + }) + if len(i32s) != 1 || i32s[0]!= 3 { + t.Fatalf("Expected ss to be overwritten with '3.1', but got: %v", i32s) + } +} + func TestI32SCalledTwice(t *testing.T) { var is []int32 f := setUpI32SFlagSet(&is) diff --git a/int64_slice.go b/int64_slice.go index 86cb619..2546463 100644 --- a/int64_slice.go +++ b/int64_slice.go @@ -51,6 +51,44 @@ func (s *int64SliceValue) String() string { return "[" + strings.Join(out, ",") + "]" } +func (s *int64SliceValue) fromString(val string) (int64, error) { + return strconv.ParseInt(val, 0, 64) +} + +func (s *int64SliceValue) toString(val int64) string { + return fmt.Sprintf("%d", val) +} + +func (s *int64SliceValue) Append(val string) error { + i, err := s.fromString(val) + if err != nil { + return err + } + *s.value = append(*s.value, i) + return nil +} + +func (s *int64SliceValue) Replace(val []string) error { + out := make([]int64, len(val)) + for i, d := range val { + var err error + out[i], err = s.fromString(d) + if err != nil { + return err + } + } + *s.value = out + return nil +} + +func (s *int64SliceValue) GetSlice() []string { + out := make([]string, len(*s.value)) + for i, d := range *s.value { + out[i] = s.toString(d) + } + return out +} + func int64SliceConv(val string) (interface{}, error) { val = strings.Trim(val, "[]") // Empty string would cause a slice with one (empty) entry diff --git a/int64_slice_test.go b/int64_slice_test.go index b536943..daf4bd4 100644 --- a/int64_slice_test.go +++ b/int64_slice_test.go @@ -144,6 +144,29 @@ func TestI64SWithDefault(t *testing.T) { } } +func TestI64SAsSliceValue(t *testing.T) { + var i64s []int64 + f := setUpI64SFlagSet(&i64s) + + in := []string{"1", "2"} + 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) + } + + f.VisitAll(func(f *Flag) { + if val, ok := f.Value.(SliceValue); ok { + _ = val.Replace([]string{"3"}) + } + }) + if len(i64s) != 1 || i64s[0]!= 3 { + t.Fatalf("Expected ss to be overwritten with '3.1', but got: %v", i64s) + } +} + func TestI64SCalledTwice(t *testing.T) { var is []int64 f := setUpI64SFlagSet(&is) diff --git a/ip_slice.go b/ip_slice.go index 6ad2e92..775faae 100644 --- a/ip_slice.go +++ b/ip_slice.go @@ -72,6 +72,44 @@ func (s *ipSliceValue) String() string { return "[" + out + "]" } +func (s *ipSliceValue) fromString(val string) (net.IP, error) { + return net.ParseIP(strings.TrimSpace(val)), nil +} + +func (s *ipSliceValue) toString(val net.IP) string { + return val.String() +} + +func (s *ipSliceValue) Append(val string) error { + i, err := s.fromString(val) + if err != nil { + return err + } + *s.value = append(*s.value, i) + return nil +} + +func (s *ipSliceValue) Replace(val []string) error { + out := make([]net.IP, len(val)) + for i, d := range val { + var err error + out[i], err = s.fromString(d) + if err != nil { + return err + } + } + *s.value = out + return nil +} + +func (s *ipSliceValue) GetSlice() []string { + out := make([]string, len(*s.value)) + for i, d := range *s.value { + out[i] = s.toString(d) + } + return out +} + func ipSliceConv(val string) (interface{}, error) { val = strings.Trim(val, "[]") // Empty string would cause a slice with one (empty) entry diff --git a/ip_slice_test.go b/ip_slice_test.go index b0c681c..d189276 100644 --- a/ip_slice_test.go +++ b/ip_slice_test.go @@ -141,6 +141,29 @@ func TestIPSCalledTwice(t *testing.T) { } } +func TestIPSAsSliceValue(t *testing.T) { + var ips []net.IP + f := setUpIPSFlagSet(&ips) + + in := []string{"192.168.1.1", "0:0:0:0:0:0:0:1"} + argfmt := "--ips=%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) + } + + f.VisitAll(func(f *Flag) { + if val, ok := f.Value.(SliceValue); ok { + _ = val.Replace([]string{"192.168.1.2"}) + } + }) + if len(ips) != 1 || !ips[0].Equal(net.ParseIP("192.168.1.2")) { + t.Fatalf("Expected ss to be overwritten with '192.168.1.2', but got: %v", ips) + } +} + func TestIPSBadQuoting(t *testing.T) { tests := []struct { diff --git a/string_array.go b/string_array.go index fa7bc60..4894af8 100644 --- a/string_array.go +++ b/string_array.go @@ -23,6 +23,32 @@ func (s *stringArrayValue) Set(val string) error { return nil } +func (s *stringArrayValue) Append(val string) error { + *s.value = append(*s.value, val) + return nil +} + +func (s *stringArrayValue) Replace(val []string) error { + out := make([]string, len(val)) + for i, d := range val { + var err error + out[i] = d + if err != nil { + return err + } + } + *s.value = out + return nil +} + +func (s *stringArrayValue) GetSlice() []string { + out := make([]string, len(*s.value)) + for i, d := range *s.value { + out[i] = d + } + return out +} + func (s *stringArrayValue) Type() string { return "stringArray" } diff --git a/string_array_test.go b/string_array_test.go index 1ceac8c..21f3633 100644 --- a/string_array_test.go +++ b/string_array_test.go @@ -193,6 +193,29 @@ func TestSAWithSpecialChar(t *testing.T) { } } +func TestSAAsSliceValue(t *testing.T) { + var sa []string + f := setUpSAFlagSet(&sa) + + in := []string{"1ns", "2ns"} + argfmt := "--sa=%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) + } + + f.VisitAll(func(f *Flag) { + if val, ok := f.Value.(SliceValue); ok { + _ = val.Replace([]string{"3ns"}) + } + }) + if len(sa) != 1 || sa[0]!= "3ns" { + t.Fatalf("Expected ss to be overwritten with '3ns', but got: %v", sa) + } +} + func TestSAWithSquareBrackets(t *testing.T) { var sa []string f := setUpSAFlagSet(&sa) diff --git a/uint_slice.go b/uint_slice.go index edd94c6..5fa9248 100644 --- a/uint_slice.go +++ b/uint_slice.go @@ -50,6 +50,48 @@ func (s *uintSliceValue) String() string { return "[" + strings.Join(out, ",") + "]" } +func (s *uintSliceValue) fromString(val string) (uint, error) { + t, err := strconv.ParseUint(val, 10, 0) + if err != nil { + return 0, err + } + return uint(t), nil +} + +func (s *uintSliceValue) toString(val uint) string { + return fmt.Sprintf("%d", val) +} + +func (s *uintSliceValue) Append(val string) error { + i, err := s.fromString(val) + if err != nil { + return err + } + *s.value = append(*s.value, i) + return nil +} + +func (s *uintSliceValue) Replace(val []string) error { + out := make([]uint, len(val)) + for i, d := range val { + var err error + out[i], err = s.fromString(d) + if err != nil { + return err + } + } + *s.value = out + return nil +} + +func (s *uintSliceValue) GetSlice() []string { + out := make([]string, len(*s.value)) + for i, d := range *s.value { + out[i] = s.toString(d) + } + return out +} + func uintSliceConv(val string) (interface{}, error) { val = strings.Trim(val, "[]") // Empty string would cause a slice with one (empty) entry diff --git a/uint_slice_test.go b/uint_slice_test.go index db1a19d..f5b5bfc 100644 --- a/uint_slice_test.go +++ b/uint_slice_test.go @@ -140,6 +140,29 @@ func TestUISWithDefault(t *testing.T) { } } +func TestUISAsSliceValue(t *testing.T) { + var uis []uint + f := setUpUISFlagSet(&uis) + + in := []string{"1", "2"} + argfmt := "--uis=%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) + } + + f.VisitAll(func(f *Flag) { + if val, ok := f.Value.(SliceValue); ok { + _ = val.Replace([]string{"3"}) + } + }) + if len(uis) != 1 || uis[0]!= 3 { + t.Fatalf("Expected ss to be overwritten with '3.1', but got: %v", uis) + } +} + func TestUISCalledTwice(t *testing.T) { var uis []uint f := setUpUISFlagSet(&uis) -- cgit v1.2.3 From 68f4136ecf4c77dccc4d7003f5020fafa4833534 Mon Sep 17 00:00:00 2001 From: therealmitchconnors Date: Tue, 10 Sep 2019 09:27:08 -0700 Subject: Add SliceValue Comments --- flag.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/flag.go b/flag.go index 60c2038..e02abfe 100644 --- a/flag.go +++ b/flag.go @@ -190,9 +190,15 @@ type Value interface { Type() string } +// SliceValue is a secondary interface to all flags which hold a list +// of values. This allows full control over the value of list flags, +// and avoids complicated marshalling and unmarshalling to csv. type SliceValue interface { + // Append adds the specified value to the end of the flag value list. Append(string) error + // Replace will fully overwrite any data currently in the flag value list. Replace([]string) error + // GetSlice returns the flag value list as an array of strings. GetSlice() []string } -- cgit v1.2.3 From 8e39cc44d9bd06ee2d9ef3109c93809072d5e551 Mon Sep 17 00:00:00 2001 From: therealmitchconnors Date: Tue, 10 Sep 2019 09:45:17 -0700 Subject: gofmt --- bool_slice_test.go | 2 +- duration_slice_test.go | 2 +- float32_slice_test.go | 2 +- float64_slice_test.go | 2 +- int32_slice_test.go | 2 +- int64_slice_test.go | 2 +- string_array_test.go | 2 +- string_slice.go | 2 +- string_slice_test.go | 4 ++-- uint_slice_test.go | 2 +- 10 files changed, 11 insertions(+), 11 deletions(-) diff --git a/bool_slice_test.go b/bool_slice_test.go index 826bc39..3c5a274 100644 --- a/bool_slice_test.go +++ b/bool_slice_test.go @@ -178,7 +178,7 @@ func TestBSAsSliceValue(t *testing.T) { _ = val.Replace([]string{"false"}) } }) - if len(bs) != 1 || bs[0]!= false { + if len(bs) != 1 || bs[0] != false { t.Fatalf("Expected ss to be overwritten with 'false', but got: %v", bs) } } diff --git a/duration_slice_test.go b/duration_slice_test.go index 22bca93..651fbd8 100644 --- a/duration_slice_test.go +++ b/duration_slice_test.go @@ -162,7 +162,7 @@ func TestDSAsSliceValue(t *testing.T) { _ = val.Replace([]string{"3ns"}) } }) - if len(ds) != 1 || ds[0]!= time.Duration(3) { + if len(ds) != 1 || ds[0] != time.Duration(3) { t.Fatalf("Expected ss to be overwritten with '3ns', but got: %v", ds) } } diff --git a/float32_slice_test.go b/float32_slice_test.go index 1e9a942..997ce5c 100644 --- a/float32_slice_test.go +++ b/float32_slice_test.go @@ -174,7 +174,7 @@ func TestF32SAsSliceValue(t *testing.T) { _ = val.Replace([]string{"3.1"}) } }) - if len(f32s) != 1 || f32s[0]!= 3.1 { + if len(f32s) != 1 || f32s[0] != 3.1 { t.Fatalf("Expected ss to be overwritten with '3.1', but got: %v", f32s) } } diff --git a/float64_slice_test.go b/float64_slice_test.go index 011bbdd..43778ef 100644 --- a/float64_slice_test.go +++ b/float64_slice_test.go @@ -162,7 +162,7 @@ func TestF64SAsSliceValue(t *testing.T) { _ = val.Replace([]string{"3.1"}) } }) - if len(f64s) != 1 || f64s[0]!= 3.1 { + if len(f64s) != 1 || f64s[0] != 3.1 { t.Fatalf("Expected ss to be overwritten with '3.1', but got: %v", f64s) } } diff --git a/int32_slice_test.go b/int32_slice_test.go index d2b49e4..809c563 100644 --- a/int32_slice_test.go +++ b/int32_slice_test.go @@ -168,7 +168,7 @@ func TestI32SAsSliceValue(t *testing.T) { _ = val.Replace([]string{"3"}) } }) - if len(i32s) != 1 || i32s[0]!= 3 { + if len(i32s) != 1 || i32s[0] != 3 { t.Fatalf("Expected ss to be overwritten with '3.1', but got: %v", i32s) } } diff --git a/int64_slice_test.go b/int64_slice_test.go index daf4bd4..09805c7 100644 --- a/int64_slice_test.go +++ b/int64_slice_test.go @@ -162,7 +162,7 @@ func TestI64SAsSliceValue(t *testing.T) { _ = val.Replace([]string{"3"}) } }) - if len(i64s) != 1 || i64s[0]!= 3 { + if len(i64s) != 1 || i64s[0] != 3 { t.Fatalf("Expected ss to be overwritten with '3.1', but got: %v", i64s) } } diff --git a/string_array_test.go b/string_array_test.go index 21f3633..3c6d595 100644 --- a/string_array_test.go +++ b/string_array_test.go @@ -211,7 +211,7 @@ func TestSAAsSliceValue(t *testing.T) { _ = val.Replace([]string{"3ns"}) } }) - if len(sa) != 1 || sa[0]!= "3ns" { + if len(sa) != 1 || sa[0] != "3ns" { t.Fatalf("Expected ss to be overwritten with '3ns', but got: %v", sa) } } diff --git a/string_slice.go b/string_slice.go index 74b934b..13d0572 100644 --- a/string_slice.go +++ b/string_slice.go @@ -73,7 +73,7 @@ func (s *stringSliceValue) Replace(val []string) error { } func (s *stringSliceValue) GetSlice() []string { - return *s.value + return *s.value } func stringSliceConv(sval string) (interface{}, error) { diff --git a/string_slice_test.go b/string_slice_test.go index 9a7e246..9692461 100644 --- a/string_slice_test.go +++ b/string_slice_test.go @@ -270,7 +270,7 @@ func TestSSAsSliceValue(t *testing.T) { _ = val.Replace([]string{"three"}) } }) - if len(ss) != 1 || ss[0]!= "three" { + if len(ss) != 1 || ss[0] != "three" { t.Fatalf("Expected ss to be overwritten with 'three', but got: %s", ss) } -} \ No newline at end of file +} diff --git a/uint_slice_test.go b/uint_slice_test.go index f5b5bfc..d0da4d0 100644 --- a/uint_slice_test.go +++ b/uint_slice_test.go @@ -158,7 +158,7 @@ func TestUISAsSliceValue(t *testing.T) { _ = val.Replace([]string{"3"}) } }) - if len(uis) != 1 || uis[0]!= 3 { + if len(uis) != 1 || uis[0] != 3 { t.Fatalf("Expected ss to be overwritten with '3.1', but got: %v", uis) } } -- cgit v1.2.3