aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNoah Dietz <[email protected]>2018-11-07 14:08:02 -0800
committerEric Paris <[email protected]>2018-11-07 17:08:02 -0500
commitaea12ed6721610dc6ed40141676d7ab0a1dac9e9 (patch)
tree433f82462c2d9932274a39332b69ab85ffc20a20
parent454a7fbb95f91e2b48adee781b0b02496988da92 (diff)
add int32 & int64 slice support (#194)
-rw-r--r--int32_slice.go132
-rw-r--r--int32_slice_test.go171
-rw-r--r--int64_slice.go128
-rw-r--r--int64_slice_test.go165
4 files changed, 596 insertions, 0 deletions
diff --git a/int32_slice.go b/int32_slice.go
new file mode 100644
index 0000000..6ccedcc
--- /dev/null
+++ b/int32_slice.go
@@ -0,0 +1,132 @@
+package pflag
+
+import (
+ "fmt"
+ "strconv"
+ "strings"
+)
+
+// -- int32Slice Value
+type int32SliceValue struct {
+ value *[]int32
+ changed bool
+}
+
+func newInt32SliceValue(val []int32, p *[]int32) *int32SliceValue {
+ isv := new(int32SliceValue)
+ isv.value = p
+ *isv.value = val
+ return isv
+}
+
+func (s *int32SliceValue) Set(val string) error {
+ ss := strings.Split(val, ",")
+ out := make([]int32, len(ss))
+ for i, d := range ss {
+ var err error
+ var temp64 int64
+ temp64, err = strconv.ParseInt(d, 0, 32)
+ if err != nil {
+ return err
+ }
+ out[i] = int32(temp64)
+
+ }
+ if !s.changed {
+ *s.value = out
+ } else {
+ *s.value = append(*s.value, out...)
+ }
+ s.changed = true
+ return nil
+}
+
+func (s *int32SliceValue) Type() string {
+ return "int32Slice"
+}
+
+func (s *int32SliceValue) String() string {
+ out := make([]string, len(*s.value))
+ for i, d := range *s.value {
+ out[i] = fmt.Sprintf("%d", d)
+ }
+ return "[" + strings.Join(out, ",") + "]"
+}
+
+func int32SliceConv(val string) (interface{}, error) {
+ val = strings.Trim(val, "[]")
+ // Empty string would cause a slice with one (empty) entry
+ if len(val) == 0 {
+ return []int32{}, nil
+ }
+ ss := strings.Split(val, ",")
+ out := make([]int32, len(ss))
+ for i, d := range ss {
+ var err error
+ var temp64 int64
+ temp64, err = strconv.ParseInt(d, 0, 32)
+ if err != nil {
+ return nil, err
+ }
+ out[i] = int32(temp64)
+
+ }
+ return out, nil
+}
+
+// GetInt32Slice return the []int32 value of a flag with the given name
+func (f *FlagSet) GetInt32Slice(name string) ([]int32, error) {
+ val, err := f.getFlagType(name, "int32Slice", int32SliceConv)
+ if err != nil {
+ return []int32{}, err
+ }
+ return val.([]int32), nil
+}
+
+// Int32SliceVar defines a int32Slice flag with specified name, default value, and usage string.
+// The argument p points to a []int32 variable in which to store the value of the flag.
+func (f *FlagSet) Int32SliceVar(p *[]int32, name string, value []int32, usage string) {
+ f.VarP(newInt32SliceValue(value, p), name, "", usage)
+}
+
+// Int32SliceVarP is like Int32SliceVar, but accepts a shorthand letter that can be used after a single dash.
+func (f *FlagSet) Int32SliceVarP(p *[]int32, name, shorthand string, value []int32, usage string) {
+ f.VarP(newInt32SliceValue(value, p), name, shorthand, usage)
+}
+
+// Int32SliceVar defines a int32[] flag with specified name, default value, and usage string.
+// The argument p points to a int32[] variable in which to store the value of the flag.
+func Int32SliceVar(p *[]int32, name string, value []int32, usage string) {
+ CommandLine.VarP(newInt32SliceValue(value, p), name, "", usage)
+}
+
+// Int32SliceVarP is like Int32SliceVar, but accepts a shorthand letter that can be used after a single dash.
+func Int32SliceVarP(p *[]int32, name, shorthand string, value []int32, usage string) {
+ CommandLine.VarP(newInt32SliceValue(value, p), name, shorthand, usage)
+}
+
+// Int32Slice defines a []int32 flag with specified name, default value, and usage string.
+// The return value is the address of a []int32 variable that stores the value of the flag.
+func (f *FlagSet) Int32Slice(name string, value []int32, usage string) *[]int32 {
+ p := []int32{}
+ f.Int32SliceVarP(&p, name, "", value, usage)
+ return &p
+}
+
+// Int32SliceP is like Int32Slice, but accepts a shorthand letter that can be used after a single dash.
+func (f *FlagSet) Int32SliceP(name, shorthand string, value []int32, usage string) *[]int32 {
+ p := []int32{}
+ f.Int32SliceVarP(&p, name, shorthand, value, usage)
+ return &p
+}
+
+// Int32Slice defines a []int32 flag with specified name, default value, and usage string.
+// The return value is the address of a []int32 variable that stores the value of the flag.
+func Int32Slice(name string, value []int32, usage string) *[]int32 {
+ return CommandLine.Int32SliceP(name, "", value, usage)
+}
+
+// Int32SliceP is like Int32Slice, but accepts a shorthand letter that can be used after a single dash.
+func Int32SliceP(name, shorthand string, value []int32, usage string) *[]int32 {
+ return CommandLine.Int32SliceP(name, shorthand, value, usage)
+}
diff --git a/int32_slice_test.go b/int32_slice_test.go
new file mode 100644
index 0000000..69aaf9b
--- /dev/null
+++ b/int32_slice_test.go
@@ -0,0 +1,171 @@
+// Copyright 2009 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package pflag
+
+import (
+ "fmt"
+ "strconv"
+ "strings"
+ "testing"
+)
+
+func setUpI32SFlagSet(isp *[]int32) *FlagSet {
+ f := NewFlagSet("test", ContinueOnError)
+ f.Int32SliceVar(isp, "is", []int32{}, "Command separated list!")
+ return f
+}
+
+func setUpI32SFlagSetWithDefault(isp *[]int32) *FlagSet {
+ f := NewFlagSet("test", ContinueOnError)
+ f.Int32SliceVar(isp, "is", []int32{0, 1}, "Command separated list!")
+ return f
+}
+
+func TestEmptyI32S(t *testing.T) {
+ var is []int32
+ f := setUpI32SFlagSet(&is)
+ err := f.Parse([]string{})
+ if err != nil {
+ t.Fatal("expected no error; got", err)
+ }
+
+ getI32S, err := f.GetInt32Slice("is")
+ if err != nil {
+ t.Fatal("got an error from GetInt32Slice():", err)
+ }
+ if len(getI32S) != 0 {
+ t.Fatalf("got is %v with len=%d but expected length=0", getI32S, len(getI32S))
+ }
+}
+
+func TestI32S(t *testing.T) {
+ var is []int32
+ f := setUpI32SFlagSet(&is)
+
+ vals := []string{"1", "2", "4", "3"}
+ arg := fmt.Sprintf("--is=%s", strings.Join(vals, ","))
+ err := f.Parse([]string{arg})
+ if err != nil {
+ t.Fatal("expected no error; got", err)
+ }
+ for i, v := range is {
+ d64, err := strconv.ParseInt(vals[i], 0, 32)
+ if err != nil {
+ t.Fatalf("got error: %v", err)
+ }
+ d := int32(d64)
+ if d != v {
+ t.Fatalf("expected is[%d] to be %s but got: %d", i, vals[i], v)
+ }
+ }
+ getI32S, err := f.GetInt32Slice("is")
+ if err != nil {
+ t.Fatalf("got error: %v", err)
+ }
+ for i, v := range getI32S {
+ d64, err := strconv.ParseInt(vals[i], 0, 32)
+ if err != nil {
+ t.Fatalf("got error: %v", err)
+ }
+ d := int32(d64)
+ if d != v {
+ t.Fatalf("expected is[%d] to be %s but got: %d from GetInt32Slice", i, vals[i], v)
+ }
+ }
+}
+
+func TestI32SDefault(t *testing.T) {
+ var is []int32
+ f := setUpI32SFlagSetWithDefault(&is)
+
+ vals := []string{"0", "1"}
+
+ err := f.Parse([]string{})
+ if err != nil {
+ t.Fatal("expected no error; got", err)
+ }
+ for i, v := range is {
+ d64, err := strconv.ParseInt(vals[i], 0, 32)
+ if err != nil {
+ t.Fatalf("got error: %v", err)
+ }
+ d := int32(d64)
+ if d != v {
+ t.Fatalf("expected is[%d] to be %d but got: %d", i, d, v)
+ }
+ }
+
+ getI32S, err := f.GetInt32Slice("is")
+ if err != nil {
+ t.Fatal("got an error from GetInt32Slice():", err)
+ }
+ for i, v := range getI32S {
+ d64, err := strconv.ParseInt(vals[i], 0, 32)
+ if err != nil {
+ t.Fatal("got an error from GetInt32Slice():", err)
+ }
+ d := int32(d64)
+ if d != v {
+ t.Fatalf("expected is[%d] to be %d from GetInt32Slice but got: %d", i, d, v)
+ }
+ }
+}
+
+func TestI32SWithDefault(t *testing.T) {
+ var is []int32
+ f := setUpI32SFlagSetWithDefault(&is)
+
+ vals := []string{"1", "2"}
+ arg := fmt.Sprintf("--is=%s", strings.Join(vals, ","))
+ err := f.Parse([]string{arg})
+ if err != nil {
+ t.Fatal("expected no error; got", err)
+ }
+ for i, v := range is {
+ d64, err := strconv.ParseInt(vals[i], 0, 32)
+ if err != nil {
+ t.Fatalf("got error: %v", err)
+ }
+ d := int32(d64)
+ if d != v {
+ t.Fatalf("expected is[%d] to be %d but got: %d", i, d, v)
+ }
+ }
+
+ getI32S, err := f.GetInt32Slice("is")
+ if err != nil {
+ t.Fatal("got an error from GetInt32Slice():", err)
+ }
+ for i, v := range getI32S {
+ d64, err := strconv.ParseInt(vals[i], 0, 32)
+ if err != nil {
+ t.Fatalf("got error: %v", err)
+ }
+ d := int32(d64)
+ if d != v {
+ t.Fatalf("expected is[%d] to be %d from GetInt32Slice but got: %d", i, d, v)
+ }
+ }
+}
+
+func TestI32SCalledTwice(t *testing.T) {
+ var is []int32
+ f := setUpI32SFlagSet(&is)
+
+ in := []string{"1,2", "3"}
+ expected := []int32{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 is[%d] to be %d but got: %d", i, expected[i], v)
+ }
+ }
+}
diff --git a/int64_slice.go b/int64_slice.go
new file mode 100644
index 0000000..86cb619
--- /dev/null
+++ b/int64_slice.go
@@ -0,0 +1,128 @@
+package pflag
+
+import (
+ "fmt"
+ "strconv"
+ "strings"
+)
+
+// -- int64Slice Value
+type int64SliceValue struct {
+ value *[]int64
+ changed bool
+}
+
+func newInt64SliceValue(val []int64, p *[]int64) *int64SliceValue {
+ isv := new(int64SliceValue)
+ isv.value = p
+ *isv.value = val
+ return isv
+}
+
+func (s *int64SliceValue) Set(val string) error {
+ ss := strings.Split(val, ",")
+ out := make([]int64, len(ss))
+ for i, d := range ss {
+ var err error
+ out[i], err = strconv.ParseInt(d, 0, 64)
+ if err != nil {
+ return err
+ }
+
+ }
+ if !s.changed {
+ *s.value = out
+ } else {
+ *s.value = append(*s.value, out...)
+ }
+ s.changed = true
+ return nil
+}
+
+func (s *int64SliceValue) Type() string {
+ return "int64Slice"
+}
+
+func (s *int64SliceValue) String() string {
+ out := make([]string, len(*s.value))
+ for i, d := range *s.value {
+ out[i] = fmt.Sprintf("%d", d)
+ }
+ return "[" + strings.Join(out, ",") + "]"
+}
+
+func int64SliceConv(val string) (interface{}, error) {
+ val = strings.Trim(val, "[]")
+ // Empty string would cause a slice with one (empty) entry
+ if len(val) == 0 {
+ return []int64{}, nil
+ }
+ ss := strings.Split(val, ",")
+ out := make([]int64, len(ss))
+ for i, d := range ss {
+ var err error
+ out[i], err = strconv.ParseInt(d, 0, 64)
+ if err != nil {
+ return nil, err
+ }
+
+ }
+ return out, nil
+}
+
+// GetInt64Slice return the []int64 value of a flag with the given name
+func (f *FlagSet) GetInt64Slice(name string) ([]int64, error) {
+ val, err := f.getFlagType(name, "int64Slice", int64SliceConv)
+ if err != nil {
+ return []int64{}, err
+ }
+ return val.([]int64), nil
+}
+
+// Int64SliceVar defines a int64Slice flag with specified name, default value, and usage string.
+// The argument p points to a []int64 variable in which to store the value of the flag.
+func (f *FlagSet) Int64SliceVar(p *[]int64, name string, value []int64, usage string) {
+ f.VarP(newInt64SliceValue(value, p), name, "", usage)
+}
+
+// Int64SliceVarP is like Int64SliceVar, but accepts a shorthand letter that can be used after a single dash.
+func (f *FlagSet) Int64SliceVarP(p *[]int64, name, shorthand string, value []int64, usage string) {
+ f.VarP(newInt64SliceValue(value, p), name, shorthand, usage)
+}
+
+// Int64SliceVar defines a int64[] flag with specified name, default value, and usage string.
+// The argument p points to a int64[] variable in which to store the value of the flag.
+func Int64SliceVar(p *[]int64, name string, value []int64, usage string) {
+ CommandLine.VarP(newInt64SliceValue(value, p), name, "", usage)
+}
+
+// Int64SliceVarP is like Int64SliceVar, but accepts a shorthand letter that can be used after a single dash.
+func Int64SliceVarP(p *[]int64, name, shorthand string, value []int64, usage string) {
+ CommandLine.VarP(newInt64SliceValue(value, p), name, shorthand, usage)
+}
+
+// Int64Slice defines a []int64 flag with specified name, default value, and usage string.
+// The return value is the address of a []int64 variable that stores the value of the flag.
+func (f *FlagSet) Int64Slice(name string, value []int64, usage string) *[]int64 {
+ p := []int64{}
+ f.Int64SliceVarP(&p, name, "", value, usage)
+ return &p
+}
+
+// Int64SliceP is like Int64Slice, but accepts a shorthand letter that can be used after a single dash.
+func (f *FlagSet) Int64SliceP(name, shorthand string, value []int64, usage string) *[]int64 {
+ p := []int64{}
+ f.Int64SliceVarP(&p, name, shorthand, value, usage)
+ return &p
+}
+
+// Int64Slice defines a []int64 flag with specified name, default value, and usage string.
+// The return value is the address of a []int64 variable that stores the value of the flag.
+func Int64Slice(name string, value []int64, usage string) *[]int64 {
+ return CommandLine.Int64SliceP(name, "", value, usage)
+}
+
+// Int64SliceP is like Int64Slice, but accepts a shorthand letter that can be used after a single dash.
+func Int64SliceP(name, shorthand string, value []int64, usage string) *[]int64 {
+ return CommandLine.Int64SliceP(name, shorthand, value, usage)
+}
diff --git a/int64_slice_test.go b/int64_slice_test.go
new file mode 100644
index 0000000..b536943
--- /dev/null
+++ b/int64_slice_test.go
@@ -0,0 +1,165 @@
+// Copyright 2009 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package pflag
+
+import (
+ "fmt"
+ "strconv"
+ "strings"
+ "testing"
+)
+
+func setUpI64SFlagSet(isp *[]int64) *FlagSet {
+ f := NewFlagSet("test", ContinueOnError)
+ f.Int64SliceVar(isp, "is", []int64{}, "Command separated list!")
+ return f
+}
+
+func setUpI64SFlagSetWithDefault(isp *[]int64) *FlagSet {
+ f := NewFlagSet("test", ContinueOnError)
+ f.Int64SliceVar(isp, "is", []int64{0, 1}, "Command separated list!")
+ return f
+}
+
+func TestEmptyI64S(t *testing.T) {
+ var is []int64
+ f := setUpI64SFlagSet(&is)
+ err := f.Parse([]string{})
+ if err != nil {
+ t.Fatal("expected no error; got", err)
+ }
+
+ getI64S, err := f.GetInt64Slice("is")
+ if err != nil {
+ t.Fatal("got an error from GetInt64Slice():", err)
+ }
+ if len(getI64S) != 0 {
+ t.Fatalf("got is %v with len=%d but expected length=0", getI64S, len(getI64S))
+ }
+}
+
+func TestI64S(t *testing.T) {
+ var is []int64
+ f := setUpI64SFlagSet(&is)
+
+ vals := []string{"1", "2", "4", "3"}
+ arg := fmt.Sprintf("--is=%s", strings.Join(vals, ","))
+ err := f.Parse([]string{arg})
+ if err != nil {
+ t.Fatal("expected no error; got", err)
+ }
+ for i, v := range is {
+ d, err := strconv.ParseInt(vals[i], 0, 64)
+ if err != nil {
+ t.Fatalf("got error: %v", err)
+ }
+ if d != v {
+ t.Fatalf("expected is[%d] to be %s but got: %d", i, vals[i], v)
+ }
+ }
+ getI64S, err := f.GetInt64Slice("is")
+ if err != nil {
+ t.Fatalf("got error: %v", err)
+ }
+ for i, v := range getI64S {
+ d, err := strconv.ParseInt(vals[i], 0, 64)
+ if err != nil {
+ t.Fatalf("got error: %v", err)
+ }
+ if d != v {
+ t.Fatalf("expected is[%d] to be %s but got: %d from GetInt64Slice", i, vals[i], v)
+ }
+ }
+}
+
+func TestI64SDefault(t *testing.T) {
+ var is []int64
+ f := setUpI64SFlagSetWithDefault(&is)
+
+ vals := []string{"0", "1"}
+
+ err := f.Parse([]string{})
+ if err != nil {
+ t.Fatal("expected no error; got", err)
+ }
+ for i, v := range is {
+ d, err := strconv.ParseInt(vals[i], 0, 64)
+ if err != nil {
+ t.Fatalf("got error: %v", err)
+ }
+ if d != v {
+ t.Fatalf("expected is[%d] to be %d but got: %d", i, d, v)
+ }
+ }
+
+ getI64S, err := f.GetInt64Slice("is")
+ if err != nil {
+ t.Fatal("got an error from GetInt64Slice():", err)
+ }
+ for i, v := range getI64S {
+ d, err := strconv.ParseInt(vals[i], 0, 64)
+ if err != nil {
+ t.Fatal("got an error from GetInt64Slice():", err)
+ }
+ if d != v {
+ t.Fatalf("expected is[%d] to be %d from GetInt64Slice but got: %d", i, d, v)
+ }
+ }
+}
+
+func TestI64SWithDefault(t *testing.T) {
+ var is []int64
+ f := setUpI64SFlagSetWithDefault(&is)
+
+ vals := []string{"1", "2"}
+ arg := fmt.Sprintf("--is=%s", strings.Join(vals, ","))
+ err := f.Parse([]string{arg})
+ if err != nil {
+ t.Fatal("expected no error; got", err)
+ }
+ for i, v := range is {
+ d, err := strconv.ParseInt(vals[i], 0, 64)
+ if err != nil {
+ t.Fatalf("got error: %v", err)
+ }
+ if d != v {
+ t.Fatalf("expected is[%d] to be %d but got: %d", i, d, v)
+ }
+ }
+
+ getI64S, err := f.GetInt64Slice("is")
+ if err != nil {
+ t.Fatal("got an error from GetInt64Slice():", err)
+ }
+ for i, v := range getI64S {
+ d, err := strconv.ParseInt(vals[i], 0, 64)
+ if err != nil {
+ t.Fatalf("got error: %v", err)
+ }
+ if d != v {
+ t.Fatalf("expected is[%d] to be %d from GetInt64Slice but got: %d", i, d, v)
+ }
+ }
+}
+
+func TestI64SCalledTwice(t *testing.T) {
+ var is []int64
+ f := setUpI64SFlagSet(&is)
+
+ in := []string{"1,2", "3"}
+ expected := []int64{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 is[%d] to be %d but got: %d", i, expected[i], v)
+ }
+ }
+}