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 1 commit
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
12 changes: 12 additions & 0 deletions flag_int64_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ func (i *Int64Slice) Set(value string) error {
return nil
}

if strings.Contains(value, ",") {
rliebz marked this conversation as resolved.
Show resolved Hide resolved
values := strings.Split(value, ",")
for _, v := range values {
tmp, err := strconv.ParseInt(v, 0, 64)
if err != nil {
return err
}
i.slice = append(i.slice, tmp)
}
return nil
}

tmp, err := strconv.ParseInt(value, 0, 64)
if err != nil {
return err
Expand Down
12 changes: 12 additions & 0 deletions flag_int_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ func (i *IntSlice) Set(value string) error {
return nil
}

if strings.Contains(value, ",") {
rliebz marked this conversation as resolved.
Show resolved Hide resolved
values := strings.Split(value, ",")
for _, v := range values {
tmp, err := strconv.ParseInt(v, 0, 64)
if err != nil {
return err
}
i.slice = append(i.slice, int(tmp))
}
return nil
}

tmp, err := strconv.ParseInt(value, 0, 64)
if err != nil {
return err
Expand Down
44 changes: 44 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 TestParseMultiIntSliceCommaSeperated(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 TestParseMultiInt64SliceCommaSeperated(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