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

Method MarkFlagsRequiredTogether

flag_groups.go:33–45  ·  view source on GitHub ↗

MarkFlagsRequiredTogether marks the given flags with annotations so that Cobra errors if the command is invoked with a subset (but not all) of the given flags.

(flagNames ...string)

Source from the content-addressed store, hash-verified

31// MarkFlagsRequiredTogether marks the given flags with annotations so that Cobra errors
32// if the command is invoked with a subset (but not all) of the given flags.
33func (c *Command) MarkFlagsRequiredTogether(flagNames ...string) {
34 c.mergePersistentFlags()
35 for _, v := range flagNames {
36 f := c.Flags().Lookup(v)
37 if f == nil {
38 panic(fmt.Sprintf("Failed to find flag %q and mark it as being required in a flag group", v))
39 }
40 if err := c.Flags().SetAnnotation(v, requiredAsGroupAnnotation, append(f.Annotations[requiredAsGroupAnnotation], strings.Join(flagNames, " "))); err != nil {
41 // Only errs if the flag isn't found.
42 panic(err)
43 }
44 }
45}
46
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.

Callers 2

TestValidateFlagGroupsFunction · 0.95

Calls 2

mergePersistentFlagsMethod · 0.95
FlagsMethod · 0.95

Tested by 2

TestValidateFlagGroupsFunction · 0.76