diff --git a/flag-spec.yaml b/flag-spec.yaml index b1a0735f15..1c594d7231 100644 --- a/flag-spec.yaml +++ b/flag-spec.yaml @@ -5,11 +5,19 @@ flag_types: bool: {} float64: {} - int64: {} - int: {} + int64: + struct_fields: + - { name: Base, type: int } + int: + struct_fields: + - { name: Base, type: int } time.Duration: {} - uint64: {} - uint: {} + uint64: + struct_fields: + - { name: Base, type: int } + uint: + struct_fields: + - { name: Base, type: int } string: struct_fields: diff --git a/flag_int.go b/flag_int.go index c70b889858..0f5c403b3d 100644 --- a/flag_int.go +++ b/flag_int.go @@ -44,7 +44,7 @@ func (f *IntFlag) GetEnvVars() []string { func (f *IntFlag) Apply(set *flag.FlagSet) error { if val, source, found := flagFromEnvOrFile(f.EnvVars, f.FilePath); found { if val != "" { - valInt, err := strconv.ParseInt(val, 0, 64) + valInt, err := strconv.ParseInt(val, f.Base, 64) if err != nil { return fmt.Errorf("could not parse %q as int value from %s for flag %s: %s", val, source, f.Name, err) diff --git a/flag_int64.go b/flag_int64.go index 5e7038cfb1..a392275def 100644 --- a/flag_int64.go +++ b/flag_int64.go @@ -44,7 +44,7 @@ func (f *Int64Flag) GetEnvVars() []string { func (f *Int64Flag) Apply(set *flag.FlagSet) error { if val, source, found := flagFromEnvOrFile(f.EnvVars, f.FilePath); found { if val != "" { - valInt, err := strconv.ParseInt(val, 0, 64) + valInt, err := strconv.ParseInt(val, f.Base, 64) if err != nil { return fmt.Errorf("could not parse %q as int value from %s for flag %s: %s", val, source, f.Name, err) diff --git a/flag_test.go b/flag_test.go index d3ded729bb..456d1b8143 100644 --- a/flag_test.go +++ b/flag_test.go @@ -110,6 +110,10 @@ func TestFlagsFromEnv(t *testing.T) { {"foobar", 0, &Int64Flag{Name: "seconds", EnvVars: []string{"SECONDS"}}, `could not parse "foobar" as int 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: .*`}, {"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: .*`}, @@ -130,10 +134,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: .*`}, {"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: .*`}, {"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: .*`}, @@ -2398,43 +2410,43 @@ func TestFlagDefaultValue(t *testing.T) { name: "stringSclice", flag: &StringSliceFlag{Name: "flag", Value: NewStringSlice("default1", "default2")}, toParse: []string{"--flag", "parsed"}, - expect: `--flag value [ --flag value ] (default: "default1", "default2")`, + expect: `--flag value [ --flag value ] (default: "default1", "default2")`, }, { name: "float64Sclice", flag: &Float64SliceFlag{Name: "flag", Value: NewFloat64Slice(1.1, 2.2)}, toParse: []string{"--flag", "13.3"}, - expect: `--flag value [ --flag value ] (default: 1.1, 2.2)`, + expect: `--flag value [ --flag value ] (default: 1.1, 2.2)`, }, { name: "int64Sclice", flag: &Int64SliceFlag{Name: "flag", Value: NewInt64Slice(1, 2)}, toParse: []string{"--flag", "13"}, - expect: `--flag value [ --flag value ] (default: 1, 2)`, + expect: `--flag value [ --flag value ] (default: 1, 2)`, }, { name: "intSclice", flag: &IntSliceFlag{Name: "flag", Value: NewIntSlice(1, 2)}, toParse: []string{"--flag", "13"}, - expect: `--flag value [ --flag value ] (default: 1, 2)`, + expect: `--flag value [ --flag value ] (default: 1, 2)`, }, { name: "string", flag: &StringFlag{Name: "flag", Value: "default"}, toParse: []string{"--flag", "parsed"}, - expect: `--flag value (default: "default")`, + expect: `--flag value (default: "default")`, }, { name: "bool", flag: &BoolFlag{Name: "flag", Value: true}, toParse: []string{"--flag", "false"}, - expect: `--flag (default: true)`, + expect: `--flag (default: true)`, }, { name: "uint64", flag: &Uint64Flag{Name: "flag", Value: 1}, toParse: []string{"--flag", "13"}, - expect: `--flag value (default: 1)`, + expect: `--flag value (default: 1)`, }, } for i, v := range cases { diff --git a/flag_uint.go b/flag_uint.go index 6092b1ad69..d25ff73ad7 100644 --- a/flag_uint.go +++ b/flag_uint.go @@ -25,7 +25,7 @@ func (f *UintFlag) GetCategory() string { func (f *UintFlag) Apply(set *flag.FlagSet) error { if val, source, found := flagFromEnvOrFile(f.EnvVars, f.FilePath); found { if val != "" { - valInt, err := strconv.ParseUint(val, 0, 64) + valInt, err := strconv.ParseUint(val, f.Base, 64) if err != nil { return fmt.Errorf("could not parse %q as uint value from %s for flag %s: %s", val, source, f.Name, err) } diff --git a/flag_uint64.go b/flag_uint64.go index a37f30d9fc..975c73393b 100644 --- a/flag_uint64.go +++ b/flag_uint64.go @@ -25,7 +25,7 @@ func (f *Uint64Flag) GetCategory() string { func (f *Uint64Flag) Apply(set *flag.FlagSet) error { if val, source, found := flagFromEnvOrFile(f.EnvVars, f.FilePath); found { if val != "" { - valInt, err := strconv.ParseUint(val, 0, 64) + valInt, err := strconv.ParseUint(val, f.Base, 64) if err != nil { return fmt.Errorf("could not parse %q as uint64 value from %s for flag %s: %s", val, source, f.Name, err) } diff --git a/godoc-current.txt b/godoc-current.txt index 6d0a908753..a6a3887ecf 100644 --- a/godoc-current.txt +++ b/godoc-current.txt @@ -1131,6 +1131,8 @@ type Int64Flag struct { Aliases []string EnvVars []string + + Base int } Int64Flag is a flag with type int64 @@ -1280,6 +1282,8 @@ type IntFlag struct { Aliases []string EnvVars []string + + Base int } IntFlag is a flag with type int @@ -1850,6 +1854,8 @@ type Uint64Flag struct { Aliases []string EnvVars []string + + Base int } Uint64Flag is a flag with type uint64 @@ -1910,6 +1916,8 @@ type UintFlag struct { Aliases []string EnvVars []string + + Base int } UintFlag is a flag with type uint diff --git a/zz_generated.flags.go b/zz_generated.flags.go index b89566f900..f5b73ba245 100644 --- a/zz_generated.flags.go +++ b/zz_generated.flags.go @@ -417,6 +417,8 @@ type IntFlag struct { Aliases []string EnvVars []string + + Base int } // String returns a readable representation of this value (for usage defaults) @@ -462,6 +464,8 @@ type Int64Flag struct { Aliases []string EnvVars []string + + Base int } // String returns a readable representation of this value (for usage defaults) @@ -599,6 +603,8 @@ type UintFlag struct { Aliases []string EnvVars []string + + Base int } // String returns a readable representation of this value (for usage defaults) @@ -644,6 +650,8 @@ type Uint64Flag struct { Aliases []string EnvVars []string + + Base int } // String returns a readable representation of this value (for usage defaults)