Skip to content
Myr

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

1 file+10 added−4 removed

modified:site/content/user_guide.md+10 added-4 removed
1 unmodified line
23While you are welcome to provide your own organization, typically a Cobra-based4application will follow the following organizational structure:56```6```test7 ▾ appName/8 ▾ cmd/9 add.go10 your.go
289 unmodified lines
300301### Bind Flags with Config302303You can also bind your flags with [viper](https://github.com/spf13/viper):304305```go306var author string307308func init() {
11 unmodified lines
320### Required flags321322Flags are optional by default. If instead you wish your command to report an error323when a flag has not been set, mark it as required:324325```go326rootCmd.Flags().StringVarP(&Region, "region", "r", "", "AWS region (required)")327rootCmd.MarkFlagRequired("region")328```329330Or, for persistent flags:331332```go333rootCmd.PersistentFlags().StringVarP(&Region, "region", "r", "", "AWS region (required)")334rootCmd.MarkPersistentFlagRequired("region")335```
1 unmodified line
337### Flag Groups338339If 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) then340Cobra can enforce that requirement:341342```go343rootCmd.Flags().StringVarP(&u, "username", "u", "", "Username (required if password is set)")344rootCmd.Flags().StringVarP(&pw, "password", "p", "", "Password (required if username is set)")345rootCmd.MarkFlagsRequiredTogether("username", "password")346```347348You can also prevent different flags from being provided together if they represent mutually349exclusive options such as specifying an output format as either `--json` or `--yaml` but never both:350351```go352rootCmd.Flags().BoolVar(&ofJson, "json", false, "Output in JSON")353rootCmd.Flags().BoolVar(&ofYaml, "yaml", false, "Output in YAML")354rootCmd.MarkFlagsMutuallyExclusive("json", "yaml")355```356357If you want to require at least one flag from a group to be present, you can use `MarkFlagsOneRequired`.358This can be combined with `MarkFlagsMutuallyExclusive` to enforce exactly one flag from a given group:359360```go361rootCmd.Flags().BoolVar(&ofJson, "json", false, "Output in JSON")362rootCmd.Flags().BoolVar(&ofYaml, "yaml", false, "Output in YAML")363rootCmd.MarkFlagsOneRequired("json", "yaml")
69 unmodified lines
433by not providing a 'Run' for the 'rootCmd'.434435We have only defined one flag for a single command.436431More documentation about flags is available at https://github.com/spf13/pflag437More documentation about flags is available at https://github.com/spf13/pflag.438439```go440package main441
285 unmodified lines
727728You 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 which729you don't want aliases. Example:730725```731```bash732$ kubectl remove733Error: unknown command "remove" for "kubectl"734735Did you mean this?
56 unmodified lines
792```793794Example run as a kubectl plugin:795790```796```bash797$ kubectl myplugin798Usage:799 kubectl myplugin [command]800
10 unmodified lines