Add DefaultValue text for flags

Allows a user to override the default value of a flag in the displayed help output.

Ex:
```
cli.IntFlag{
	Name: "foo, f",
	DefaultText: "random foo",
	Value: "bar",
}
```

Running `(app.name) -h` will now yield:

```
--foo value     (default: "random foo")
```

Fixes: #504
This commit is contained in:
Jake Champlin 2016-08-28 03:14:39 -04:00
parent a095a5a896
commit e0556cf9e8
No known key found for this signature in database
GPG Key ID: DC31F41958EF4AC2
4 changed files with 60 additions and 36 deletions

View File

@ -730,7 +730,6 @@ func stringifyFlag(f Flag) string {
needsPlaceholder := false
defaultValueString := ""
val := fv.FieldByName("Value")
if val.IsValid() {
needsPlaceholder = val.Kind() != reflect.Bool
defaultValueString = fmt.Sprintf(" (default: %v)", val.Interface())
@ -740,6 +739,12 @@ func stringifyFlag(f Flag) string {
}
}
helpText := fv.FieldByName("DefaultText")
if helpText.IsValid() && helpText.String() != "" {
needsPlaceholder = val.Kind() != reflect.Bool
defaultValueString = fmt.Sprintf(" (default: %q)", helpText.String())
}
if defaultValueString == " (default: )" {
defaultValueString = ""
}

View File

@ -16,6 +16,7 @@ type BoolFlag struct {
EnvVars []string
Hidden bool
Value bool
DefaultText string
Destination *bool
}
@ -59,6 +60,7 @@ type DurationFlag struct {
EnvVars []string
Hidden bool
Value time.Duration
DefaultText string
Destination *time.Duration
}
@ -102,6 +104,7 @@ type Float64Flag struct {
EnvVars []string
Hidden bool
Value float64
DefaultText string
Destination *float64
}
@ -145,6 +148,7 @@ type GenericFlag struct {
EnvVars []string
Hidden bool
Value Generic
DefaultText string
}
// String returns a readable representation of this value
@ -187,6 +191,7 @@ type Int64Flag struct {
EnvVars []string
Hidden bool
Value int64
DefaultText string
Destination *int64
}
@ -230,6 +235,7 @@ type IntFlag struct {
EnvVars []string
Hidden bool
Value int
DefaultText string
Destination *int
}
@ -273,6 +279,7 @@ type IntSliceFlag struct {
EnvVars []string
Hidden bool
Value *IntSlice
DefaultText string
}
// String returns a readable representation of this value
@ -315,6 +322,7 @@ type Int64SliceFlag struct {
EnvVars []string
Hidden bool
Value *Int64Slice
DefaultText string
}
// String returns a readable representation of this value
@ -357,6 +365,7 @@ type Float64SliceFlag struct {
EnvVars []string
Hidden bool
Value *Float64Slice
DefaultText string
}
// String returns a readable representation of this value
@ -399,6 +408,7 @@ type StringFlag struct {
EnvVars []string
Hidden bool
Value string
DefaultText string
Destination *string
}
@ -442,6 +452,7 @@ type StringSliceFlag struct {
EnvVars []string
Hidden bool
Value *StringSlice
DefaultText string
}
// String returns a readable representation of this value
@ -484,6 +495,7 @@ type Uint64Flag struct {
EnvVars []string
Hidden bool
Value uint64
DefaultText string
Destination *uint64
}
@ -527,6 +539,7 @@ type UintFlag struct {
EnvVars []string
Hidden bool
Value uint
DefaultText string
Destination *uint
}

View File

@ -67,6 +67,16 @@ func TestStringFlagHelpOutput(t *testing.T) {
}
}
func TestStringFlagDefaultText(t *testing.T) {
flag := &StringFlag{Name: "foo", Aliases: nil, Usage: "amount of `foo` requested", Value: "none", DefaultText: "all of it"}
expected := "--foo foo\tamount of foo requested (default: \"all of it\")"
output := flag.String()
if output != expected {
t.Errorf("%q does not match %q", output, expected)
}
}
func TestStringFlagWithEnvVarHelpOutput(t *testing.T) {
os.Clearenv()
os.Setenv("APP_FOO", "derp")
@ -482,7 +492,6 @@ func TestFloat64FlagApply_SetsAllNames(t *testing.T) {
expect(t, v, float64(43.33333))
}
var float64SliceFlagTests = []struct {
name string
aliases []string
@ -523,8 +532,6 @@ func TestFloat64SliceFlagWithEnvVarHelpOutput(t *testing.T) {
}
}
var genericFlagTests = []struct {
name string
value Generic
@ -1100,7 +1107,6 @@ func TestParseMultiFloat64FromEnvCascade(t *testing.T) {
a.Run([]string{"run"})
}
func TestParseMultiFloat64SliceFromEnv(t *testing.T) {
os.Clearenv()
os.Setenv("APP_INTERVALS", "0.1,-10.5")
@ -1141,7 +1147,6 @@ func TestParseMultiFloat64SliceFromEnvCascade(t *testing.T) {
}).Run([]string{"run"})
}
func TestParseMultiBool(t *testing.T) {
a := App{
Flags: []Flag{

View File

@ -145,6 +145,7 @@ def _write_cli_flag_types(outfile, types):
EnvVars []string
Hidden bool
Value {type}
DefaultText string
""".format(**typedef))
if typedef['dest']: