Merge pull request #1008 from urfave/lynncyrin-patch-8

Add required flags documentation
This commit is contained in:
lynn [they] 2019-12-25 08:15:23 -08:00 committed by GitHub
commit c7f04148e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -13,6 +13,7 @@ cli v2 manual
+ [Values from the Environment](#values-from-the-environment) + [Values from the Environment](#values-from-the-environment)
+ [Values from files](#values-from-files) + [Values from files](#values-from-files)
+ [Values from alternate input sources (YAML, TOML, and others)](#values-from-alternate-input-sources-yaml-toml-and-others) + [Values from alternate input sources (YAML, TOML, and others)](#values-from-alternate-input-sources-yaml-toml-and-others)
+ [Required Flags](#required-flags)
+ [Default Values for help output](#default-values-for-help-output) + [Default Values for help output](#default-values-for-help-output)
+ [Precedence](#precedence) + [Precedence](#precedence)
* [Subcommands](#subcommands) * [Subcommands](#subcommands)
@ -641,6 +642,63 @@ func main() {
} }
``` ```
#### Required Flags
You can make a flag required by setting the `Required` field to `true`. If a user
does not provide a required flag, they will be shown an error message.
Take for example this app that reqiures the `lang` flag:
<!-- {
"error": "Required flag \"lang\" not set"
} -->
```go
package main
import (
"log"
"os"
"strings"
"github.com/urfave/cli"
)
func main() {
app := cli.NewApp()
app.Flags = []cli.Flag {
cli.StringFlag{
Name: "lang",
Value: "english",
Usage: "language for the greeting",
Required: true,
},
}
app.Action = func(c *cli.Context) error {
var output string
if c.String("lang") == "spanish" {
output = "Hola"
} else {
output = "Hello"
}
fmt.Println(output)
return nil
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
```
If the app is run without the `lang` flag, the user will see the following message
```
Required flag "lang" not set
```
#### Default Values for help output #### 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. 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.