Skip to content

Commit

Permalink
Add first SliceValue implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
therealmitchconnors committed Aug 16, 2019
1 parent 24fa697 commit c6c0f0f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
6 changes: 6 additions & 0 deletions flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
30 changes: 30 additions & 0 deletions int_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions string_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions string_slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit c6c0f0f

Please sign in to comment.