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

Method MarkFlagsOneRequired

flag_groups.go:49–61  ·  view source on GitHub ↗

MarkFlagsOneRequired marks the given flags with annotations so that Cobra errors if the command is invoked without at least one flag from the given set of flags.

(flagNames ...string)

Source from the content-addressed store, hash-verified

47// MarkFlagsOneRequired marks the given flags with annotations so that Cobra errors
48// if the command is invoked without at least one flag from the given set of flags.
49func (c *Command) MarkFlagsOneRequired(flagNames ...string) {
50 c.mergePersistentFlags()
51 for _, v := range flagNames {
52 f := c.Flags().Lookup(v)
53 if f == nil {
54 panic(fmt.Sprintf("Failed to find flag %q and mark it as being in a one-required flag group", v))
55 }
56 if err := c.Flags().SetAnnotation(v, oneRequiredAnnotation, append(f.Annotations[oneRequiredAnnotation], strings.Join(flagNames, " "))); err != nil {
57 // Only errs if the flag isn't found.
58 panic(err)
59 }
60 }
61}
62
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.

Callers 2

TestValidateFlagGroupsFunction · 0.95

Calls 2

mergePersistentFlagsMethod · 0.95
FlagsMethod · 0.95

Tested by 2

TestValidateFlagGroupsFunction · 0.76