Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for comma separated IntSliceFlag and Int64SliceFlag #1118

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions flag_float64_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ func (f *Float64Slice) Set(value string) error {
return nil
}

tmp, err := strconv.ParseFloat(value, 64)
if err != nil {
return err
for _, v := range strings.Split(value, ",") {
tmp, err := strconv.ParseFloat(v, 64)
if err != nil {
return err
}
f.slice = append(f.slice, tmp)
}

f.slice = append(f.slice, tmp)
return nil
}

Expand Down
12 changes: 6 additions & 6 deletions flag_int64_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ func (i *Int64Slice) Set(value string) error {
return nil
}

tmp, err := strconv.ParseInt(value, 0, 64)
if err != nil {
return err
for _, v := range strings.Split(value, ",") {
tmp, err := strconv.ParseInt(v, 0, 64)
if err != nil {
return err
}
i.slice = append(i.slice, tmp)
}

i.slice = append(i.slice, tmp)

return nil
}

Expand Down
12 changes: 6 additions & 6 deletions flag_int_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ func (i *IntSlice) Set(value string) error {
return nil
}

tmp, err := strconv.ParseInt(value, 0, 64)
if err != nil {
return err
for _, v := range strings.Split(value, ",") {
tmp, err := strconv.ParseInt(v, 0, 64)
if err != nil {
return err
}
i.slice = append(i.slice, int(tmp))
}

i.slice = append(i.slice, int(tmp))

return nil
}

Expand Down
117 changes: 117 additions & 0 deletions flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,28 @@ func TestParseMultiIntSlice(t *testing.T) {
}).Run([]string{"run", "-s", "10", "-s", "20"})
}

func TestParseMultiIntSliceCommaSeparated(t *testing.T) {
//Need to check for error since the parsing fails before it gets into the Action
//Then this test will fail without being noticed
err := (&App{
Flags: []Flag{
&IntSliceFlag{Name: "serve", Aliases: []string{"s"}, Value: NewIntSlice()},
},
Action: func(ctx *Context) error {
if !reflect.DeepEqual(ctx.IntSlice("serve"), []int{10, 20}) {
t.Errorf("main name not set")
}
if !reflect.DeepEqual(ctx.IntSlice("s"), []int{10, 20}) {
t.Errorf("short name not set")
}
return nil
},
}).Run([]string{"run", "-s", "10,20"})
if err != nil {
t.Errorf("failed to parse flags : %s", err)
}
}

func TestParseMultiIntSliceWithDefaults(t *testing.T) {
_ = (&App{
Flags: []Flag{
Expand Down Expand Up @@ -1310,6 +1332,28 @@ func TestParseMultiInt64Slice(t *testing.T) {
}).Run([]string{"run", "-s", "10", "-s", "17179869184"})
}

func TestParseMultiInt64SliceCommaSeparated(t *testing.T) {
//Need to check for error since the parsing fails before it gets into the Action
//Then this test will fail without being noticed
err := (&App{
Flags: []Flag{
&Int64SliceFlag{Name: "serve", Aliases: []string{"s"}, Value: NewInt64Slice()},
},
Action: func(ctx *Context) error {
if !reflect.DeepEqual(ctx.Int64Slice("serve"), []int64{10, 17179869184}) {
t.Errorf("main name not set")
}
if !reflect.DeepEqual(ctx.Int64Slice("s"), []int64{10, 17179869184}) {
t.Errorf("short name not set")
}
return nil
},
}).Run([]string{"run", "-s", "10,17179869184"})
if err != nil {
t.Errorf("failed to parse flags : %s", err)
}
}

func TestParseMultiInt64SliceFromEnv(t *testing.T) {
defer resetEnv(os.Environ())
os.Clearenv()
Expand Down Expand Up @@ -1427,6 +1471,79 @@ func TestParseMultiFloat64FromEnvCascade(t *testing.T) {
}).Run([]string{"run"})
}

func TestParseMultiFloat64Slice(t *testing.T) {
_ = (&App{
Flags: []Flag{
&Float64SliceFlag{Name: "intervals", Aliases: []string{"i"}, Value: NewFloat64Slice()},
},
Action: func(ctx *Context) error {
if !reflect.DeepEqual(ctx.Float64Slice("intervals"), []float64{0.1, -10.5}) {
t.Errorf("main name not set from env")
}
if !reflect.DeepEqual(ctx.Float64Slice("i"), []float64{0.1, -10.5}) {
t.Errorf("short name not set from env")
}
return nil
},
}).Run([]string{"run", "-i", "0.1", "-i", "-10.5"})
}

func TestParseMultiFloat64SliceCommaSeparated(t *testing.T) {
//Need to check for error since the parsing fails before it gets into the Action
//Then this test will fail without being noticed
err := (&App{
Flags: []Flag{
&Float64SliceFlag{Name: "intervals", Aliases: []string{"i"}, Value: NewFloat64Slice()},
},
Action: func(ctx *Context) error {
if !reflect.DeepEqual(ctx.Float64Slice("intervals"), []float64{0.1, -10.5}) {
t.Errorf("main name not set from env")
}
if !reflect.DeepEqual(ctx.Float64Slice("i"), []float64{0.1, -10.5}) {
t.Errorf("short name not set from env")
}
return nil
},
}).Run([]string{"run", "-i", "0.1,-10.5"})
if err != nil {
t.Errorf("failed to parse flags : %s", err)
}
}

func TestParseMultiFloat64SliceWithDefaults(t *testing.T) {
_ = (&App{
Flags: []Flag{
&Float64SliceFlag{Name: "intervals", Aliases: []string{"i"}, Value: NewFloat64Slice(2.3, -3.4)},
},
Action: func(ctx *Context) error {
if !reflect.DeepEqual(ctx.Float64Slice("intervals"), []float64{0.1, -10.5}) {
t.Errorf("main name not set from env")
}
if !reflect.DeepEqual(ctx.Float64Slice("i"), []float64{0.1, -10.5}) {
t.Errorf("short name not set from env")
}
return nil
},
}).Run([]string{"run", "-i", "0.1", "-i", "-10.5"})
}

func TestParseMultiFloat64SliceWithDefaultsUnset(t *testing.T) {
_ = (&App{
Flags: []Flag{
&Float64SliceFlag{Name: "intervals", Aliases: []string{"i"}, Value: NewFloat64Slice(2.3, -3.4)},
},
Action: func(ctx *Context) error {
if !reflect.DeepEqual(ctx.Float64Slice("intervals"), []float64{2.3, -3.4}) {
t.Errorf("main name not set from env")
}
if !reflect.DeepEqual(ctx.Float64Slice("i"), []float64{2.3, -3.4}) {
t.Errorf("short name not set from env")
}
return nil
},
}).Run([]string{"run"})
}

func TestParseMultiFloat64SliceFromEnv(t *testing.T) {
defer resetEnv(os.Environ())
os.Clearenv()
Expand Down