diff --git a/.travis.yml b/.travis.yml index 3225cc1..84c3828 100644 --- a/.travis.yml +++ b/.travis.yml @@ -36,6 +36,9 @@ before_script: - if [ ! -f node_modules/.bin/markdown-toc ] ; then npm install markdown-toc ; 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: - flake8 runtests cli-v1-to-v2 diff --git a/flag.go b/flag.go index 2e6caf4..3ff53ce 100644 --- a/flag.go +++ b/flag.go @@ -219,6 +219,11 @@ func NewIntSlice(defaults ...int) *IntSlice { 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 func (i *IntSlice) SetInt(value int) { if !i.hasBeenSet { @@ -243,12 +248,12 @@ func (i *IntSlice) Set(value string) error { return nil } - tmp, err := strconv.Atoi(value) + tmp, err := strconv.ParseInt(value, 0, 64) if err != nil { return err } - i.slice = append(i.slice, tmp) + i.slice = append(i.slice, int(tmp)) return nil } @@ -318,50 +323,72 @@ func (f *IntSliceFlag) Names() []string { } // 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 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 { return err } - *f = append(*f, tmp) + + f.slice = append(f.slice, tmp) return nil } // String returns a readable representation of this value (for usage defaults) 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 func (f *Int64Slice) Value() []int64 { - return *f + return f.slice } // Int64SliceFlag is an int flag that can be specified multiple times on the // command-line type Int64SliceFlag struct { - Name string - Value *Int64Slice - Usage string - EnvVar string - Hidden bool + Name string + Aliases []string + Value *Int64Slice + Usage string + EnvVars []string + Hidden bool } // String returns the usage -func (f Int64SliceFlag) String() string { +func (f *Int64SliceFlag) String() string { return FlagStringer(f) } // Apply populates the flag given the flag set and environment -func (f Int64SliceFlag) Apply(set *flag.FlagSet) { - if f.EnvVar != "" { - for _, envVar := range strings.Split(f.EnvVar, ",") { - envVar = strings.TrimSpace(envVar) +func (f *Int64SliceFlag) Apply(set *flag.FlagSet) { + if f.EnvVars != nil { + for _, envVar := range f.EnvVars { if envVal := os.Getenv(envVar); envVal != "" { - newVal := &Int64Slice{} + newVal := NewInt64Slice() for _, s := range strings.Split(envVal, ",") { s = strings.TrimSpace(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 { - f.Value = &Int64Slice{} - } + if f.Value == nil { + f.Value = NewInt64Slice() + } + + for _, name := range f.Names() { set.Var(f.Value, name, f.Usage) - }) + } } -// GetName returns the name of the flag. -func (f Int64SliceFlag) GetName() string { - return f.Name +// Names returns the names of the flag. +func (f *Int64SliceFlag) Names() []string { + return flagNames(f) } // 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 type Int64Flag struct { Name string + Aliases []string Value int64 Usage string - EnvVar string + EnvVars []string Destination *int64 Hidden bool } // String returns the usage -func (f Int64Flag) String() string { +func (f *Int64Flag) String() string { return FlagStringer(f) } // Apply populates the flag given the flag set and environment -func (f Int64Flag) Apply(set *flag.FlagSet) { - if f.EnvVar != "" { - for _, envVar := range strings.Split(f.EnvVar, ",") { - envVar = strings.TrimSpace(envVar) +func (f *Int64Flag) Apply(set *flag.FlagSet) { + if f.EnvVars != nil { + for _, envVar := range f.EnvVars { if envVal := os.Getenv(envVar); envVal != "" { envValInt, err := strconv.ParseInt(envVal, 0, 64) 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 { set.Int64Var(f.Destination, name, f.Value, f.Usage) return } set.Int64(name, f.Value, f.Usage) - }) + } } -// GetName returns the name of the flag. -func (f Int64Flag) GetName() string { - return f.Name +// Names returns the names of the flag. +func (f *Int64Flag) Names() []string { + return flagNames(f) } // UintFlag is a flag that takes an unsigned integer type UintFlag struct { Name string + Aliases []string Value uint Usage string - EnvVar string + EnvVars []string Destination *uint Hidden bool } // String returns the usage -func (f UintFlag) String() string { +func (f *UintFlag) String() string { return FlagStringer(f) } // Apply populates the flag given the flag set and environment -func (f UintFlag) Apply(set *flag.FlagSet) { - if f.EnvVar != "" { - for _, envVar := range strings.Split(f.EnvVar, ",") { - envVar = strings.TrimSpace(envVar) +func (f *UintFlag) Apply(set *flag.FlagSet) { + if f.EnvVars != nil { + for _, envVar := range f.EnvVars { if envVal := os.Getenv(envVar); envVal != "" { envValInt, err := strconv.ParseUint(envVal, 0, 64) 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 { set.UintVar(f.Destination, name, f.Value, f.Usage) return } set.Uint(name, f.Value, f.Usage) - }) + } } -// GetName returns the name of the flag. -func (f UintFlag) GetName() string { - return f.Name +// Names returns the names of the flag. +func (f *UintFlag) Names() []string { + return flagNames(f) } // Uint64Flag is a flag that takes an unsigned 64-bit integer type Uint64Flag struct { Name string + Aliases []string Value uint64 Usage string - EnvVar string + EnvVars []string Destination *uint64 Hidden bool } // String returns the usage -func (f Uint64Flag) String() string { +func (f *Uint64Flag) String() string { return FlagStringer(f) } // Apply populates the flag given the flag set and environment -func (f Uint64Flag) Apply(set *flag.FlagSet) { - if f.EnvVar != "" { - for _, envVar := range strings.Split(f.EnvVar, ",") { - envVar = strings.TrimSpace(envVar) +func (f *Uint64Flag) Apply(set *flag.FlagSet) { + if f.EnvVars != nil { + for _, envVar := range f.EnvVars { if envVal := os.Getenv(envVar); envVal != "" { envValInt, err := strconv.ParseUint(envVal, 0, 64) 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 { set.Uint64Var(f.Destination, name, f.Value, f.Usage) return } set.Uint64(name, f.Value, f.Usage) - }) + } } -// GetName returns the name of the flag. -func (f Uint64Flag) GetName() string { - return f.Name +// Names returns the names of the flag. +func (f *Uint64Flag) Names() []string { + return flagNames(f) } // 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) { case *IntSliceFlag: - return withEnvHint(fv.FieldByName("EnvVar").String(), + return withEnvHint(flagStringSliceField(f, "EnvVars"), stringifyIntSliceFlag(f.(*IntSliceFlag))) case *Int64SliceFlag: - return withEnvHint(fv.FieldByName("EnvVar").String(), + return withEnvHint(flagStringSliceField(f, "EnvVars"), stringifyInt64SliceFlag(f.(*Int64SliceFlag))) case *StringSliceFlag: - return withEnvHint(fv.FieldByName("EnvVar").String(), + return withEnvHint(flagStringSliceField(f, "EnvVars"), 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{} if f.Value != nil && len(f.Value.Value()) > 0 { 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 { @@ -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 { diff --git a/flag_test.go b/flag_test.go index c82f4d3..1b69f51 100644 --- a/flag_test.go +++ b/flag_test.go @@ -215,7 +215,7 @@ func TestInt64FlagWithEnvVarHelpOutput(t *testing.T) { os.Clearenv() os.Setenv("APP_BAR", "2") for _, test := range int64FlagTests { - flag := IntFlag{Name: test.name, EnvVar: "APP_BAR"} + flag := IntFlag{Name: test.name, EnvVars: []string{"APP_BAR"}} output := flag.String() expectedSuffix := " [$APP_BAR]" @@ -251,7 +251,7 @@ func TestUintFlagWithEnvVarHelpOutput(t *testing.T) { os.Clearenv() os.Setenv("APP_BAR", "2") for _, test := range uintFlagTests { - flag := UintFlag{Name: test.name, EnvVar: "APP_BAR"} + flag := UintFlag{Name: test.name, EnvVars: []string{"APP_BAR"}} output := flag.String() expectedSuffix := " [$APP_BAR]" @@ -287,7 +287,7 @@ func TestUint64FlagWithEnvVarHelpOutput(t *testing.T) { os.Clearenv() os.Setenv("APP_BAR", "2") for _, test := range uint64FlagTests { - flag := UintFlag{Name: test.name, EnvVar: "APP_BAR"} + flag := UintFlag{Name: test.name, EnvVars: []string{"APP_BAR"}} output := flag.String() expectedSuffix := " [$APP_BAR]" @@ -397,22 +397,19 @@ func TestIntSliceFlagApply_SetsAllNames(t *testing.T) { var int64SliceFlagTests = []struct { name string + aliases []string value *Int64Slice expected string }{ - {"heads", &Int64Slice{}, "--heads value\t"}, - {"H", &Int64Slice{}, "-H value\t"}, - {"H, heads", func() *Int64Slice { - i := &Int64Slice{} - i.Set("2") - i.Set("17179869184") - return i - }(), "-H value, --heads value\t(default: 2, 17179869184)"}, + {"heads", nil, NewInt64Slice(), "--heads value\t"}, + {"H", nil, NewInt64Slice(), "-H value\t"}, + {"heads", []string{"H"}, NewInt64Slice(int64(2), int64(17179869184)), + "--heads value, -H value\t(default: 2, 17179869184)"}, } func TestInt64SliceFlagHelpOutput(t *testing.T) { 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() if output != test.expected { @@ -425,7 +422,7 @@ func TestInt64SliceFlagWithEnvVarHelpOutput(t *testing.T) { os.Clearenv() os.Setenv("APP_SMURF", "42,17179869184") 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() expectedSuffix := " [$APP_SMURF]" @@ -929,7 +926,7 @@ func TestParseMultiIntSliceFromEnvCascade(t *testing.T) { func TestParseMultiInt64Slice(t *testing.T) { (&App{ Flags: []Flag{ - Int64SliceFlag{Name: "serve, s", Value: &Int64Slice{}}, + &Int64SliceFlag{Name: "serve", Aliases: []string{"s"}, Value: NewInt64Slice()}, }, Action: func(ctx *Context) error { if !reflect.DeepEqual(ctx.Int64Slice("serve"), []int64{10, 17179869184}) { @@ -949,7 +946,7 @@ func TestParseMultiInt64SliceFromEnv(t *testing.T) { (&App{ 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 { if !reflect.DeepEqual(ctx.Int64Slice("intervals"), []int64{20, 30, 17179869184}) { @@ -969,7 +966,7 @@ func TestParseMultiInt64SliceFromEnvCascade(t *testing.T) { (&App{ 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 { 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) } } + +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) + } +}