Skip to content

Commit

Permalink
Update all flags with similar behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
dearchap committed Nov 29, 2023
1 parent e2301c8 commit 92aa673
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 25 deletions.
6 changes: 5 additions & 1 deletion flag_bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 != "" {
Expand Down
8 changes: 7 additions & 1 deletion flag_float64.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
11 changes: 9 additions & 2 deletions flag_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
}

Expand All @@ -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 {
Expand Down
12 changes: 5 additions & 7 deletions flag_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,14 @@ func (f *PathFlag) GetDefaultText() string {
if f.DefaultText != "" {
return f.DefaultText
}
val := f.Value
if f.defaultValueSet {
if f.defaultValue == "" {
return f.defaultValue
}
return fmt.Sprintf("%q", f.defaultValue)
val = f.defaultValue
}
if f.Value == "" {
return f.Value
if val == "" {
return val
}
return fmt.Sprintf("%q", f.Value)
return fmt.Sprintf("%q", val)
}

// GetEnvVars returns the env vars for this flag
Expand Down
13 changes: 6 additions & 7 deletions flag_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,15 @@ func (f *StringFlag) GetDefaultText() string {
if f.DefaultText != "" {
return f.DefaultText
}
val := f.Value
if f.defaultValueSet {
if f.defaultValue == "" {
return f.defaultValue
}
return fmt.Sprintf("%q", f.defaultValue)
val = f.defaultValue
}
if f.Value == "" {
return f.Value

if val == "" {
return val
}
return fmt.Sprintf("%q", f.Value)
return fmt.Sprintf("%q", val)
}

// GetEnvVars returns the env vars for this flag
Expand Down
13 changes: 6 additions & 7 deletions flag_timestamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,13 @@ func (f *TimestampFlag) GetDefaultText() string {
if f.DefaultText != "" {
return f.DefaultText
}
val := f.Value
if f.defaultValueSet {
if f.defaultValue != nil && f.defaultValue.timestamp != nil {
return f.defaultValue.timestamp.String()
}
} else {
if f.Value != nil && f.Value.timestamp != nil {
return f.Value.timestamp.String()
}
val = f.defaultValue
}

if val != nil && val.timestamp != nil {
return val.timestamp.String()
}

return ""
Expand Down

0 comments on commit 92aa673

Please sign in to comment.