Skip to content
Myr

Consistent annotation names (#2140)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Add `Annotation` suffix to the private annotations to allow nicer code using the constants. For example one can use the current annotation names as a temporary variable instead of unclear shortcut. Instead of this: rag := flagsFromAnnotation(c, f, requiredAsGroup) me := flagsFromAnnotation(c, f, mutuallyExclusive) or := flagsFromAnnotation(c, f, oneRequired) We can use now: requiredAsGrop := flagsFromAnnotation(c, f, requiredAsGroupAnnotation) mutuallyExclusive := flagsFromAnnotation(c, f, mutuallyExclusiveAnnotation) oneRequired := flagsFromAnnotation(c, f, oneRequiredAnnotation) Example taken from #2105.

authored by Nir Soffer · committed by GitHub · parent 5a1acea · Browse files · 1 file+12 added−12 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+12 added−12 removed

  • other:+12 added−12 removed

1 file+12 added−12 removed

modified:flag_groups.go+12 added-12 removed
21 unmodified lines
22 flag "github.com/spf13/pflag"23)2425const (26 requiredAsGroup = "cobra_annotation_required_if_others_set"27 oneRequired = "cobra_annotation_one_required"28 mutuallyExclusive = "cobra_annotation_mutually_exclusive"26 requiredAsGroupAnnotation = "cobra_annotation_required_if_others_set"27 oneRequiredAnnotation = "cobra_annotation_one_required"28 mutuallyExclusiveAnnotation = "cobra_annotation_mutually_exclusive"29)3031// MarkFlagsRequiredTogether marks the given flags with annotations so that Cobra errors32// if the command is invoked with a subset (but not all) of the given flags.
3 unmodified lines
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, requiredAsGroup, append(f.Annotations[requiredAsGroup], strings.Join(flagNames, " "))); err != nil {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 }
7 unmodified lines
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, oneRequired, append(f.Annotations[oneRequired], strings.Join(flagNames, " "))); err != nil {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 }
8 unmodified lines
69 if f == nil {70 panic(fmt.Sprintf("Failed to find flag %q and mark it as being in a mutually exclusive flag group", v))71 }72 // Each time this is called is a single new entry; this allows it to be a member of multiple groups if needed.73 if err := c.Flags().SetAnnotation(v, mutuallyExclusive, append(f.Annotations[mutuallyExclusive], strings.Join(flagNames, " "))); err != nil {73 if err := c.Flags().SetAnnotation(v, mutuallyExclusiveAnnotation, append(f.Annotations[mutuallyExclusiveAnnotation], strings.Join(flagNames, " "))); err != nil {74 panic(err)75 }76 }77}
12 unmodified lines
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, requiredAsGroup, groupStatus)95 processFlagForGroupAnnotation(flags, pflag, oneRequired, oneRequiredGroupStatus)96 processFlagForGroupAnnotation(flags, pflag, mutuallyExclusive, mutuallyExclusiveGroupStatus)94 processFlagForGroupAnnotation(flags, pflag, requiredAsGroupAnnotation, groupStatus)95 processFlagForGroupAnnotation(flags, pflag, oneRequiredAnnotation, oneRequiredGroupStatus)96 processFlagForGroupAnnotation(flags, pflag, mutuallyExclusiveAnnotation, mutuallyExclusiveGroupStatus)97 })9899 if err := validateRequiredFlagGroups(groupStatus); err != nil {100 return err
130 unmodified lines
231 groupStatus := map[string]map[string]bool{}232 oneRequiredGroupStatus := map[string]map[string]bool{}233 mutuallyExclusiveGroupStatus := map[string]map[string]bool{}234 c.Flags().VisitAll(func(pflag *flag.Flag) {235 processFlagForGroupAnnotation(flags, pflag, requiredAsGroup, groupStatus)236 processFlagForGroupAnnotation(flags, pflag, oneRequired, oneRequiredGroupStatus)237 processFlagForGroupAnnotation(flags, pflag, mutuallyExclusive, mutuallyExclusiveGroupStatus)235 processFlagForGroupAnnotation(flags, pflag, requiredAsGroupAnnotation, groupStatus)236 processFlagForGroupAnnotation(flags, pflag, oneRequiredAnnotation, oneRequiredGroupStatus)237 processFlagForGroupAnnotation(flags, pflag, mutuallyExclusiveAnnotation, mutuallyExclusiveGroupStatus)238 })239240 // If a flag that is part of a group is present, we make all the other flags241 // of that group required so that the shell completion suggests them automatically
49 unmodified lines