MarkFlagsMutuallyExclusive marks the given flags with annotations so that Cobra errors if the command is invoked with more than one flag from the given set of flags.
(flagNames ...string)
| 63 | // MarkFlagsMutuallyExclusive marks the given flags with annotations so that Cobra errors |
| 64 | // if the command is invoked with more than one flag from the given set of flags. |
| 65 | func (c *Command) MarkFlagsMutuallyExclusive(flagNames ...string) { |
| 66 | c.mergePersistentFlags() |
| 67 | for _, v := range flagNames { |
| 68 | f := c.Flags().Lookup(v) |
| 69 | if f == nil { |
| 70 | panic(fmt.Sprintf("Failed to find flag %q and mark it as being in a mutually exclusive flag group", v)) |
| 71 | } |
| 72 | // Each time this is called is a single new entry; this allows it to be a member of multiple groups if needed. |
| 73 | if err := c.Flags().SetAnnotation(v, mutuallyExclusiveAnnotation, append(f.Annotations[mutuallyExclusiveAnnotation], strings.Join(flagNames, " "))); err != nil { |
| 74 | panic(err) |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | // ValidateFlagGroups validates the mutuallyExclusive/oneRequired/requiredAsGroup logic and returns the |
| 80 | // first error encountered. |