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)
| 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. |
| 33 | func (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. |