Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix:(issue_1689) Have consistent behavior for default text in man and… #1825

Merged
merged 2 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/urfave-cli-genflags/generated.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type {{.TypeName}} struct {
EnvVars []string

defaultValue {{if .ValuePointer}}*{{end}}{{.GoType}}
defaultValueSet bool
{{ range .StructFields}}
{{.Name}} {{if .Pointer}}*{{end}}{{.Type}}
{{end -}}
Expand Down
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
6 changes: 5 additions & 1 deletion flag_duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 != "" {
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
6 changes: 5 additions & 1 deletion flag_int.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 != "" {
Expand Down
6 changes: 5 additions & 1 deletion flag_int64.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 != "" {
Expand Down
11 changes: 8 additions & 3 deletions flag_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
12 changes: 9 additions & 3 deletions flag_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
10 changes: 8 additions & 2 deletions flag_timestamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
Expand All @@ -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 {
Expand Down
6 changes: 5 additions & 1 deletion flag_uint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion flag_uint64.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand Down Expand Up @@ -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
Expand Down
Loading