Add documentation, remove quotes by default
This commit is contained in:
parent
e0556cf9e8
commit
7d56512ecc
42
README.md
42
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:
|
||||||
|
|
||||||
|
<!-- {
|
||||||
|
"args": ["--help"],
|
||||||
|
"output": "--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
|
||||||
|
|
||||||
Subcommands can be defined for a more git-like command line app.
|
Subcommands can be defined for a more git-like command line app.
|
||||||
|
2
flag.go
2
flag.go
@ -742,7 +742,7 @@ func stringifyFlag(f Flag) string {
|
|||||||
helpText := fv.FieldByName("DefaultText")
|
helpText := fv.FieldByName("DefaultText")
|
||||||
if helpText.IsValid() && helpText.String() != "" {
|
if helpText.IsValid() && helpText.String() != "" {
|
||||||
needsPlaceholder = val.Kind() != reflect.Bool
|
needsPlaceholder = val.Kind() != reflect.Bool
|
||||||
defaultValueString = fmt.Sprintf(" (default: %q)", helpText.String())
|
defaultValueString = fmt.Sprintf(" (default: %s)", helpText.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
if defaultValueString == " (default: )" {
|
if defaultValueString == " (default: )" {
|
||||||
|
@ -69,7 +69,7 @@ func TestStringFlagHelpOutput(t *testing.T) {
|
|||||||
|
|
||||||
func TestStringFlagDefaultText(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"}
|
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()
|
output := flag.String()
|
||||||
|
|
||||||
if output != expected {
|
if output != expected {
|
||||||
|
Loading…
Reference in New Issue
Block a user