Skip to content

Commit 495e1d3

Browse files
authored
Merge pull request #2214 from dearchap/def_text
Fix: Make DefaultText behaviour consistent
2 parents 83247f0 + 7135fdb commit 495e1d3

File tree

4 files changed

+18
-22
lines changed

4 files changed

+18
-22
lines changed

docs.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,12 @@ func stringifyFlag(f Flag) string {
107107

108108
// don't print default text for required flags
109109
if rf, ok := f.(RequiredFlag); !ok || !rf.IsRequired() {
110-
isVisible := df.IsDefaultVisible()
111-
if s := df.GetDefaultText(); isVisible && s != "" {
112-
defaultValueString = fmt.Sprintf(formatDefault("%s"), s)
110+
if df.IsDefaultVisible() {
111+
if s := df.GetDefaultText(); s != "" {
112+
defaultValueString = fmt.Sprintf(formatDefault("%s"), s)
113+
} else if df.TakesValue() && df.GetValue() != "" {
114+
defaultValueString = fmt.Sprintf(formatDefault("%s"), df.GetValue())
115+
}
113116
}
114117
}
115118

flag_impl.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,8 @@ type FlagBase[T any, C any, VC ValueCreator[T, C]] struct {
7070
// GetValue returns the flags value as string representation and an empty
7171
// string if the flag takes no value at all.
7272
func (f *FlagBase[T, C, V]) GetValue() string {
73-
if !f.TakesValue() {
74-
return ""
75-
}
76-
return fmt.Sprintf("%v", f.Value)
73+
var v V
74+
return v.ToString(f.Value)
7775
}
7876

7977
// TypeName returns the type of the flag.
@@ -253,11 +251,7 @@ func (f *FlagBase[T, C, V]) TakesValue() bool {
253251

254252
// GetDefaultText returns the default text for this flag
255253
func (f *FlagBase[T, C, V]) GetDefaultText() string {
256-
if f.DefaultText != "" {
257-
return f.DefaultText
258-
}
259-
var v V
260-
return v.ToString(f.Value)
254+
return f.DefaultText
261255
}
262256

263257
// RunAction executes flag action if set

flag_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ var boolFlagTests = []struct {
4444
name string
4545
expected string
4646
}{
47-
{"help", "--help\t(default: false)"},
48-
{"h", "-h\t(default: false)"},
47+
{"help", "--help\t"},
48+
{"h", "-h\t"},
4949
}
5050

5151
func TestBoolFlagHelpOutput(t *testing.T) {
@@ -470,7 +470,7 @@ func TestFlagStringifying(t *testing.T) {
470470
{
471471
name: "bool-flag",
472472
fl: &BoolFlag{Name: "vividly"},
473-
expected: "--vividly\t(default: false)",
473+
expected: "--vividly\t",
474474
},
475475
{
476476
name: "bool-flag-with-default-text",
@@ -2771,7 +2771,7 @@ func TestFlagDefaultValue(t *testing.T) {
27712771
name: "bool",
27722772
flag: &BoolFlag{Name: "flag", Value: true},
27732773
toParse: []string{"--flag=false"},
2774-
expect: `--flag (default: true)`,
2774+
expect: `--flag `,
27752775
},
27762776
{
27772777
name: "uint",
@@ -2866,7 +2866,7 @@ func TestFlagDefaultValueWithEnv(t *testing.T) {
28662866
name: "bool",
28672867
flag: &BoolFlag{Name: "flag", Value: true, Sources: EnvVars("uflag")},
28682868
toParse: []string{"--flag=false"},
2869-
expect: `--flag (default: true)` + withEnvHint([]string{"uflag"}, ""),
2869+
expect: `--flag ` + withEnvHint([]string{"uflag"}, ""),
28702870
environ: map[string]string{
28712871
"uflag": "false",
28722872
},
@@ -3359,9 +3359,9 @@ func TestEnvHintWindows(t *testing.T) {
33593359
}
33603360

33613361
func TestDocGetValue(t *testing.T) {
3362-
assert.Equal(t, "", (&BoolFlag{Name: "foo", Value: true}).GetValue())
3363-
assert.Equal(t, "", (&BoolFlag{Name: "foo", Value: false}).GetValue())
3364-
assert.Equal(t, "bar", (&StringFlag{Name: "foo", Value: "bar"}).GetValue())
3362+
assert.Equal(t, "true", (&BoolFlag{Name: "foo", Value: true}).GetValue())
3363+
assert.Equal(t, "false", (&BoolFlag{Name: "foo", Value: false}).GetValue())
3364+
assert.Equal(t, "\"bar\"", (&StringFlag{Name: "foo", Value: "bar"}).GetValue())
33653365
assert.Equal(t, "", (&BoolWithInverseFlag{Name: "foo", Value: false}).GetValue())
33663366
}
33673367

help_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,8 +1479,7 @@ GLOBAL OPTIONS:
14791479
really long help text
14801480
line, let's see where it
14811481
wraps. blah blah blah
1482-
and so on. (default:
1483-
false)
1482+
and so on.
14841483
14851484
COPYRIGHT:
14861485
Here's a sample copyright

0 commit comments

Comments
 (0)