aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--flag.go6
-rw-r--r--int_slice.go30
-rw-r--r--string_slice.go14
-rw-r--r--string_slice_test.go23
4 files changed, 73 insertions, 0 deletions
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