Skip to content

Commit dfeca96

Browse files
committed
Update user guide
1 parent 06d37a0 commit dfeca96

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

user_guide.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,16 @@ rootCmd.Flags().BoolVar(&ofYaml, "yaml", false, "Output in YAML")
349349
rootCmd.MarkFlagsMutuallyExclusive("json", "yaml")
350350
```
351351

352-
In both of these cases:
352+
If you want to require least one flag from a group to be present, you can use `MarkFlagsOneRequired`.
353+
This can be combined with `MarkFlagsMutuallyExclusive` to enforce exactly one flag from a given group:
354+
```go
355+
rootCmd.Flags().BoolVar(&ofJson, "json", false, "Output in JSON")
356+
rootCmd.Flags().BoolVar(&ofYaml, "yaml", false, "Output in YAML")
357+
rootCmd.MarkFlagsOneRequired("json", "yaml")
358+
rootCmd.MarkFlagsMutuallyExclusive("json", "yaml")
359+
```
360+
361+
In all of these cases:
353362
- both local and persistent flags can be used
354363
- **NOTE:** the group is only enforced on commands where every flag is defined
355364
- a flag may appear in multiple groups

0 commit comments

Comments
 (0)