From f8eb02c99d101961632763a4a2e24ede8886c365 Mon Sep 17 00:00:00 2001 From: Aaron Berns Date: Fri, 30 Aug 2019 13:02:26 -0400 Subject: [PATCH] add required flag and custom required flag error message example to README --- README.md | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/README.md b/README.md index 29ed171..60ac106 100644 --- a/README.md +++ b/README.md @@ -560,6 +560,85 @@ Will result in help output like: --lang value, -l value Language for the greeting (default: "english") ``` +#### Required Flags + +You can make a flag required by setting the `Required` field to `true`. If an end-user +fails to provide a required flag, they will be shown a default error message, or a +custom message if one is defined. To define a custom error message for a required flag, +set the `RequiredFlagErr` field equal to a `cli.FlagErr` struct with a `Custom` field of `true` +and a `Message` field containing the custom error message. Default and custom error messages +can be mixed with default messages displayed before custom messages. + +For example this: + +```go +package main + +import ( + "log" + "os" + "strings" + + "../cli" +) + +func main() { + app := cli.NewApp() + + app.Flags = []cli.Flag { + cli.StringFlag{ + Name: "lang", + Value: "english", + Usage: "language for the greeting", + Required: true, + RequiredFlagErr: cli.FlagErr{ + Custom: true, + Message: `There's a problem: "lang" is a required flag` , + }, + }, + cli.StringFlag{ + Name: "mood", + Value: "normal", + Usage: "emphasis for the greeting", + Required: true, + }, + } + + app.Action = func(c *cli.Context) error { + name := "Nefertiti" + if c.NArg() > 0 { + name = c.Args().Get(0) + } + var output string + if c.String("lang") == "spanish" { + output = "Hola " + name + } else { + output = "Hello " + name + } + + if c.String("mood") == "excited" { + output = strings.ToUpper(output) + } + + return nil + } + + err := app.Run(os.Args) + if err != nil { + log.Fatal(err) + } +} +``` + +Creates an app that requires both `lang` and `mood` flags. If an attempt +to run the app is made without either, the end-user will see the following +prompts: + +``` +Required flag "mood" not set +There's a problem: "lang" is a required flag +``` + #### Values from the Environment You can also have the default value set from the environment via `EnvVar`. e.g.