Skip to content
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
9 changes: 6 additions & 3 deletions docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,12 @@ func stringifyFlag(f Flag) string {

// don't print default text for required flags
if rf, ok := f.(RequiredFlag); !ok || !rf.IsRequired() {
isVisible := df.IsDefaultVisible()
if s := df.GetDefaultText(); isVisible && s != "" {
defaultValueString = fmt.Sprintf(formatDefault("%s"), s)
if df.IsDefaultVisible() {
if s := df.GetDefaultText(); s != "" {
defaultValueString = fmt.Sprintf(formatDefault("%s"), s)
} else if df.TakesValue() && df.GetValue() != "" {
defaultValueString = fmt.Sprintf(formatDefault("%s"), df.GetValue())
}
}
}

Expand Down
12 changes: 3 additions & 9 deletions flag_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,8 @@ type FlagBase[T any, C any, VC ValueCreator[T, C]] struct {
// GetValue returns the flags value as string representation and an empty
// string if the flag takes no value at all.
func (f *FlagBase[T, C, V]) GetValue() string {
if !f.TakesValue() {
return ""
}
return fmt.Sprintf("%v", f.Value)
var v V
return v.ToString(f.Value)
}

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

// GetDefaultText returns the default text for this flag
func (f *FlagBase[T, C, V]) GetDefaultText() string {
if f.DefaultText != "" {
return f.DefaultText
}
var v V
return v.ToString(f.Value)
return f.DefaultText
}

// RunAction executes flag action if set
Expand Down
16 changes: 8 additions & 8 deletions flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ var boolFlagTests = []struct {
name string
expected string
}{
{"help", "--help\t(default: false)"},
{"h", "-h\t(default: false)"},
{"help", "--help\t"},
{"h", "-h\t"},
}

func TestBoolFlagHelpOutput(t *testing.T) {
Expand Down Expand Up @@ -470,7 +470,7 @@ func TestFlagStringifying(t *testing.T) {
{
name: "bool-flag",
fl: &BoolFlag{Name: "vividly"},
expected: "--vividly\t(default: false)",
expected: "--vividly\t",
},
{
name: "bool-flag-with-default-text",
Expand Down Expand Up @@ -2771,7 +2771,7 @@ func TestFlagDefaultValue(t *testing.T) {
name: "bool",
flag: &BoolFlag{Name: "flag", Value: true},
toParse: []string{"--flag=false"},
expect: `--flag (default: true)`,
expect: `--flag `,
},
{
name: "uint",
Expand Down Expand Up @@ -2866,7 +2866,7 @@ func TestFlagDefaultValueWithEnv(t *testing.T) {
name: "bool",
flag: &BoolFlag{Name: "flag", Value: true, Sources: EnvVars("uflag")},
toParse: []string{"--flag=false"},
expect: `--flag (default: true)` + withEnvHint([]string{"uflag"}, ""),
expect: `--flag ` + withEnvHint([]string{"uflag"}, ""),
environ: map[string]string{
"uflag": "false",
},
Expand Down Expand Up @@ -3359,9 +3359,9 @@ func TestEnvHintWindows(t *testing.T) {
}

func TestDocGetValue(t *testing.T) {
assert.Equal(t, "", (&BoolFlag{Name: "foo", Value: true}).GetValue())
assert.Equal(t, "", (&BoolFlag{Name: "foo", Value: false}).GetValue())
assert.Equal(t, "bar", (&StringFlag{Name: "foo", Value: "bar"}).GetValue())
assert.Equal(t, "true", (&BoolFlag{Name: "foo", Value: true}).GetValue())
assert.Equal(t, "false", (&BoolFlag{Name: "foo", Value: false}).GetValue())
assert.Equal(t, "\"bar\"", (&StringFlag{Name: "foo", Value: "bar"}).GetValue())
assert.Equal(t, "", (&BoolWithInverseFlag{Name: "foo", Value: false}).GetValue())
}

Expand Down
3 changes: 1 addition & 2 deletions help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1479,8 +1479,7 @@ GLOBAL OPTIONS:
really long help text
line, let's see where it
wraps. blah blah blah
and so on. (default:
false)
and so on.

COPYRIGHT:
Here's a sample copyright
Expand Down