docs: update user guide (#2128)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Xinwei Xiong · committed by GitHub · parent 6b5f577 · Browse files · 1 file·+10 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+10 added−4 removed
- other:+10 added−4 removed
Provenance
- Commit
0fc86c2- Tree
96b197d- Author
Xinwei Xiong <3293172751NSS@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
6b5f577
1 file changed+10 added−4 removed
modified:site/content/user_guide.md+10 added-4 removed
1 unmodified line2 3 While you are welcome to provide your own organization, typically a Cobra-based4 application will follow the following organizational structure:5 6-```6+```test7 ▾ appName/8 ▾ cmd/9 add.go10 your.go289 unmodified lines300 301 ### Bind Flags with Config302 303 You can also bind your flags with [viper](https://github.com/spf13/viper):304+305 ```go306 var author string307 308 func init() {11 unmodified lines320 ### Required flags321 322 Flags are optional by default. If instead you wish your command to report an error323 when a flag has not been set, mark it as required:324+325 ```go326 rootCmd.Flags().StringVarP(&Region, "region", "r", "", "AWS region (required)")327 rootCmd.MarkFlagRequired("region")328 ```329 330 Or, for persistent flags:331+332 ```go333 rootCmd.PersistentFlags().StringVarP(&Region, "region", "r", "", "AWS region (required)")334 rootCmd.MarkPersistentFlagRequired("region")335 ```1 unmodified line337 ### Flag Groups338 339 If you have different flags that must be provided together (e.g. if they provide the `--username` flag they MUST provide the `--password` flag as well) then340 Cobra can enforce that requirement:341+342 ```go343 rootCmd.Flags().StringVarP(&u, "username", "u", "", "Username (required if password is set)")344 rootCmd.Flags().StringVarP(&pw, "password", "p", "", "Password (required if username is set)")345 rootCmd.MarkFlagsRequiredTogether("username", "password")346 ```347 348 You can also prevent different flags from being provided together if they represent mutually349 exclusive options such as specifying an output format as either `--json` or `--yaml` but never both:350+351 ```go352 rootCmd.Flags().BoolVar(&ofJson, "json", false, "Output in JSON")353 rootCmd.Flags().BoolVar(&ofYaml, "yaml", false, "Output in YAML")354 rootCmd.MarkFlagsMutuallyExclusive("json", "yaml")355 ```356 357 If you want to require at least one flag from a group to be present, you can use `MarkFlagsOneRequired`.358 This can be combined with `MarkFlagsMutuallyExclusive` to enforce exactly one flag from a given group:359+360 ```go361 rootCmd.Flags().BoolVar(&ofJson, "json", false, "Output in JSON")362 rootCmd.Flags().BoolVar(&ofYaml, "yaml", false, "Output in YAML")363 rootCmd.MarkFlagsOneRequired("json", "yaml")69 unmodified lines433 by not providing a 'Run' for the 'rootCmd'.434 435 We have only defined one flag for a single command.436 431-More documentation about flags is available at https://github.com/spf13/pflag437+More documentation about flags is available at https://github.com/spf13/pflag.438 439 ```go440 package main441 285 unmodified lines727 728 You can also explicitly set names for which a given command will be suggested using the `SuggestFor` attribute. This allows suggestions for strings that are not close in terms of string distance, but make sense in your set of commands but for which729 you don't want aliases. Example:730 725-```731+```bash732 $ kubectl remove733 Error: unknown command "remove" for "kubectl"734 735 Did you mean this?56 unmodified lines792 ```793 794 Example run as a kubectl plugin:795 790-```796+```bash797 $ kubectl myplugin798 Usage:799 kubectl myplugin [command]800 10 unmodified lines