From cb433e7468f95fdf70780e87cf770e4a1c2ebabf Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Tue, 5 Apr 2016 22:38:31 -0400 Subject: [PATCH] Use NewIntSlice and NewStringSlice internally --- app_test.go | 4 ++-- flag.go | 11 ++++++----- flag_test.go | 18 +++++++++--------- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/app_test.go b/app_test.go index ebf26c7..0d1820e 100644 --- a/app_test.go +++ b/app_test.go @@ -313,8 +313,8 @@ func TestApp_ParseSliceFlags(t *testing.T) { command := Command{ Name: "cmd", Flags: []Flag{ - IntSliceFlag{Name: "p", Value: &IntSlice{}, Usage: "set one or more ip addr"}, - StringSliceFlag{Name: "ip", Value: &StringSlice{}, Usage: "set one or more ports to open"}, + IntSliceFlag{Name: "p", Value: NewIntSlice(), Usage: "set one or more ip addr"}, + StringSliceFlag{Name: "ip", Value: NewStringSlice(), Usage: "set one or more ports to open"}, }, Action: func(c *Context) { parsedIntSlice = c.IntSlice("p") diff --git a/flag.go b/flag.go index c7b367b..f93d3e8 100644 --- a/flag.go +++ b/flag.go @@ -165,7 +165,7 @@ func (f StringSliceFlag) Apply(set *flag.FlagSet) { for _, envVar := range strings.Split(f.EnvVar, ",") { envVar = strings.TrimSpace(envVar) if envVal := os.Getenv(envVar); envVal != "" { - newVal := &StringSlice{} + newVal := NewStringSlice() for _, s := range strings.Split(envVal, ",") { s = strings.TrimSpace(s) newVal.Set(s) @@ -177,7 +177,7 @@ func (f StringSliceFlag) Apply(set *flag.FlagSet) { } if f.Value == nil { - f.Value = &StringSlice{} + f.Value = NewStringSlice() } eachName(f.Name, func(name string) { @@ -272,10 +272,11 @@ func (f IntSliceFlag) Apply(set *flag.FlagSet) { } } + if f.Value == nil { + f.Value = NewIntSlice() + } + eachName(f.Name, func(name string) { - if f.Value == nil { - f.Value = &IntSlice{} - } set.Var(f.Value, name, f.Usage) }) } diff --git a/flag_test.go b/flag_test.go index ba8cc0d..3bc6be7 100644 --- a/flag_test.go +++ b/flag_test.go @@ -188,9 +188,9 @@ var intSliceFlagTests = []struct { value *IntSlice expected string }{ - {"help", &IntSlice{}, "--help [--help option --help option]\t"}, - {"h", &IntSlice{}, "-h [-h option -h option]\t"}, - {"h", &IntSlice{}, "-h [-h option -h option]\t"}, + {"help", NewIntSlice(), "--help [--help option --help option]\t"}, + {"h", NewIntSlice(), "-h [-h option -h option]\t"}, + {"h", NewIntSlice(), "-h [-h option -h option]\t"}, {"test", NewIntSlice(9), "--test [--test option --test option]\t"}, } @@ -371,7 +371,7 @@ func TestParseMultiStringFromEnvCascade(t *testing.T) { func TestParseMultiStringSlice(t *testing.T) { (&App{ Flags: []Flag{ - StringSliceFlag{Name: "serve, s", Value: &StringSlice{}}, + StringSliceFlag{Name: "serve, s", Value: NewStringSlice()}, }, Action: func(ctx *Context) { if !reflect.DeepEqual(ctx.StringSlice("serve"), []string{"10", "20"}) { @@ -422,7 +422,7 @@ func TestParseMultiStringSliceFromEnv(t *testing.T) { (&App{ Flags: []Flag{ - StringSliceFlag{Name: "intervals, i", Value: &StringSlice{}, EnvVar: "APP_INTERVALS"}, + StringSliceFlag{Name: "intervals, i", Value: NewStringSlice(), EnvVar: "APP_INTERVALS"}, }, Action: func(ctx *Context) { if !reflect.DeepEqual(ctx.StringSlice("intervals"), []string{"20", "30", "40"}) { @@ -460,7 +460,7 @@ func TestParseMultiStringSliceFromEnvCascade(t *testing.T) { (&App{ Flags: []Flag{ - StringSliceFlag{Name: "intervals, i", Value: &StringSlice{}, EnvVar: "COMPAT_INTERVALS,APP_INTERVALS"}, + StringSliceFlag{Name: "intervals, i", Value: NewStringSlice(), EnvVar: "COMPAT_INTERVALS,APP_INTERVALS"}, }, Action: func(ctx *Context) { if !reflect.DeepEqual(ctx.StringSlice("intervals"), []string{"20", "30", "40"}) { @@ -568,7 +568,7 @@ func TestParseMultiIntFromEnvCascade(t *testing.T) { func TestParseMultiIntSlice(t *testing.T) { (&App{ Flags: []Flag{ - IntSliceFlag{Name: "serve, s", Value: &IntSlice{}}, + IntSliceFlag{Name: "serve, s", Value: NewIntSlice()}, }, Action: func(ctx *Context) { if !reflect.DeepEqual(ctx.IntSlice("serve"), []int{10, 20}) { @@ -619,7 +619,7 @@ func TestParseMultiIntSliceFromEnv(t *testing.T) { (&App{ Flags: []Flag{ - IntSliceFlag{Name: "intervals, i", Value: &IntSlice{}, EnvVar: "APP_INTERVALS"}, + IntSliceFlag{Name: "intervals, i", Value: NewIntSlice(), EnvVar: "APP_INTERVALS"}, }, Action: func(ctx *Context) { if !reflect.DeepEqual(ctx.IntSlice("intervals"), []int{20, 30, 40}) { @@ -657,7 +657,7 @@ func TestParseMultiIntSliceFromEnvCascade(t *testing.T) { (&App{ Flags: []Flag{ - IntSliceFlag{Name: "intervals, i", Value: &IntSlice{}, EnvVar: "COMPAT_INTERVALS,APP_INTERVALS"}, + IntSliceFlag{Name: "intervals, i", Value: NewIntSlice(), EnvVar: "COMPAT_INTERVALS,APP_INTERVALS"}, }, Action: func(ctx *Context) { if !reflect.DeepEqual(ctx.IntSlice("intervals"), []int{20, 30, 40}) {