From c6c0f0fd30b9b96d1c9a1cbf90799706bd501458 Mon Sep 17 00:00:00 2001 From: therealmitchconnors Date: Fri, 16 Aug 2019 12:07:51 -0700 Subject: [PATCH] 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 9beeda8e..60c2038c 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 1e7c9edd..e71c39d9 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 0cd3ccc0..74b934b2 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 c41f3bd6..9a7e246c 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