Ensure remaining v1 additions are v2 compatible

This commit is contained in:
Dan Buch 2016-06-17 09:54:54 -04:00
parent 6c628a1a88
commit 0c9b549918
No known key found for this signature in database
GPG Key ID: FAEF12936DD3E3EC
3 changed files with 126 additions and 82 deletions

View File

@ -36,6 +36,9 @@ before_script:
- if [ ! -f node_modules/.bin/markdown-toc ] ; then - if [ ! -f node_modules/.bin/markdown-toc ] ; then
npm install markdown-toc ; npm install markdown-toc ;
fi fi
- mkdir -p ~/gopath/src/gopkg.in/urfave
- rm -rf ~/gopath/src/gopkg.in/urfave/cli.v2
- ln -sv ~/gopath/src/github.com/urfave/cli ~/gopath/src/gopkg.in/urfave/cli.v2
script: script:
- flake8 runtests cli-v1-to-v2 - flake8 runtests cli-v1-to-v2

148
flag.go
View File

@ -219,6 +219,11 @@ func NewIntSlice(defaults ...int) *IntSlice {
return &IntSlice{slice: append([]int{}, defaults...)} return &IntSlice{slice: append([]int{}, defaults...)}
} }
// NewInt64Slice makes an *Int64Slice with default values
func NewInt64Slice(defaults ...int64) *Int64Slice {
return &Int64Slice{slice: append([]int64{}, defaults...)}
}
// SetInt directly adds an integer to the list of values // SetInt directly adds an integer to the list of values
func (i *IntSlice) SetInt(value int) { func (i *IntSlice) SetInt(value int) {
if !i.hasBeenSet { if !i.hasBeenSet {
@ -243,12 +248,12 @@ func (i *IntSlice) Set(value string) error {
return nil return nil
} }
tmp, err := strconv.Atoi(value) tmp, err := strconv.ParseInt(value, 0, 64)
if err != nil { if err != nil {
return err return err
} }
i.slice = append(i.slice, tmp) i.slice = append(i.slice, int(tmp))
return nil return nil
} }
@ -318,50 +323,72 @@ func (f *IntSliceFlag) Names() []string {
} }
// Int64Slice is an opaque type for []int to satisfy flag.Value // Int64Slice is an opaque type for []int to satisfy flag.Value
type Int64Slice []int64 type Int64Slice struct {
slice []int64
hasBeenSet bool
}
// 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 *Int64Slice) Set(value string) error { func (f *Int64Slice) Set(value string) error {
tmp, err := strconv.ParseInt(value, 10, 64) if !f.hasBeenSet {
f.slice = []int64{}
f.hasBeenSet = true
}
if strings.HasPrefix(value, slPfx) {
// Deserializing assumes overwrite
_ = json.Unmarshal([]byte(strings.Replace(value, slPfx, "", 1)), &f.slice)
f.hasBeenSet = true
return nil
}
tmp, err := strconv.ParseInt(value, 0, 64)
if err != nil { if err != nil {
return err return err
} }
*f = append(*f, tmp)
f.slice = append(f.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 *Int64Slice) String() string { func (f *Int64Slice) String() string {
return fmt.Sprintf("%#v", *f) return fmt.Sprintf("%#v", f.slice)
}
// Serialized allows Int64Slice to fulfill Serializeder
func (f *Int64Slice) Serialized() string {
jsonBytes, _ := json.Marshal(f.slice)
return fmt.Sprintf("%s%s", slPfx, string(jsonBytes))
} }
// Value returns the slice of ints set by this flag // Value returns the slice of ints set by this flag
func (f *Int64Slice) Value() []int64 { func (f *Int64Slice) Value() []int64 {
return *f return f.slice
} }
// Int64SliceFlag is an int flag that can be specified multiple times on the // Int64SliceFlag is an int flag that can be specified multiple times on the
// command-line // command-line
type Int64SliceFlag struct { type Int64SliceFlag struct {
Name string Name string
Aliases []string
Value *Int64Slice Value *Int64Slice
Usage string Usage string
EnvVar string EnvVars []string
Hidden bool Hidden bool
} }
// String returns the usage // String returns the usage
func (f Int64SliceFlag) String() string { func (f *Int64SliceFlag) String() string {
return FlagStringer(f) return FlagStringer(f)
} }
// Apply populates the flag given the flag set and environment // Apply populates the flag given the flag set and environment
func (f Int64SliceFlag) Apply(set *flag.FlagSet) { func (f *Int64SliceFlag) Apply(set *flag.FlagSet) {
if f.EnvVar != "" { if f.EnvVars != nil {
for _, envVar := range strings.Split(f.EnvVar, ",") { for _, envVar := range f.EnvVars {
envVar = strings.TrimSpace(envVar)
if envVal := os.Getenv(envVar); envVal != "" { if envVal := os.Getenv(envVar); envVal != "" {
newVal := &Int64Slice{} newVal := NewInt64Slice()
for _, s := range strings.Split(envVal, ",") { for _, s := range strings.Split(envVal, ",") {
s = strings.TrimSpace(s) s = strings.TrimSpace(s)
err := newVal.Set(s) err := newVal.Set(s)
@ -375,17 +402,18 @@ func (f Int64SliceFlag) Apply(set *flag.FlagSet) {
} }
} }
eachName(f.Name, func(name string) {
if f.Value == nil { if f.Value == nil {
f.Value = &Int64Slice{} f.Value = NewInt64Slice()
} }
for _, name := range f.Names() {
set.Var(f.Value, name, f.Usage) set.Var(f.Value, name, f.Usage)
}) }
} }
// GetName returns the name of the flag. // Names returns the names of the flag.
func (f Int64SliceFlag) GetName() string { func (f *Int64SliceFlag) Names() []string {
return f.Name return flagNames(f)
} }
// BoolFlag is a switch that defaults to false // BoolFlag is a switch that defaults to false
@ -520,23 +548,23 @@ func (f *IntFlag) Names() []string {
// Int64Flag is a flag that takes a 64-bit integer // Int64Flag is a flag that takes a 64-bit integer
type Int64Flag struct { type Int64Flag struct {
Name string Name string
Aliases []string
Value int64 Value int64
Usage string Usage string
EnvVar string EnvVars []string
Destination *int64 Destination *int64
Hidden bool Hidden bool
} }
// String returns the usage // String returns the usage
func (f Int64Flag) String() string { func (f *Int64Flag) String() string {
return FlagStringer(f) return FlagStringer(f)
} }
// Apply populates the flag given the flag set and environment // Apply populates the flag given the flag set and environment
func (f Int64Flag) Apply(set *flag.FlagSet) { func (f *Int64Flag) Apply(set *flag.FlagSet) {
if f.EnvVar != "" { if f.EnvVars != nil {
for _, envVar := range strings.Split(f.EnvVar, ",") { for _, envVar := range f.EnvVars {
envVar = strings.TrimSpace(envVar)
if envVal := os.Getenv(envVar); envVal != "" { if envVal := os.Getenv(envVar); envVal != "" {
envValInt, err := strconv.ParseInt(envVal, 0, 64) envValInt, err := strconv.ParseInt(envVal, 0, 64)
if err == nil { if err == nil {
@ -547,40 +575,40 @@ func (f Int64Flag) Apply(set *flag.FlagSet) {
} }
} }
eachName(f.Name, func(name string) { for _, name := range f.Names() {
if f.Destination != nil { if f.Destination != nil {
set.Int64Var(f.Destination, name, f.Value, f.Usage) set.Int64Var(f.Destination, name, f.Value, f.Usage)
return return
} }
set.Int64(name, f.Value, f.Usage) set.Int64(name, f.Value, f.Usage)
}) }
} }
// GetName returns the name of the flag. // Names returns the names of the flag.
func (f Int64Flag) GetName() string { func (f *Int64Flag) Names() []string {
return f.Name return flagNames(f)
} }
// UintFlag is a flag that takes an unsigned integer // UintFlag is a flag that takes an unsigned integer
type UintFlag struct { type UintFlag struct {
Name string Name string
Aliases []string
Value uint Value uint
Usage string Usage string
EnvVar string EnvVars []string
Destination *uint Destination *uint
Hidden bool Hidden bool
} }
// String returns the usage // String returns the usage
func (f UintFlag) String() string { func (f *UintFlag) String() string {
return FlagStringer(f) return FlagStringer(f)
} }
// Apply populates the flag given the flag set and environment // Apply populates the flag given the flag set and environment
func (f UintFlag) Apply(set *flag.FlagSet) { func (f *UintFlag) Apply(set *flag.FlagSet) {
if f.EnvVar != "" { if f.EnvVars != nil {
for _, envVar := range strings.Split(f.EnvVar, ",") { for _, envVar := range f.EnvVars {
envVar = strings.TrimSpace(envVar)
if envVal := os.Getenv(envVar); envVal != "" { if envVal := os.Getenv(envVar); envVal != "" {
envValInt, err := strconv.ParseUint(envVal, 0, 64) envValInt, err := strconv.ParseUint(envVal, 0, 64)
if err == nil { if err == nil {
@ -591,40 +619,40 @@ func (f UintFlag) Apply(set *flag.FlagSet) {
} }
} }
eachName(f.Name, func(name string) { for _, name := range f.Names() {
if f.Destination != nil { if f.Destination != nil {
set.UintVar(f.Destination, name, f.Value, f.Usage) set.UintVar(f.Destination, name, f.Value, f.Usage)
return return
} }
set.Uint(name, f.Value, f.Usage) set.Uint(name, f.Value, f.Usage)
}) }
} }
// GetName returns the name of the flag. // Names returns the names of the flag.
func (f UintFlag) GetName() string { func (f *UintFlag) Names() []string {
return f.Name return flagNames(f)
} }
// Uint64Flag is a flag that takes an unsigned 64-bit integer // Uint64Flag is a flag that takes an unsigned 64-bit integer
type Uint64Flag struct { type Uint64Flag struct {
Name string Name string
Aliases []string
Value uint64 Value uint64
Usage string Usage string
EnvVar string EnvVars []string
Destination *uint64 Destination *uint64
Hidden bool Hidden bool
} }
// String returns the usage // String returns the usage
func (f Uint64Flag) String() string { func (f *Uint64Flag) String() string {
return FlagStringer(f) return FlagStringer(f)
} }
// Apply populates the flag given the flag set and environment // Apply populates the flag given the flag set and environment
func (f Uint64Flag) Apply(set *flag.FlagSet) { func (f *Uint64Flag) Apply(set *flag.FlagSet) {
if f.EnvVar != "" { if f.EnvVars != nil {
for _, envVar := range strings.Split(f.EnvVar, ",") { for _, envVar := range f.EnvVars {
envVar = strings.TrimSpace(envVar)
if envVal := os.Getenv(envVar); envVal != "" { if envVal := os.Getenv(envVar); envVal != "" {
envValInt, err := strconv.ParseUint(envVal, 0, 64) envValInt, err := strconv.ParseUint(envVal, 0, 64)
if err == nil { if err == nil {
@ -635,18 +663,18 @@ func (f Uint64Flag) Apply(set *flag.FlagSet) {
} }
} }
eachName(f.Name, func(name string) { for _, name := range f.Names() {
if f.Destination != nil { if f.Destination != nil {
set.Uint64Var(f.Destination, name, f.Value, f.Usage) set.Uint64Var(f.Destination, name, f.Value, f.Usage)
return return
} }
set.Uint64(name, f.Value, f.Usage) set.Uint64(name, f.Value, f.Usage)
}) }
} }
// GetName returns the name of the flag. // Names returns the names of the flag.
func (f Uint64Flag) GetName() string { func (f *Uint64Flag) Names() []string {
return f.Name return flagNames(f)
} }
// DurationFlag is a flag that takes a duration specified in Go's duration // DurationFlag is a flag that takes a duration specified in Go's duration
@ -860,13 +888,13 @@ func stringifyFlag(f Flag) string {
switch f.(type) { switch f.(type) {
case *IntSliceFlag: case *IntSliceFlag:
return withEnvHint(fv.FieldByName("EnvVar").String(), return withEnvHint(flagStringSliceField(f, "EnvVars"),
stringifyIntSliceFlag(f.(*IntSliceFlag))) stringifyIntSliceFlag(f.(*IntSliceFlag)))
case *Int64SliceFlag: case *Int64SliceFlag:
return withEnvHint(fv.FieldByName("EnvVar").String(), return withEnvHint(flagStringSliceField(f, "EnvVars"),
stringifyInt64SliceFlag(f.(*Int64SliceFlag))) stringifyInt64SliceFlag(f.(*Int64SliceFlag)))
case *StringSliceFlag: case *StringSliceFlag:
return withEnvHint(fv.FieldByName("EnvVar").String(), return withEnvHint(flagStringSliceField(f, "EnvVars"),
stringifyStringSliceFlag(f.(*StringSliceFlag))) stringifyStringSliceFlag(f.(*StringSliceFlag)))
} }
@ -907,10 +935,10 @@ func stringifyIntSliceFlag(f *IntSliceFlag) string {
} }
} }
return stringifySliceFlag(f.Usage, append([]string{f.Name}, f.Aliases...), defaultVals) return stringifySliceFlag(f.Usage, f.Names(), defaultVals)
} }
func stringifyInt64SliceFlag(f Int64SliceFlag) string { func stringifyInt64SliceFlag(f *Int64SliceFlag) string {
defaultVals := []string{} defaultVals := []string{}
if f.Value != nil && len(f.Value.Value()) > 0 { if f.Value != nil && len(f.Value.Value()) > 0 {
for _, i := range f.Value.Value() { for _, i := range f.Value.Value() {
@ -918,7 +946,7 @@ func stringifyInt64SliceFlag(f Int64SliceFlag) string {
} }
} }
return stringifySliceFlag(f.Usage, f.Name, defaultVals) return stringifySliceFlag(f.Usage, f.Names(), defaultVals)
} }
func stringifyStringSliceFlag(f *StringSliceFlag) string { func stringifyStringSliceFlag(f *StringSliceFlag) string {
@ -931,7 +959,7 @@ func stringifyStringSliceFlag(f *StringSliceFlag) string {
} }
} }
return stringifySliceFlag(f.Usage, append([]string{f.Name}, f.Aliases...), defaultVals) return stringifySliceFlag(f.Usage, f.Names(), defaultVals)
} }
func stringifySliceFlag(usage string, names, defaultVals []string) string { func stringifySliceFlag(usage string, names, defaultVals []string) string {

View File

@ -215,7 +215,7 @@ func TestInt64FlagWithEnvVarHelpOutput(t *testing.T) {
os.Clearenv() os.Clearenv()
os.Setenv("APP_BAR", "2") os.Setenv("APP_BAR", "2")
for _, test := range int64FlagTests { for _, test := range int64FlagTests {
flag := IntFlag{Name: test.name, EnvVar: "APP_BAR"} flag := IntFlag{Name: test.name, EnvVars: []string{"APP_BAR"}}
output := flag.String() output := flag.String()
expectedSuffix := " [$APP_BAR]" expectedSuffix := " [$APP_BAR]"
@ -251,7 +251,7 @@ func TestUintFlagWithEnvVarHelpOutput(t *testing.T) {
os.Clearenv() os.Clearenv()
os.Setenv("APP_BAR", "2") os.Setenv("APP_BAR", "2")
for _, test := range uintFlagTests { for _, test := range uintFlagTests {
flag := UintFlag{Name: test.name, EnvVar: "APP_BAR"} flag := UintFlag{Name: test.name, EnvVars: []string{"APP_BAR"}}
output := flag.String() output := flag.String()
expectedSuffix := " [$APP_BAR]" expectedSuffix := " [$APP_BAR]"
@ -287,7 +287,7 @@ func TestUint64FlagWithEnvVarHelpOutput(t *testing.T) {
os.Clearenv() os.Clearenv()
os.Setenv("APP_BAR", "2") os.Setenv("APP_BAR", "2")
for _, test := range uint64FlagTests { for _, test := range uint64FlagTests {
flag := UintFlag{Name: test.name, EnvVar: "APP_BAR"} flag := UintFlag{Name: test.name, EnvVars: []string{"APP_BAR"}}
output := flag.String() output := flag.String()
expectedSuffix := " [$APP_BAR]" expectedSuffix := " [$APP_BAR]"
@ -397,22 +397,19 @@ func TestIntSliceFlagApply_SetsAllNames(t *testing.T) {
var int64SliceFlagTests = []struct { var int64SliceFlagTests = []struct {
name string name string
aliases []string
value *Int64Slice value *Int64Slice
expected string expected string
}{ }{
{"heads", &Int64Slice{}, "--heads value\t"}, {"heads", nil, NewInt64Slice(), "--heads value\t"},
{"H", &Int64Slice{}, "-H value\t"}, {"H", nil, NewInt64Slice(), "-H value\t"},
{"H, heads", func() *Int64Slice { {"heads", []string{"H"}, NewInt64Slice(int64(2), int64(17179869184)),
i := &Int64Slice{} "--heads value, -H value\t(default: 2, 17179869184)"},
i.Set("2")
i.Set("17179869184")
return i
}(), "-H value, --heads value\t(default: 2, 17179869184)"},
} }
func TestInt64SliceFlagHelpOutput(t *testing.T) { func TestInt64SliceFlagHelpOutput(t *testing.T) {
for _, test := range int64SliceFlagTests { for _, test := range int64SliceFlagTests {
flag := Int64SliceFlag{Name: test.name, Value: test.value} flag := Int64SliceFlag{Name: test.name, Aliases: test.aliases, Value: test.value}
output := flag.String() output := flag.String()
if output != test.expected { if output != test.expected {
@ -425,7 +422,7 @@ func TestInt64SliceFlagWithEnvVarHelpOutput(t *testing.T) {
os.Clearenv() os.Clearenv()
os.Setenv("APP_SMURF", "42,17179869184") os.Setenv("APP_SMURF", "42,17179869184")
for _, test := range int64SliceFlagTests { for _, test := range int64SliceFlagTests {
flag := Int64SliceFlag{Name: test.name, Value: test.value, EnvVar: "APP_SMURF"} flag := Int64SliceFlag{Name: test.name, Value: test.value, EnvVars: []string{"APP_SMURF"}}
output := flag.String() output := flag.String()
expectedSuffix := " [$APP_SMURF]" expectedSuffix := " [$APP_SMURF]"
@ -929,7 +926,7 @@ func TestParseMultiIntSliceFromEnvCascade(t *testing.T) {
func TestParseMultiInt64Slice(t *testing.T) { func TestParseMultiInt64Slice(t *testing.T) {
(&App{ (&App{
Flags: []Flag{ Flags: []Flag{
Int64SliceFlag{Name: "serve, s", Value: &Int64Slice{}}, &Int64SliceFlag{Name: "serve", Aliases: []string{"s"}, Value: NewInt64Slice()},
}, },
Action: func(ctx *Context) error { Action: func(ctx *Context) error {
if !reflect.DeepEqual(ctx.Int64Slice("serve"), []int64{10, 17179869184}) { if !reflect.DeepEqual(ctx.Int64Slice("serve"), []int64{10, 17179869184}) {
@ -949,7 +946,7 @@ func TestParseMultiInt64SliceFromEnv(t *testing.T) {
(&App{ (&App{
Flags: []Flag{ Flags: []Flag{
Int64SliceFlag{Name: "intervals, i", Value: &Int64Slice{}, EnvVar: "APP_INTERVALS"}, &Int64SliceFlag{Name: "intervals", Aliases: []string{"i"}, Value: NewInt64Slice(), EnvVars: []string{"APP_INTERVALS"}},
}, },
Action: func(ctx *Context) error { Action: func(ctx *Context) error {
if !reflect.DeepEqual(ctx.Int64Slice("intervals"), []int64{20, 30, 17179869184}) { if !reflect.DeepEqual(ctx.Int64Slice("intervals"), []int64{20, 30, 17179869184}) {
@ -969,7 +966,7 @@ func TestParseMultiInt64SliceFromEnvCascade(t *testing.T) {
(&App{ (&App{
Flags: []Flag{ Flags: []Flag{
Int64SliceFlag{Name: "intervals, i", Value: &Int64Slice{}, EnvVar: "COMPAT_INTERVALS,APP_INTERVALS"}, &Int64SliceFlag{Name: "intervals", Aliases: []string{"i"}, Value: NewInt64Slice(), EnvVars: []string{"COMPAT_INTERVALS", "APP_INTERVALS"}},
}, },
Action: func(ctx *Context) error { Action: func(ctx *Context) error {
if !reflect.DeepEqual(ctx.Int64Slice("intervals"), []int64{20, 30, 17179869184}) { if !reflect.DeepEqual(ctx.Int64Slice("intervals"), []int64{20, 30, 17179869184}) {
@ -1339,3 +1336,19 @@ func TestIntSlice_Serialized_Set(t *testing.T) {
t.Fatalf("pre and post serialization do not match: %v != %v", sl0, sl1) t.Fatalf("pre and post serialization do not match: %v != %v", sl0, sl1)
} }
} }
func TestInt64Slice_Serialized_Set(t *testing.T) {
sl0 := NewInt64Slice(int64(1), int64(2))
ser0 := sl0.Serialized()
if len(ser0) < len(slPfx) {
t.Fatalf("serialized shorter than expected: %q", ser0)
}
sl1 := NewInt64Slice(int64(3), int64(4))
sl1.Set(ser0)
if sl0.String() != sl1.String() {
t.Fatalf("pre and post serialization do not match: %v != %v", sl0, sl1)
}
}