MCPcopy
hub / github.com/spf13/cobra / MarkFlagsMutuallyExclusive

Method MarkFlagsMutuallyExclusive

flag_groups.go:65–77  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.
65func (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.

Callers 2

TestValidateFlagGroupsFunction · 0.95

Calls 2

mergePersistentFlagsMethod · 0.95
FlagsMethod · 0.95

Tested by 2

TestValidateFlagGroupsFunction · 0.76