Skip to content

Commit 5c2c1d6

Browse files
authored
Consistent annotation names (#2140)
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.
1 parent 5a1acea commit 5c2c1d6

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

flag_groups.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ import (
2323
)
2424

2525
const (
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"
2929
)
3030

3131
// MarkFlagsRequiredTogether marks the given flags with annotations so that Cobra errors
@@ -37,7 +37,7 @@ func (c *Command) MarkFlagsRequiredTogether(flagNames ...string) {
3737
if f == nil {
3838
panic(fmt.Sprintf("Failed to find flag %q and mark it as being required in a flag group", v))
3939
}
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 {
4141
// Only errs if the flag isn't found.
4242
panic(err)
4343
}
@@ -53,7 +53,7 @@ func (c *Command) MarkFlagsOneRequired(flagNames ...string) {
5353
if f == nil {
5454
panic(fmt.Sprintf("Failed to find flag %q and mark it as being in a one-required flag group", v))
5555
}
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 {
5757
// Only errs if the flag isn't found.
5858
panic(err)
5959
}
@@ -70,7 +70,7 @@ func (c *Command) MarkFlagsMutuallyExclusive(flagNames ...string) {
7070
panic(fmt.Sprintf("Failed to find flag %q and mark it as being in a mutually exclusive flag group", v))
7171
}
7272
// 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 {
7474
panic(err)
7575
}
7676
}
@@ -91,9 +91,9 @@ func (c *Command) ValidateFlagGroups() error {
9191
oneRequiredGroupStatus := map[string]map[string]bool{}
9292
mutuallyExclusiveGroupStatus := map[string]map[string]bool{}
9393
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)
9797
})
9898

9999
if err := validateRequiredFlagGroups(groupStatus); err != nil {
@@ -232,9 +232,9 @@ func (c *Command) enforceFlagGroupsForCompletion() {
232232
oneRequiredGroupStatus := map[string]map[string]bool{}
233233
mutuallyExclusiveGroupStatus := map[string]map[string]bool{}
234234
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)
238238
})
239239

240240
// If a flag that is part of a group is present, we make all the other flags

0 commit comments

Comments
 (0)