mirror of
https://github.com/spf13/cobra
synced 2025-01-30 15:36:47 +00:00
fix test error message
This commit is contained in:
parent
1c44c42794
commit
dd7bc42aa8
2 changed files with 11 additions and 11 deletions
|
@ -23,10 +23,10 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
requiredAsGroup = "cobra_annotation_required_if_others_set"
|
||||
oneRequired = "cobra_annotation_one_required"
|
||||
mutuallyExclusive = "cobra_annotation_mutually_exclusive"
|
||||
ifPresentThenOthersRequired = "cobra_annotation_if_present_then_others_required"
|
||||
requiredAsGroupAnnotation = "cobra_annotation_required_if_others_set"
|
||||
oneRequiredAnnotation = "cobra_annotation_one_required"
|
||||
mutuallyExclusiveAnnotation = "cobra_annotation_mutually_exclusive"
|
||||
ifPresentThenOthersRequiredAnnotation = "cobra_annotation_if_present_then_others_required"
|
||||
)
|
||||
|
||||
// MarkFlagsRequiredTogether marks the given flags with annotations so that Cobra errors
|
||||
|
@ -90,7 +90,7 @@ func (c *Command) MarkIfFlagPresentThenOthersRequired(flagNames ...string) {
|
|||
panic(fmt.Sprintf("Failed to find flag %q and mark it as being in an if present then others required flag group", v))
|
||||
}
|
||||
// Each time this is called is a single new entry; this allows it to be a member of multiple groups if needed.
|
||||
if err := c.Flags().SetAnnotation(v, ifPresentThenOthersRequired, append(f.Annotations[ifPresentThenOthersRequired], strings.Join(flagNames, " "))); err != nil {
|
||||
if err := c.Flags().SetAnnotation(v, ifPresentThenOthersRequiredAnnotation, append(f.Annotations[ifPresentThenOthersRequiredAnnotation], strings.Join(flagNames, " "))); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
@ -115,7 +115,7 @@ func (c *Command) ValidateFlagGroups() error {
|
|||
processFlagForGroupAnnotation(flags, pflag, requiredAsGroupAnnotation, groupStatus)
|
||||
processFlagForGroupAnnotation(flags, pflag, oneRequiredAnnotation, oneRequiredGroupStatus)
|
||||
processFlagForGroupAnnotation(flags, pflag, mutuallyExclusiveAnnotation, mutuallyExclusiveGroupStatus)
|
||||
processFlagForGroupAnnotation(flags, pflag, ifPresentThenOthersRequired, ifPresentThenOthersRequiredGroupStatus)
|
||||
processFlagForGroupAnnotation(flags, pflag, ifPresentThenOthersRequiredAnnotation, ifPresentThenOthersRequiredGroupStatus)
|
||||
})
|
||||
|
||||
if err := validateRequiredFlagGroups(groupStatus); err != nil {
|
||||
|
@ -255,7 +255,7 @@ func validateIfPresentThenRequiredFlagGroups(data map[string]map[string]bool) er
|
|||
if len(unset) > 0 {
|
||||
return fmt.Errorf(
|
||||
"%v is set, the following flags must be provided: %v",
|
||||
flagList[0], unset,
|
||||
primaryFlag, unset,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -294,7 +294,7 @@ func (c *Command) enforceFlagGroupsForCompletion() {
|
|||
processFlagForGroupAnnotation(flags, pflag, requiredAsGroupAnnotation, groupStatus)
|
||||
processFlagForGroupAnnotation(flags, pflag, oneRequiredAnnotation, oneRequiredGroupStatus)
|
||||
processFlagForGroupAnnotation(flags, pflag, mutuallyExclusiveAnnotation, mutuallyExclusiveGroupStatus)
|
||||
processFlagForGroupAnnotation(flags, pflag, ifPresentThenOthersRequired, ifPresentThenRequiredGroupStatus)
|
||||
processFlagForGroupAnnotation(flags, pflag, ifPresentThenOthersRequiredAnnotation, ifPresentThenRequiredGroupStatus)
|
||||
})
|
||||
|
||||
// If a flag that is part of a group is present, we make all the other flags
|
||||
|
|
|
@ -81,7 +81,7 @@ func TestValidateFlagGroups(t *testing.T) {
|
|||
desc: "If present then others required flag group not satisfied",
|
||||
flagGroupsIfPresentThenRequired: []string{"a b"},
|
||||
args: []string{"--a=foo"},
|
||||
expectErr: "if the first flag in the group [a b] is set, all other flags must be set; the following flags are not set: [b]",
|
||||
expectErr: "a is set, the following flags must be provided: [b]",
|
||||
}, {
|
||||
desc: "Multiple required flag group not satisfied returns first error",
|
||||
flagGroupsRequired: []string{"a b c", "a d"},
|
||||
|
@ -102,7 +102,7 @@ func TestValidateFlagGroups(t *testing.T) {
|
|||
desc: "Multiple if present then others required flag group not satisfied returns first error",
|
||||
flagGroupsIfPresentThenRequired: []string{"a b", "d e"},
|
||||
args: []string{"--a=foo", "--f=foo"},
|
||||
expectErr: `if the first flag in the group [a b] is set, all other flags must be set; the following flags are not set: [b]`,
|
||||
expectErr: `a is set, the following flags must be provided: [b]`,
|
||||
}, {
|
||||
desc: "Validation of required groups occurs on groups in sorted order",
|
||||
flagGroupsRequired: []string{"a d", "a b", "a c"},
|
||||
|
|
Loading…
Reference in a new issue