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