diff --git a/cmd/urfave-cli-genflags/generated.gotmpl b/cmd/urfave-cli-genflags/generated.gotmpl index dfa5e3c4f0..150c68eb82 100644 --- a/cmd/urfave-cli-genflags/generated.gotmpl +++ b/cmd/urfave-cli-genflags/generated.gotmpl @@ -23,6 +23,7 @@ type {{.TypeName}} struct { EnvVars []string defaultValue {{if .ValuePointer}}*{{end}}{{.GoType}} + defaultValueSet bool {{ range .StructFields}} {{.Name}} {{if .Pointer}}*{{end}}{{.Type}} {{end -}} diff --git a/flag_bool.go b/flag_bool.go index 369d18b77f..01862ea764 100644 --- a/flag_bool.go +++ b/flag_bool.go @@ -84,7 +84,10 @@ func (f *BoolFlag) GetDefaultText() string { if f.DefaultText != "" { return f.DefaultText } - return fmt.Sprintf("%v", f.defaultValue) + if f.defaultValueSet { + return fmt.Sprintf("%v", f.defaultValue) + } + return fmt.Sprintf("%v", f.Value) } // GetEnvVars returns the env vars for this flag @@ -105,6 +108,7 @@ func (f *BoolFlag) RunAction(c *Context) error { func (f *BoolFlag) Apply(set *flag.FlagSet) error { // set default value so that environment wont be able to overwrite it f.defaultValue = f.Value + f.defaultValueSet = true if val, source, found := flagFromEnvOrFile(f.EnvVars, f.FilePath); found { if val != "" { diff --git a/flag_duration.go b/flag_duration.go index eac4cb8332..e600cc30ad 100644 --- a/flag_duration.go +++ b/flag_duration.go @@ -32,7 +32,10 @@ func (f *DurationFlag) GetDefaultText() string { if f.DefaultText != "" { return f.DefaultText } - return f.defaultValue.String() + if f.defaultValueSet { + return f.defaultValue.String() + } + return f.Value.String() } // GetEnvVars returns the env vars for this flag @@ -44,6 +47,7 @@ func (f *DurationFlag) GetEnvVars() []string { func (f *DurationFlag) Apply(set *flag.FlagSet) error { // set default value so that environment wont be able to overwrite it f.defaultValue = f.Value + f.defaultValueSet = true if val, source, found := flagFromEnvOrFile(f.EnvVars, f.FilePath); found { if val != "" { diff --git a/flag_float64.go b/flag_float64.go index bce26c1958..6a4de5c88b 100644 --- a/flag_float64.go +++ b/flag_float64.go @@ -32,7 +32,10 @@ func (f *Float64Flag) GetDefaultText() string { if f.DefaultText != "" { return f.DefaultText } - return f.GetValue() + if f.defaultValueSet { + return fmt.Sprintf("%v", f.defaultValue) + } + return fmt.Sprintf("%v", f.Value) } // GetEnvVars returns the env vars for this flag @@ -42,6 +45,9 @@ func (f *Float64Flag) GetEnvVars() []string { // Apply populates the flag given the flag set and environment func (f *Float64Flag) Apply(set *flag.FlagSet) error { + f.defaultValue = f.Value + f.defaultValueSet = true + if val, source, found := flagFromEnvOrFile(f.EnvVars, f.FilePath); found { if val != "" { valFloat, err := strconv.ParseFloat(val, 64) diff --git a/flag_generic.go b/flag_generic.go index 039ffdfeee..7528c934cd 100644 --- a/flag_generic.go +++ b/flag_generic.go @@ -53,9 +53,15 @@ func (f *GenericFlag) GetDefaultText() string { if f.DefaultText != "" { return f.DefaultText } - if f.defaultValue != nil { - return f.defaultValue.String() + val := f.Value + if f.defaultValueSet { + val = f.defaultValue } + + if val != nil { + return val.String() + } + return "" } @@ -70,6 +76,7 @@ func (f *GenericFlag) Apply(set *flag.FlagSet) error { // set default value so that environment wont be able to overwrite it if f.Value != nil { f.defaultValue = &stringGeneric{value: f.Value.String()} + f.defaultValueSet = true } if val, source, found := flagFromEnvOrFile(f.EnvVars, f.FilePath); found { diff --git a/flag_int.go b/flag_int.go index d681270a23..750e7ebfc8 100644 --- a/flag_int.go +++ b/flag_int.go @@ -32,7 +32,10 @@ func (f *IntFlag) GetDefaultText() string { if f.DefaultText != "" { return f.DefaultText } - return fmt.Sprintf("%d", f.defaultValue) + if f.defaultValueSet { + return fmt.Sprintf("%d", f.defaultValue) + } + return fmt.Sprintf("%d", f.Value) } // GetEnvVars returns the env vars for this flag @@ -44,6 +47,7 @@ func (f *IntFlag) GetEnvVars() []string { func (f *IntFlag) Apply(set *flag.FlagSet) error { // set default value so that environment wont be able to overwrite it f.defaultValue = f.Value + f.defaultValueSet = true if val, source, found := flagFromEnvOrFile(f.EnvVars, f.FilePath); found { if val != "" { diff --git a/flag_int64.go b/flag_int64.go index 6f8c8d27d8..688c267162 100644 --- a/flag_int64.go +++ b/flag_int64.go @@ -32,7 +32,10 @@ func (f *Int64Flag) GetDefaultText() string { if f.DefaultText != "" { return f.DefaultText } - return fmt.Sprintf("%d", f.defaultValue) + if f.defaultValueSet { + return fmt.Sprintf("%d", f.defaultValue) + } + return fmt.Sprintf("%d", f.Value) } // GetEnvVars returns the env vars for this flag @@ -44,6 +47,7 @@ func (f *Int64Flag) GetEnvVars() []string { func (f *Int64Flag) Apply(set *flag.FlagSet) error { // set default value so that environment wont be able to overwrite it f.defaultValue = f.Value + f.defaultValueSet = true if val, source, found := flagFromEnvOrFile(f.EnvVars, f.FilePath); found { if val != "" { diff --git a/flag_path.go b/flag_path.go index c4986779de..76cb35248c 100644 --- a/flag_path.go +++ b/flag_path.go @@ -33,10 +33,14 @@ func (f *PathFlag) GetDefaultText() string { if f.DefaultText != "" { return f.DefaultText } - if f.defaultValue == "" { - return f.defaultValue + val := f.Value + if f.defaultValueSet { + val = f.defaultValue } - return fmt.Sprintf("%q", f.defaultValue) + if val == "" { + return val + } + return fmt.Sprintf("%q", val) } // GetEnvVars returns the env vars for this flag @@ -48,6 +52,7 @@ func (f *PathFlag) GetEnvVars() []string { func (f *PathFlag) Apply(set *flag.FlagSet) error { // set default value so that environment wont be able to overwrite it f.defaultValue = f.Value + f.defaultValueSet = true if val, _, found := flagFromEnvOrFile(f.EnvVars, f.FilePath); found { f.Value = val diff --git a/flag_string.go b/flag_string.go index 4e55a2ca30..0f73e06215 100644 --- a/flag_string.go +++ b/flag_string.go @@ -31,10 +31,15 @@ func (f *StringFlag) GetDefaultText() string { if f.DefaultText != "" { return f.DefaultText } - if f.defaultValue == "" { - return f.defaultValue + val := f.Value + if f.defaultValueSet { + val = f.defaultValue } - return fmt.Sprintf("%q", f.defaultValue) + + if val == "" { + return val + } + return fmt.Sprintf("%q", val) } // GetEnvVars returns the env vars for this flag @@ -46,6 +51,7 @@ func (f *StringFlag) GetEnvVars() []string { func (f *StringFlag) Apply(set *flag.FlagSet) error { // set default value so that environment wont be able to overwrite it f.defaultValue = f.Value + f.defaultValueSet = true if val, _, found := flagFromEnvOrFile(f.EnvVars, f.FilePath); found { f.Value = val diff --git a/flag_timestamp.go b/flag_timestamp.go index 83f750a319..b90123087c 100644 --- a/flag_timestamp.go +++ b/flag_timestamp.go @@ -120,8 +120,13 @@ func (f *TimestampFlag) GetDefaultText() string { if f.DefaultText != "" { return f.DefaultText } - if f.defaultValue != nil && f.defaultValue.timestamp != nil { - return f.defaultValue.timestamp.String() + val := f.Value + if f.defaultValueSet { + val = f.defaultValue + } + + if val != nil && val.timestamp != nil { + return val.timestamp.String() } return "" @@ -144,6 +149,7 @@ func (f *TimestampFlag) Apply(set *flag.FlagSet) error { f.Value.SetLocation(f.Timezone) f.defaultValue = f.Value.clone() + f.defaultValueSet = true if val, source, found := flagFromEnvOrFile(f.EnvVars, f.FilePath); found { if err := f.Value.Set(val); err != nil { diff --git a/flag_uint.go b/flag_uint.go index d11aa0a891..8d5b85458c 100644 --- a/flag_uint.go +++ b/flag_uint.go @@ -25,6 +25,7 @@ func (f *UintFlag) GetCategory() string { func (f *UintFlag) Apply(set *flag.FlagSet) error { // set default value so that environment wont be able to overwrite it f.defaultValue = f.Value + f.defaultValueSet = true if val, source, found := flagFromEnvOrFile(f.EnvVars, f.FilePath); found { if val != "" { @@ -69,7 +70,10 @@ func (f *UintFlag) GetDefaultText() string { if f.DefaultText != "" { return f.DefaultText } - return fmt.Sprintf("%d", f.defaultValue) + if f.defaultValueSet { + return fmt.Sprintf("%d", f.defaultValue) + } + return fmt.Sprintf("%d", f.Value) } // GetEnvVars returns the env vars for this flag diff --git a/flag_uint64.go b/flag_uint64.go index ea73100a97..c356e533ba 100644 --- a/flag_uint64.go +++ b/flag_uint64.go @@ -25,6 +25,7 @@ func (f *Uint64Flag) GetCategory() string { func (f *Uint64Flag) Apply(set *flag.FlagSet) error { // set default value so that environment wont be able to overwrite it f.defaultValue = f.Value + f.defaultValueSet = true if val, source, found := flagFromEnvOrFile(f.EnvVars, f.FilePath); found { if val != "" { @@ -69,7 +70,10 @@ func (f *Uint64Flag) GetDefaultText() string { if f.DefaultText != "" { return f.DefaultText } - return fmt.Sprintf("%d", f.defaultValue) + if f.defaultValueSet { + return fmt.Sprintf("%d", f.defaultValue) + } + return fmt.Sprintf("%d", f.Value) } // GetEnvVars returns the env vars for this flag diff --git a/zz_generated.flags.go b/zz_generated.flags.go index 73e771451a..f2fc8c88b5 100644 --- a/zz_generated.flags.go +++ b/zz_generated.flags.go @@ -23,7 +23,8 @@ type Float64SliceFlag struct { Aliases []string EnvVars []string - defaultValue *Float64Slice + defaultValue *Float64Slice + defaultValueSet bool separator separatorSpec @@ -69,7 +70,8 @@ type GenericFlag struct { Aliases []string EnvVars []string - defaultValue Generic + defaultValue Generic + defaultValueSet bool TakesFile bool @@ -120,7 +122,8 @@ type Int64SliceFlag struct { Aliases []string EnvVars []string - defaultValue *Int64Slice + defaultValue *Int64Slice + defaultValueSet bool separator separatorSpec @@ -166,7 +169,8 @@ type IntSliceFlag struct { Aliases []string EnvVars []string - defaultValue *IntSlice + defaultValue *IntSlice + defaultValueSet bool separator separatorSpec @@ -212,7 +216,8 @@ type PathFlag struct { Aliases []string EnvVars []string - defaultValue Path + defaultValue Path + defaultValueSet bool TakesFile bool @@ -263,7 +268,8 @@ type StringSliceFlag struct { Aliases []string EnvVars []string - defaultValue *StringSlice + defaultValue *StringSlice + defaultValueSet bool separator separatorSpec @@ -313,7 +319,8 @@ type TimestampFlag struct { Aliases []string EnvVars []string - defaultValue *Timestamp + defaultValue *Timestamp + defaultValueSet bool Layout string @@ -366,7 +373,8 @@ type Uint64SliceFlag struct { Aliases []string EnvVars []string - defaultValue *Uint64Slice + defaultValue *Uint64Slice + defaultValueSet bool separator separatorSpec @@ -412,7 +420,8 @@ type UintSliceFlag struct { Aliases []string EnvVars []string - defaultValue *UintSlice + defaultValue *UintSlice + defaultValueSet bool separator separatorSpec @@ -458,7 +467,8 @@ type BoolFlag struct { Aliases []string EnvVars []string - defaultValue bool + defaultValue bool + defaultValueSet bool Count *int @@ -511,7 +521,8 @@ type Float64Flag struct { Aliases []string EnvVars []string - defaultValue float64 + defaultValue float64 + defaultValueSet bool Action func(*Context, float64) error } @@ -560,7 +571,8 @@ type IntFlag struct { Aliases []string EnvVars []string - defaultValue int + defaultValue int + defaultValueSet bool Base int @@ -611,7 +623,8 @@ type Int64Flag struct { Aliases []string EnvVars []string - defaultValue int64 + defaultValue int64 + defaultValueSet bool Base int @@ -662,7 +675,8 @@ type StringFlag struct { Aliases []string EnvVars []string - defaultValue string + defaultValue string + defaultValueSet bool TakesFile bool @@ -713,7 +727,8 @@ type DurationFlag struct { Aliases []string EnvVars []string - defaultValue time.Duration + defaultValue time.Duration + defaultValueSet bool Action func(*Context, time.Duration) error } @@ -762,7 +777,8 @@ type UintFlag struct { Aliases []string EnvVars []string - defaultValue uint + defaultValue uint + defaultValueSet bool Base int @@ -813,7 +829,8 @@ type Uint64Flag struct { Aliases []string EnvVars []string - defaultValue uint64 + defaultValue uint64 + defaultValueSet bool Base int