ValidateFlagGroups validates the mutuallyExclusive/oneRequired/requiredAsGroup logic and returns the first error encountered.
()
| 79 | // ValidateFlagGroups validates the mutuallyExclusive/oneRequired/requiredAsGroup logic and returns the |
| 80 | // first error encountered. |
| 81 | func (c *Command) ValidateFlagGroups() error { |
| 82 | if c.DisableFlagParsing { |
| 83 | return nil |
| 84 | } |
| 85 | |
| 86 | flags := c.Flags() |
| 87 | |
| 88 | // groupStatus format is the list of flags as a unique ID, |
| 89 | // then a map of each flag name and whether it is set or not. |
| 90 | groupStatus := map[string]map[string]bool{} |
| 91 | oneRequiredGroupStatus := map[string]map[string]bool{} |
| 92 | mutuallyExclusiveGroupStatus := map[string]map[string]bool{} |
| 93 | flags.VisitAll(func(pflag *flag.Flag) { |
| 94 | processFlagForGroupAnnotation(flags, pflag, requiredAsGroupAnnotation, groupStatus) |
| 95 | processFlagForGroupAnnotation(flags, pflag, oneRequiredAnnotation, oneRequiredGroupStatus) |
| 96 | processFlagForGroupAnnotation(flags, pflag, mutuallyExclusiveAnnotation, mutuallyExclusiveGroupStatus) |
| 97 | }) |
| 98 | |
| 99 | if err := validateRequiredFlagGroups(groupStatus); err != nil { |
| 100 | return err |
| 101 | } |
| 102 | if err := validateOneRequiredFlagGroups(oneRequiredGroupStatus); err != nil { |
| 103 | return err |
| 104 | } |
| 105 | if err := validateExclusiveFlagGroups(mutuallyExclusiveGroupStatus); err != nil { |
| 106 | return err |
| 107 | } |
| 108 | return nil |
| 109 | } |
| 110 | |
| 111 | func hasAllFlags(fs *flag.FlagSet, flagnames ...string) bool { |
| 112 | for _, fname := range flagnames { |
no test coverage detected