You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- Allows defaults to be given via struct tag `default`
26
26
- Falls back to using instance field values as declared default
27
27
- Declare flag usage via struct tag `usage`
28
+
- Mark flags as required via struct tag `required` and validate with `Verify()` method (cannot be combined with `default` tag)
28
29
- Can be combined with other modules, such as [google/subcommands](https://github.com/google/subcommands) for sub-command processing. Can also be integrated with [spf13/cobra](https://github.com/spf13/cobra) by using pflag's [AddGoFlagSet](https://godoc.org/github.com/spf13/pflag#FlagSet.AddGoFlagSet)
29
30
- Beyond the standard types supported by flag.FlagSet also includes support for:
30
31
-`[]string` where repetition of the argument appends to the slice and/or an argument value can contain a comma or newline-separated list of values. For example: `--arg one --arg two,three`
@@ -93,6 +94,36 @@ The following shows an example of the usage provided when passing `--help`:
93
94
How long to wait (default 5s)
94
95
```
95
96
97
+
## Required flags
98
+
99
+
Flags can be marked as required using the `required:"true"` struct tag. After parsing command-line arguments, call the `Verify()` method to ensure all required flags have been provided:
Usernamestring`required:"true" usage:"Username for authentication"`
106
+
}
107
+
108
+
varconfigConfig
109
+
110
+
filler:= flagsfiller.New()
111
+
err:= filler.Fill(flag.CommandLine, &config)
112
+
if err != nil {
113
+
log.Fatal(err)
114
+
}
115
+
116
+
flag.Parse()
117
+
118
+
// Verify all required fields are set
119
+
err = filler.Verify()
120
+
if err != nil {
121
+
log.Fatal(err) // Will fail if Host or Username not provided
122
+
}
123
+
```
124
+
125
+
**Note:** A field cannot be both required and have a default value. Attempting to use both tags will result in an error during `Fill()`.
126
+
96
127
## Real world example
97
128
98
129
[saml-auth-proxy](https://github.com/itzg/saml-auth-proxy) shows an end-to-end usage of flagsfiller where the main function fills the flags, maps those to environment variables with [envy](https://github.com/jamiealquiza/envy), and parses the command line:
0 commit comments