Overwrite slice flag defaults when set

Closes #160
This commit is contained in:
Dan Buch 2016-04-05 12:35:30 -04:00
parent 71f57d300d
commit 867aa0912d
3 changed files with 191 additions and 53 deletions

View File

@ -122,7 +122,7 @@ func (f *StringSliceFlag) ApplyInputSourceValue(context *cli.Context, isc InputS
return err return err
} }
if value != nil { if value != nil {
var sliceValue cli.StringSlice = value var sliceValue cli.StringSlice = *(cli.NewStringSlice(value...))
eachName(f.Name, func(name string) { eachName(f.Name, func(name string) {
underlyingFlag := f.set.Lookup(f.Name) underlyingFlag := f.set.Lookup(f.Name)
if underlyingFlag != nil { if underlyingFlag != nil {
@ -163,7 +163,7 @@ func (f *IntSliceFlag) ApplyInputSourceValue(context *cli.Context, isc InputSour
return err return err
} }
if value != nil { if value != nil {
var sliceValue cli.IntSlice = value var sliceValue cli.IntSlice = *(cli.NewIntSlice(value...))
eachName(f.Name, func(name string) { eachName(f.Name, func(name string) {
underlyingFlag := f.set.Lookup(f.Name) underlyingFlag := f.set.Lookup(f.Name)
if underlyingFlag != nil { if underlyingFlag != nil {

71
flag.go
View File

@ -111,26 +111,39 @@ func (f GenericFlag) GetName() string {
return f.Name return f.Name
} }
// StringSlice is an opaque type for []string to satisfy flag.Value // StringSlice wraps a []string to satisfy flag.Value
type StringSlice []string type StringSlice struct {
slice []string
hasBeenSet bool
}
// NewStringSlice creates a *StringSlice with default values
func NewStringSlice(defaults ...string) *StringSlice {
return &StringSlice{slice: append([]string{}, defaults...)}
}
// Set appends the string value to the list of values // Set appends the string value to the list of values
func (f *StringSlice) Set(value string) error { func (f *StringSlice) Set(value string) error {
*f = append(*f, value) if !f.hasBeenSet {
f.slice = []string{}
f.hasBeenSet = true
}
f.slice = append(f.slice, value)
return nil return nil
} }
// String returns a readable representation of this value (for usage defaults) // String returns a readable representation of this value (for usage defaults)
func (f *StringSlice) String() string { func (f *StringSlice) String() string {
return fmt.Sprintf("%s", *f) return fmt.Sprintf("%s", f.slice)
} }
// Value returns the slice of strings set by this flag // Value returns the slice of strings set by this flag
func (f *StringSlice) Value() []string { func (f *StringSlice) Value() []string {
return *f return f.slice
} }
// StringSlice is a string flag that can be specified multiple times on the // StringSliceFlag is a string flag that can be specified multiple times on the
// command-line // command-line
type StringSliceFlag struct { type StringSliceFlag struct {
Name string Name string
@ -163,10 +176,11 @@ func (f StringSliceFlag) Apply(set *flag.FlagSet) {
} }
} }
if f.Value == nil {
f.Value = &StringSlice{}
}
eachName(f.Name, func(name string) { eachName(f.Name, func(name string) {
if f.Value == nil {
f.Value = &StringSlice{}
}
set.Var(f.Value, name, f.Usage) set.Var(f.Value, name, f.Usage)
}) })
} }
@ -175,28 +189,51 @@ func (f StringSliceFlag) GetName() string {
return f.Name return f.Name
} }
// StringSlice is an opaque type for []int to satisfy flag.Value // IntSlice wraps an []int to satisfy flag.Value
type IntSlice []int type IntSlice struct {
slice []int
hasBeenSet bool
}
// NewIntSlice makes an *IntSlice with default values
func NewIntSlice(defaults ...int) *IntSlice {
return &IntSlice{slice: append([]int{}, defaults...)}
}
// SetInt directly adds an integer to the list of values
func (i *IntSlice) SetInt(value int) {
if !i.hasBeenSet {
i.slice = []int{}
i.hasBeenSet = true
}
i.slice = append(i.slice, value)
}
// Set parses the value into an integer and appends it to the list of values // Set parses the value into an integer and appends it to the list of values
func (f *IntSlice) Set(value string) error { func (i *IntSlice) Set(value string) error {
if !i.hasBeenSet {
i.slice = []int{}
i.hasBeenSet = true
}
tmp, err := strconv.Atoi(value) tmp, err := strconv.Atoi(value)
if err != nil { if err != nil {
return err return err
} else { } else {
*f = append(*f, tmp) i.slice = append(i.slice, tmp)
} }
return nil return nil
} }
// String returns a readable representation of this value (for usage defaults) // String returns a readable representation of this value (for usage defaults)
func (f *IntSlice) String() string { func (i *IntSlice) String() string {
return fmt.Sprintf("%d", *f) return fmt.Sprintf("%v", i.slice)
} }
// Value returns the slice of ints set by this flag // Value returns the slice of ints set by this flag
func (f *IntSlice) Value() []int { func (i *IntSlice) Value() []int {
return *f return i.slice
} }
// IntSliceFlag is an int flag that can be specified multiple times on the // IntSliceFlag is an int flag that can be specified multiple times on the

View File

@ -4,9 +4,9 @@ import (
"fmt" "fmt"
"os" "os"
"reflect" "reflect"
"runtime"
"strings" "strings"
"testing" "testing"
"runtime"
) )
var boolFlagTests = []struct { var boolFlagTests = []struct {
@ -64,7 +64,7 @@ func TestStringFlagWithEnvVarHelpOutput(t *testing.T) {
expectedSuffix = " [%APP_FOO%]" expectedSuffix = " [%APP_FOO%]"
} }
if !strings.HasSuffix(output, expectedSuffix) { if !strings.HasSuffix(output, expectedSuffix) {
t.Errorf("%s does not end with" + expectedSuffix, output) t.Errorf("%s does not end with"+expectedSuffix, output)
} }
} }
} }
@ -74,30 +74,14 @@ var stringSliceFlagTests = []struct {
value *StringSlice value *StringSlice
expected string expected string
}{ }{
{"help", func() *StringSlice { {"help", NewStringSlice(""), "--help [--help option --help option]\t"},
s := &StringSlice{} {"h", NewStringSlice(""), "-h [-h option -h option]\t"},
s.Set("") {"h", NewStringSlice(""), "-h [-h option -h option]\t"},
return s {"test", NewStringSlice("Something"), "--test [--test option --test option]\t"},
}(), "--help [--help option --help option]\t"}, {"d, dee", NewStringSlice("Inka", "Dinka", "dooo"), "-d, --dee [-d option -d option]\t"},
{"h", func() *StringSlice {
s := &StringSlice{}
s.Set("")
return s
}(), "-h [-h option -h option]\t"},
{"h", func() *StringSlice {
s := &StringSlice{}
s.Set("")
return s
}(), "-h [-h option -h option]\t"},
{"test", func() *StringSlice {
s := &StringSlice{}
s.Set("Something")
return s
}(), "--test [--test option --test option]\t"},
} }
func TestStringSliceFlagHelpOutput(t *testing.T) { func TestStringSliceFlagHelpOutput(t *testing.T) {
for _, test := range stringSliceFlagTests { for _, test := range stringSliceFlagTests {
flag := StringSliceFlag{Name: test.name, Value: test.value} flag := StringSliceFlag{Name: test.name, Value: test.value}
output := flag.String() output := flag.String()
@ -120,7 +104,7 @@ func TestStringSliceFlagWithEnvVarHelpOutput(t *testing.T) {
expectedSuffix = " [%APP_QWWX%]" expectedSuffix = " [%APP_QWWX%]"
} }
if !strings.HasSuffix(output, expectedSuffix) { if !strings.HasSuffix(output, expectedSuffix) {
t.Errorf("%q does not end with" + expectedSuffix, output) t.Errorf("%q does not end with"+expectedSuffix, output)
} }
} }
} }
@ -157,7 +141,7 @@ func TestIntFlagWithEnvVarHelpOutput(t *testing.T) {
expectedSuffix = " [%APP_BAR%]" expectedSuffix = " [%APP_BAR%]"
} }
if !strings.HasSuffix(output, expectedSuffix) { if !strings.HasSuffix(output, expectedSuffix) {
t.Errorf("%s does not end with" + expectedSuffix, output) t.Errorf("%s does not end with"+expectedSuffix, output)
} }
} }
} }
@ -194,7 +178,7 @@ func TestDurationFlagWithEnvVarHelpOutput(t *testing.T) {
expectedSuffix = " [%APP_BAR%]" expectedSuffix = " [%APP_BAR%]"
} }
if !strings.HasSuffix(output, expectedSuffix) { if !strings.HasSuffix(output, expectedSuffix) {
t.Errorf("%s does not end with" + expectedSuffix, output) t.Errorf("%s does not end with"+expectedSuffix, output)
} }
} }
} }
@ -207,11 +191,7 @@ var intSliceFlagTests = []struct {
{"help", &IntSlice{}, "--help [--help option --help option]\t"}, {"help", &IntSlice{}, "--help [--help option --help option]\t"},
{"h", &IntSlice{}, "-h [-h option -h option]\t"}, {"h", &IntSlice{}, "-h [-h option -h option]\t"},
{"h", &IntSlice{}, "-h [-h option -h option]\t"}, {"h", &IntSlice{}, "-h [-h option -h option]\t"},
{"test", func() *IntSlice { {"test", NewIntSlice(9), "--test [--test option --test option]\t"},
i := &IntSlice{}
i.Set("9")
return i
}(), "--test [--test option --test option]\t"},
} }
func TestIntSliceFlagHelpOutput(t *testing.T) { func TestIntSliceFlagHelpOutput(t *testing.T) {
@ -238,7 +218,7 @@ func TestIntSliceFlagWithEnvVarHelpOutput(t *testing.T) {
expectedSuffix = " [%APP_SMURF%]" expectedSuffix = " [%APP_SMURF%]"
} }
if !strings.HasSuffix(output, expectedSuffix) { if !strings.HasSuffix(output, expectedSuffix) {
t.Errorf("%q does not end with" + expectedSuffix, output) t.Errorf("%q does not end with"+expectedSuffix, output)
} }
} }
} }
@ -275,7 +255,7 @@ func TestFloat64FlagWithEnvVarHelpOutput(t *testing.T) {
expectedSuffix = " [%APP_BAZ%]" expectedSuffix = " [%APP_BAZ%]"
} }
if !strings.HasSuffix(output, expectedSuffix) { if !strings.HasSuffix(output, expectedSuffix) {
t.Errorf("%s does not end with" + expectedSuffix, output) t.Errorf("%s does not end with"+expectedSuffix, output)
} }
} }
} }
@ -313,7 +293,7 @@ func TestGenericFlagWithEnvVarHelpOutput(t *testing.T) {
expectedSuffix = " [%APP_ZAP%]" expectedSuffix = " [%APP_ZAP%]"
} }
if !strings.HasSuffix(output, expectedSuffix) { if !strings.HasSuffix(output, expectedSuffix) {
t.Errorf("%s does not end with" + expectedSuffix, output) t.Errorf("%s does not end with"+expectedSuffix, output)
} }
} }
} }
@ -404,6 +384,38 @@ func TestParseMultiStringSlice(t *testing.T) {
}).Run([]string{"run", "-s", "10", "-s", "20"}) }).Run([]string{"run", "-s", "10", "-s", "20"})
} }
func TestParseMultiStringSliceWithDefaults(t *testing.T) {
(&App{
Flags: []Flag{
StringSliceFlag{Name: "serve, s", Value: NewStringSlice("9", "2")},
},
Action: func(ctx *Context) {
if !reflect.DeepEqual(ctx.StringSlice("serve"), []string{"10", "20"}) {
t.Errorf("main name not set: %v", ctx.StringSlice("serve"))
}
if !reflect.DeepEqual(ctx.StringSlice("s"), []string{"10", "20"}) {
t.Errorf("short name not set: %v", ctx.StringSlice("s"))
}
},
}).Run([]string{"run", "-s", "10", "-s", "20"})
}
func TestParseMultiStringSliceWithDefaultsUnset(t *testing.T) {
(&App{
Flags: []Flag{
StringSliceFlag{Name: "serve, s", Value: NewStringSlice("9", "2")},
},
Action: func(ctx *Context) {
if !reflect.DeepEqual(ctx.StringSlice("serve"), []string{"9", "2"}) {
t.Errorf("main name not set: %v", ctx.StringSlice("serve"))
}
if !reflect.DeepEqual(ctx.StringSlice("s"), []string{"9", "2"}) {
t.Errorf("short name not set: %v", ctx.StringSlice("s"))
}
},
}).Run([]string{"run"})
}
func TestParseMultiStringSliceFromEnv(t *testing.T) { func TestParseMultiStringSliceFromEnv(t *testing.T) {
os.Clearenv() os.Clearenv()
os.Setenv("APP_INTERVALS", "20,30,40") os.Setenv("APP_INTERVALS", "20,30,40")
@ -423,6 +435,25 @@ func TestParseMultiStringSliceFromEnv(t *testing.T) {
}).Run([]string{"run"}) }).Run([]string{"run"})
} }
func TestParseMultiStringSliceFromEnvWithDefaults(t *testing.T) {
os.Clearenv()
os.Setenv("APP_INTERVALS", "20,30,40")
(&App{
Flags: []Flag{
StringSliceFlag{Name: "intervals, i", Value: NewStringSlice("1", "2", "5"), EnvVar: "APP_INTERVALS"},
},
Action: func(ctx *Context) {
if !reflect.DeepEqual(ctx.StringSlice("intervals"), []string{"20", "30", "40"}) {
t.Errorf("main name not set from env")
}
if !reflect.DeepEqual(ctx.StringSlice("i"), []string{"20", "30", "40"}) {
t.Errorf("short name not set from env")
}
},
}).Run([]string{"run"})
}
func TestParseMultiStringSliceFromEnvCascade(t *testing.T) { func TestParseMultiStringSliceFromEnvCascade(t *testing.T) {
os.Clearenv() os.Clearenv()
os.Setenv("APP_INTERVALS", "20,30,40") os.Setenv("APP_INTERVALS", "20,30,40")
@ -442,6 +473,25 @@ func TestParseMultiStringSliceFromEnvCascade(t *testing.T) {
}).Run([]string{"run"}) }).Run([]string{"run"})
} }
func TestParseMultiStringSliceFromEnvCascadeWithDefaults(t *testing.T) {
os.Clearenv()
os.Setenv("APP_INTERVALS", "20,30,40")
(&App{
Flags: []Flag{
StringSliceFlag{Name: "intervals, i", Value: NewStringSlice("1", "2", "5"), EnvVar: "COMPAT_INTERVALS,APP_INTERVALS"},
},
Action: func(ctx *Context) {
if !reflect.DeepEqual(ctx.StringSlice("intervals"), []string{"20", "30", "40"}) {
t.Errorf("main name not set from env")
}
if !reflect.DeepEqual(ctx.StringSlice("i"), []string{"20", "30", "40"}) {
t.Errorf("short name not set from env")
}
},
}).Run([]string{"run"})
}
func TestParseMultiInt(t *testing.T) { func TestParseMultiInt(t *testing.T) {
a := App{ a := App{
Flags: []Flag{ Flags: []Flag{
@ -531,6 +581,38 @@ func TestParseMultiIntSlice(t *testing.T) {
}).Run([]string{"run", "-s", "10", "-s", "20"}) }).Run([]string{"run", "-s", "10", "-s", "20"})
} }
func TestParseMultiIntSliceWithDefaults(t *testing.T) {
(&App{
Flags: []Flag{
IntSliceFlag{Name: "serve, s", Value: NewIntSlice(9, 2)},
},
Action: func(ctx *Context) {
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")
}
},
}).Run([]string{"run", "-s", "10", "-s", "20"})
}
func TestParseMultiIntSliceWithDefaultsUnset(t *testing.T) {
(&App{
Flags: []Flag{
IntSliceFlag{Name: "serve, s", Value: NewIntSlice(9, 2)},
},
Action: func(ctx *Context) {
if !reflect.DeepEqual(ctx.IntSlice("serve"), []int{9, 2}) {
t.Errorf("main name not set")
}
if !reflect.DeepEqual(ctx.IntSlice("s"), []int{9, 2}) {
t.Errorf("short name not set")
}
},
}).Run([]string{"run"})
}
func TestParseMultiIntSliceFromEnv(t *testing.T) { func TestParseMultiIntSliceFromEnv(t *testing.T) {
os.Clearenv() os.Clearenv()
os.Setenv("APP_INTERVALS", "20,30,40") os.Setenv("APP_INTERVALS", "20,30,40")
@ -550,6 +632,25 @@ func TestParseMultiIntSliceFromEnv(t *testing.T) {
}).Run([]string{"run"}) }).Run([]string{"run"})
} }
func TestParseMultiIntSliceFromEnvWithDefaults(t *testing.T) {
os.Clearenv()
os.Setenv("APP_INTERVALS", "20,30,40")
(&App{
Flags: []Flag{
IntSliceFlag{Name: "intervals, i", Value: NewIntSlice(1, 2, 5), EnvVar: "APP_INTERVALS"},
},
Action: func(ctx *Context) {
if !reflect.DeepEqual(ctx.IntSlice("intervals"), []int{20, 30, 40}) {
t.Errorf("main name not set from env")
}
if !reflect.DeepEqual(ctx.IntSlice("i"), []int{20, 30, 40}) {
t.Errorf("short name not set from env")
}
},
}).Run([]string{"run"})
}
func TestParseMultiIntSliceFromEnvCascade(t *testing.T) { func TestParseMultiIntSliceFromEnvCascade(t *testing.T) {
os.Clearenv() os.Clearenv()
os.Setenv("APP_INTERVALS", "20,30,40") os.Setenv("APP_INTERVALS", "20,30,40")