Skip to content

Commit 0fc86c2

Browse files
authored
docs: update user guide (#2128)
1 parent 6b5f577 commit 0fc86c2

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

site/content/user_guide.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
While you are welcome to provide your own organization, typically a Cobra-based
44
application will follow the following organizational structure:
55

6-
```
6+
```test
77
▾ appName/
88
▾ cmd/
99
add.go
@@ -301,6 +301,7 @@ command := cobra.Command{
301301
### Bind Flags with Config
302302

303303
You can also bind your flags with [viper](https://github.com/spf13/viper):
304+
304305
```go
305306
var author string
306307

@@ -320,12 +321,14 @@ More in [viper documentation](https://github.com/spf13/viper#working-with-flags)
320321

321322
Flags are optional by default. If instead you wish your command to report an error
322323
when a flag has not been set, mark it as required:
324+
323325
```go
324326
rootCmd.Flags().StringVarP(&Region, "region", "r", "", "AWS region (required)")
325327
rootCmd.MarkFlagRequired("region")
326328
```
327329

328330
Or, for persistent flags:
331+
329332
```go
330333
rootCmd.PersistentFlags().StringVarP(&Region, "region", "r", "", "AWS region (required)")
331334
rootCmd.MarkPersistentFlagRequired("region")
@@ -335,6 +338,7 @@ rootCmd.MarkPersistentFlagRequired("region")
335338

336339
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) then
337340
Cobra can enforce that requirement:
341+
338342
```go
339343
rootCmd.Flags().StringVarP(&u, "username", "u", "", "Username (required if password is set)")
340344
rootCmd.Flags().StringVarP(&pw, "password", "p", "", "Password (required if username is set)")
@@ -343,6 +347,7 @@ rootCmd.MarkFlagsRequiredTogether("username", "password")
343347

344348
You can also prevent different flags from being provided together if they represent mutually
345349
exclusive options such as specifying an output format as either `--json` or `--yaml` but never both:
350+
346351
```go
347352
rootCmd.Flags().BoolVar(&ofJson, "json", false, "Output in JSON")
348353
rootCmd.Flags().BoolVar(&ofYaml, "yaml", false, "Output in YAML")
@@ -351,6 +356,7 @@ rootCmd.MarkFlagsMutuallyExclusive("json", "yaml")
351356

352357
If you want to require at least one flag from a group to be present, you can use `MarkFlagsOneRequired`.
353358
This can be combined with `MarkFlagsMutuallyExclusive` to enforce exactly one flag from a given group:
359+
354360
```go
355361
rootCmd.Flags().BoolVar(&ofJson, "json", false, "Output in JSON")
356362
rootCmd.Flags().BoolVar(&ofYaml, "yaml", false, "Output in YAML")
@@ -428,7 +434,7 @@ by not providing a 'Run' for the 'rootCmd'.
428434

429435
We have only defined one flag for a single command.
430436

431-
More documentation about flags is available at https://github.com/spf13/pflag
437+
More documentation about flags is available at https://github.com/spf13/pflag.
432438

433439
```go
434440
package main
@@ -722,7 +728,7 @@ command.SuggestionsMinimumDistance = 1
722728
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 which
723729
you don't want aliases. Example:
724730

725-
```
731+
```bash
726732
$ kubectl remove
727733
Error: unknown command "remove" for "kubectl"
728734

@@ -787,7 +793,7 @@ func main() {
787793

788794
Example run as a kubectl plugin:
789795

790-
```
796+
```bash
791797
$ kubectl myplugin
792798
Usage:
793799
kubectl myplugin [command]

0 commit comments

Comments
 (0)