diff --git a/README.md b/README.md index 2f0980e..43f2247 100644 --- a/README.md +++ b/README.md @@ -589,6 +589,48 @@ func main() { } ``` +#### Default Values for help output + +Sometimes it's useful to specify a flag's default help-text value within the flag declaration. This can be useful if the default value for a flag is a computed value. The default value can be set via the `DefaultText` struct field. + +For example this: + + +```go +package main + +import ( + "os" + + "gopkg.in/urfave/cli.v2" +) + +func main() { + app := &cli.App{ + Flags: []cli.Flag{ + &cli.IntFlag{ + Name: "port", + Usage: "Use a randomized port", + Value: 0, + DefaultText: "random", + }, + }, + } + + app.Run(os.Args) +} +``` + +Will result in help output like: + +``` +--port value Use a randomized port (default: random) +``` + + ### Subcommands Subcommands can be defined for a more git-like command line app. diff --git a/flag.go b/flag.go index 29010f2..10adca8 100644 --- a/flag.go +++ b/flag.go @@ -742,7 +742,7 @@ 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()) + defaultValueString = fmt.Sprintf(" (default: %s)", helpText.String()) } if defaultValueString == " (default: )" { diff --git a/flag_test.go b/flag_test.go index be126f9..ccb1d45 100644 --- a/flag_test.go +++ b/flag_test.go @@ -69,7 +69,7 @@ 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\")" + expected := "--foo foo\tamount of foo requested (default: all of it)" output := flag.String() if output != expected {