diff --git a/flag_impl.go b/flag_impl.go index c20879b637..9ba49fba5c 100644 --- a/flag_impl.go +++ b/flag_impl.go @@ -7,7 +7,7 @@ import ( ) type FlagConfig interface { - IntBase() int + GetNumberBase() int GetCount() *int } @@ -15,7 +15,11 @@ type ValueCreator[T any] interface { Create(T, *T, FlagConfig) flag.Value } -// Float64Flag is a flag with type float64 +// FlagBase[T,VC] is a generic flag base which can be used +// as a boilerplate to implement the most common interfaces +// used by urfave/cli. T specifies the types and VC specifies +// a value creator which can be used to get the correct flag.Value +// for that type type FlagBase[T any, VC ValueCreator[T]] struct { Name string @@ -34,9 +38,9 @@ type FlagBase[T any, VC ValueCreator[T]] struct { Aliases []string EnvVars []string - TakesFile bool - Base int - Count *int + TakesFile bool + NumberBase int + Count *int Action func(*Context, T) error @@ -46,8 +50,8 @@ type FlagBase[T any, VC ValueCreator[T]] struct { value flag.Value } -func (f *FlagBase[T, V]) IntBase() int { - return f.Base +func (f *FlagBase[T, V]) GetNumberBase() int { + return f.NumberBase } func (f *FlagBase[T, V]) GetCount() *int { diff --git a/flag_int.go b/flag_int.go index 89556fb67b..aaa2056089 100644 --- a/flag_int.go +++ b/flag_int.go @@ -15,7 +15,7 @@ func (i intValue) Create(val int, p *int, c FlagConfig) flag.Value { *p = val return &intValue{ val: p, - base: c.IntBase(), + base: c.GetNumberBase(), } } diff --git a/flag_int64.go b/flag_int64.go index 602e043e9c..477680eabb 100644 --- a/flag_int64.go +++ b/flag_int64.go @@ -15,7 +15,7 @@ func (i int64Value) Create(val int64, p *int64, c FlagConfig) flag.Value { *p = val return &int64Value{ val: p, - base: c.IntBase(), + base: c.GetNumberBase(), } } diff --git a/flag_test.go b/flag_test.go index cf7b45b931..2cf7200498 100644 --- a/flag_test.go +++ b/flag_test.go @@ -167,10 +167,10 @@ func TestFlagsFromEnv(t *testing.T) { {"foobar", 0, &Int64Flag{Name: "seconds", EnvVars: []string{"SECONDS"}}, `could not parse "foobar" as int64 value from environment variable "SECONDS" for flag seconds: .*`}, {"1", 1, &IntFlag{Name: "seconds", EnvVars: []string{"SECONDS"}}, ""}, - {"08", 8, &IntFlag{Name: "seconds", EnvVars: []string{"SECONDS"}, Base: 10}, ""}, - {"755", 493, &IntFlag{Name: "seconds", EnvVars: []string{"SECONDS"}, Base: 8}, ""}, - {"deadBEEF", 3735928559, &IntFlag{Name: "seconds", EnvVars: []string{"SECONDS"}, Base: 16}, ""}, - {"08", 0, &IntFlag{Name: "seconds", EnvVars: []string{"SECONDS"}, Base: 0}, `could not parse "08" as int value from environment variable "SECONDS" for flag seconds: .*`}, + {"08", 8, &IntFlag{Name: "seconds", EnvVars: []string{"SECONDS"}, NumberBase: 10}, ""}, + {"755", 493, &IntFlag{Name: "seconds", EnvVars: []string{"SECONDS"}, NumberBase: 8}, ""}, + {"deadBEEF", 3735928559, &IntFlag{Name: "seconds", EnvVars: []string{"SECONDS"}, NumberBase: 16}, ""}, + {"08", 0, &IntFlag{Name: "seconds", EnvVars: []string{"SECONDS"}, NumberBase: 0}, `could not parse "08" as int value from environment variable "SECONDS" for flag seconds: .*`}, {"1.2", 0, &IntFlag{Name: "seconds", EnvVars: []string{"SECONDS"}}, `could not parse "1.2" as int value from environment variable "SECONDS" for flag seconds: .*`}, {"foobar", 0, &IntFlag{Name: "seconds", EnvVars: []string{"SECONDS"}}, `could not parse "foobar" as int value from environment variable "SECONDS" for flag seconds: .*`}, @@ -199,18 +199,18 @@ func TestFlagsFromEnv(t *testing.T) { {"foo,bar", newSetStringSlice("foo", "bar"), &StringSliceFlag{Name: "names", EnvVars: []string{"NAMES"}}, ""}, {"1", uint(1), &UintFlag{Name: "seconds", EnvVars: []string{"SECONDS"}}, ""}, - {"08", uint(8), &UintFlag{Name: "seconds", EnvVars: []string{"SECONDS"}, Base: 10}, ""}, - {"755", uint(493), &UintFlag{Name: "seconds", EnvVars: []string{"SECONDS"}, Base: 8}, ""}, - {"deadBEEF", uint(3735928559), &UintFlag{Name: "seconds", EnvVars: []string{"SECONDS"}, Base: 16}, ""}, - {"08", 0, &UintFlag{Name: "seconds", EnvVars: []string{"SECONDS"}, Base: 0}, `could not parse "08" as uint value from environment variable "SECONDS" for flag seconds: .*`}, + {"08", uint(8), &UintFlag{Name: "seconds", EnvVars: []string{"SECONDS"}, NumberBase: 10}, ""}, + {"755", uint(493), &UintFlag{Name: "seconds", EnvVars: []string{"SECONDS"}, NumberBase: 8}, ""}, + {"deadBEEF", uint(3735928559), &UintFlag{Name: "seconds", EnvVars: []string{"SECONDS"}, NumberBase: 16}, ""}, + {"08", 0, &UintFlag{Name: "seconds", EnvVars: []string{"SECONDS"}, NumberBase: 0}, `could not parse "08" as uint value from environment variable "SECONDS" for flag seconds: .*`}, {"1.2", 0, &UintFlag{Name: "seconds", EnvVars: []string{"SECONDS"}}, `could not parse "1.2" as uint value from environment variable "SECONDS" for flag seconds: .*`}, {"foobar", 0, &UintFlag{Name: "seconds", EnvVars: []string{"SECONDS"}}, `could not parse "foobar" as uint value from environment variable "SECONDS" for flag seconds: .*`}, {"1", uint64(1), &Uint64Flag{Name: "seconds", EnvVars: []string{"SECONDS"}}, ""}, - {"08", uint64(8), &Uint64Flag{Name: "seconds", EnvVars: []string{"SECONDS"}, Base: 10}, ""}, - {"755", uint64(493), &Uint64Flag{Name: "seconds", EnvVars: []string{"SECONDS"}, Base: 8}, ""}, - {"deadBEEF", uint64(3735928559), &Uint64Flag{Name: "seconds", EnvVars: []string{"SECONDS"}, Base: 16}, ""}, - {"08", 0, &Uint64Flag{Name: "seconds", EnvVars: []string{"SECONDS"}, Base: 0}, `could not parse "08" as uint64 value from environment variable "SECONDS" for flag seconds: .*`}, + {"08", uint64(8), &Uint64Flag{Name: "seconds", EnvVars: []string{"SECONDS"}, NumberBase: 10}, ""}, + {"755", uint64(493), &Uint64Flag{Name: "seconds", EnvVars: []string{"SECONDS"}, NumberBase: 8}, ""}, + {"deadBEEF", uint64(3735928559), &Uint64Flag{Name: "seconds", EnvVars: []string{"SECONDS"}, NumberBase: 16}, ""}, + {"08", 0, &Uint64Flag{Name: "seconds", EnvVars: []string{"SECONDS"}, NumberBase: 0}, `could not parse "08" as uint64 value from environment variable "SECONDS" for flag seconds: .*`}, {"1.2", 0, &Uint64Flag{Name: "seconds", EnvVars: []string{"SECONDS"}}, `could not parse "1.2" as uint64 value from environment variable "SECONDS" for flag seconds: .*`}, {"foobar", 0, &Uint64Flag{Name: "seconds", EnvVars: []string{"SECONDS"}}, `could not parse "foobar" as uint64 value from environment variable "SECONDS" for flag seconds: .*`}, diff --git a/flag_uint.go b/flag_uint.go index cb8c0964aa..de3fa72c2c 100644 --- a/flag_uint.go +++ b/flag_uint.go @@ -15,7 +15,7 @@ func (i uintValue) Create(val uint, p *uint, c FlagConfig) flag.Value { *p = val return &uintValue{ val: p, - base: c.IntBase(), + base: c.GetNumberBase(), } } diff --git a/flag_uint64.go b/flag_uint64.go index d44fd079bf..3adc44ded6 100644 --- a/flag_uint64.go +++ b/flag_uint64.go @@ -15,7 +15,7 @@ func (i uint64Value) Create(val uint64, p *uint64, c FlagConfig) flag.Value { *p = val return &uint64Value{ val: p, - base: c.IntBase(), + base: c.GetNumberBase(), } } diff --git a/godoc-current.txt b/godoc-current.txt index 3b7b75fa7e..456753031c 100644 --- a/godoc-current.txt +++ b/godoc-current.txt @@ -784,15 +784,18 @@ type FlagBase[T any, VC ValueCreator[T]] struct { Aliases []string EnvVars []string - TakesFile bool - Base int - Count *int + TakesFile bool + NumberBase int + Count *int Action func(*Context, T) error // Has unexported fields. } - Float64Flag is a flag with type float64 + FlagBase[T,VC] is a generic flag base which can be used as a boilerplate + to implement the most common interfaces used by urfave/cli. T specifies the + types and VC specifies a value creator which can be used to get the correct + flag.Value for that type func (f *FlagBase[T, V]) Apply(set *flag.FlagSet) error Apply populates the flag given the flag set and environment @@ -811,6 +814,8 @@ func (f *FlagBase[T, V]) GetDefaultText() string func (f *FlagBase[T, V]) GetEnvVars() []string GetEnvVars returns the env vars for this flag +func (f *FlagBase[T, V]) GetNumberBase() int + func (f *FlagBase[T, V]) GetUsage() string GetUsage returns the usage string for the flag @@ -818,8 +823,6 @@ func (f *FlagBase[T, V]) GetValue() string GetValue returns the flags value as string representation and an empty string if the flag takes no value at all. -func (f *FlagBase[T, V]) IntBase() int - func (f *FlagBase[T, V]) IsRequired() bool IsRequired returns whether or not the flag is required @@ -850,7 +853,7 @@ type FlagCategories interface { FlagCategories interface allows for category manipulation type FlagConfig interface { - IntBase() int + GetNumberBase() int GetCount() *int } @@ -955,6 +958,8 @@ func (f *Float64SliceFlag) GetCategory() string func (f *Float64SliceFlag) GetDefaultText() string GetDefaultText returns the default text for this flag +func (f *Float64SliceFlag) GetDestination() []float64 + func (f *Float64SliceFlag) GetEnvVars() []string GetEnvVars returns the env vars for this flag @@ -983,6 +988,10 @@ func (f *Float64SliceFlag) Names() []string func (f *Float64SliceFlag) RunAction(c *Context) error RunAction executes flag action if set +func (f *Float64SliceFlag) SetDestination(slice []float64) + +func (f *Float64SliceFlag) SetValue(slice []float64) + func (f *Float64SliceFlag) String() string String returns a readable representation of this value (for usage defaults) diff --git a/sliceflag.go b/sliceflag.go index 7d4632cd33..105b031837 100644 --- a/sliceflag.go +++ b/sliceflag.go @@ -229,7 +229,7 @@ func unwrapFlagValue(v flag.Value) flag.Value { // NOTE: the methods below are in this file to make use of the build constraint -/*func (f *Float64SliceFlag) SetValue(slice []float64) { +func (f *Float64SliceFlag) SetValue(slice []float64) { f.Value = newSliceFlagValue(NewFloat64Slice, slice) } @@ -242,7 +242,7 @@ func (f *Float64SliceFlag) GetDestination() []float64 { return destination.Value() } return nil -}*/ +} func (f *Int64SliceFlag) SetValue(slice []int64) { f.Value = newSliceFlagValue(NewInt64Slice, slice)