Add documentation, remove quotes by default

This commit is contained in:
Jake Champlin 2016-08-30 10:52:24 -04:00
parent e0556cf9e8
commit 7d56512ecc
No known key found for this signature in database
GPG Key ID: DC31F41958EF4AC2
3 changed files with 44 additions and 2 deletions

View File

@ -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:
<!-- {
"args": ["&#45;&#45;help"],
"output": "&#45;&#45;port value"
} -->
```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.

View File

@ -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: )" {

View File

@ -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 {