Skip to content

Commit

Permalink
Porting #1464 to v3
Browse files Browse the repository at this point in the history
  • Loading branch information
meatballhat committed Oct 1, 2022
1 parent d97b915 commit 629c426
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 8 deletions.
16 changes: 12 additions & 4 deletions flag-spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@ flag_types:
bool:
no_default_text: true
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:
no_default_text: true
Expand Down
2 changes: 1 addition & 1 deletion flag_int.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (f *IntFlag) GetValue() 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)
Expand Down
2 changes: 1 addition & 1 deletion flag_int64.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (f *Int64Flag) GetValue() 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)
Expand Down
12 changes: 12 additions & 0 deletions flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: .*`},

Expand All @@ -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: .*`},

Expand Down
2 changes: 1 addition & 1 deletion flag_uint.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
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)
}
Expand Down
2 changes: 1 addition & 1 deletion flag_uint64.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
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)
}
Expand Down
8 changes: 8 additions & 0 deletions godoc-current.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1131,6 +1131,8 @@ type Int64Flag struct {

Aliases []string
EnvVars []string

Base int
}
Int64Flag is a flag with type int64

Expand Down Expand Up @@ -1280,6 +1282,8 @@ type IntFlag struct {

Aliases []string
EnvVars []string

Base int
}
IntFlag is a flag with type int

Expand Down Expand Up @@ -1837,6 +1841,8 @@ type Uint64Flag struct {

Aliases []string
EnvVars []string

Base int
}
Uint64Flag is a flag with type uint64

Expand Down Expand Up @@ -1897,6 +1903,8 @@ type UintFlag struct {

Aliases []string
EnvVars []string

Base int
}
UintFlag is a flag with type uint

Expand Down
8 changes: 8 additions & 0 deletions zz_generated.flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,8 @@ type IntFlag struct {

Aliases []string
EnvVars []string

Base int
}

// String returns a readable representation of this value (for usage defaults)
Expand Down Expand Up @@ -726,6 +728,8 @@ type Int64Flag struct {

Aliases []string
EnvVars []string

Base int
}

// String returns a readable representation of this value (for usage defaults)
Expand Down Expand Up @@ -939,6 +943,8 @@ type UintFlag struct {

Aliases []string
EnvVars []string

Base int
}

// String returns a readable representation of this value (for usage defaults)
Expand Down Expand Up @@ -1012,6 +1018,8 @@ type Uint64Flag struct {

Aliases []string
EnvVars []string

Base int
}

// String returns a readable representation of this value (for usage defaults)
Expand Down

0 comments on commit 629c426

Please sign in to comment.