mirror of
https://github.com/spf13/cobra.git
synced 2024-12-19 18:15:20 -07:00
Compare commits
5 Commits
bd414b05d2
...
6d78a0186d
Author | SHA1 | Date | |
---|---|---|---|
|
6d78a0186d | ||
|
9f9056765c | ||
|
23cadf7ab4 | ||
|
49443501ed | ||
|
ca3972ed08 |
@ -23,9 +23,10 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
requiredAsGroupAnnotation = "cobra_annotation_required_if_others_set"
|
||||
oneRequiredAnnotation = "cobra_annotation_one_required"
|
||||
mutuallyExclusiveAnnotation = "cobra_annotation_mutually_exclusive"
|
||||
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"
|
||||
)
|
||||
|
||||
// MarkFlagsRequiredTogether marks the given flags with annotations so that Cobra errors
|
||||
@ -76,6 +77,25 @@ func (c *Command) MarkFlagsMutuallyExclusive(flagNames ...string) {
|
||||
}
|
||||
}
|
||||
|
||||
// MarkIfFlagPresentThenOthersRequired marks the given flags so that if the first flag is set,
|
||||
// all the other flags become required.
|
||||
func (c *Command) MarkIfFlagPresentThenOthersRequired(flagNames ...string) {
|
||||
if len(flagNames) < 2 {
|
||||
panic("MarkIfFlagPresentThenRequired requires at least two flags")
|
||||
}
|
||||
c.mergePersistentFlags()
|
||||
for _, v := range flagNames {
|
||||
f := c.Flags().Lookup(v)
|
||||
if f == nil {
|
||||
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 {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ValidateFlagGroups validates the mutuallyExclusive/oneRequired/requiredAsGroup logic and returns the
|
||||
// first error encountered.
|
||||
func (c *Command) ValidateFlagGroups() error {
|
||||
@ -90,10 +110,12 @@ func (c *Command) ValidateFlagGroups() error {
|
||||
groupStatus := map[string]map[string]bool{}
|
||||
oneRequiredGroupStatus := map[string]map[string]bool{}
|
||||
mutuallyExclusiveGroupStatus := map[string]map[string]bool{}
|
||||
ifPresentThenOthersRequiredGroupStatus := map[string]map[string]bool{}
|
||||
flags.VisitAll(func(pflag *flag.Flag) {
|
||||
processFlagForGroupAnnotation(flags, pflag, requiredAsGroupAnnotation, groupStatus)
|
||||
processFlagForGroupAnnotation(flags, pflag, oneRequiredAnnotation, oneRequiredGroupStatus)
|
||||
processFlagForGroupAnnotation(flags, pflag, mutuallyExclusiveAnnotation, mutuallyExclusiveGroupStatus)
|
||||
processFlagForGroupAnnotation(flags, pflag, ifPresentThenOthersRequired, ifPresentThenOthersRequiredGroupStatus)
|
||||
})
|
||||
|
||||
if err := validateRequiredFlagGroups(groupStatus); err != nil {
|
||||
@ -105,6 +127,9 @@ func (c *Command) ValidateFlagGroups() error {
|
||||
if err := validateExclusiveFlagGroups(mutuallyExclusiveGroupStatus); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := validateIfPresentThenRequiredFlagGroups(ifPresentThenOthersRequiredGroupStatus); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -206,6 +231,38 @@ func validateExclusiveFlagGroups(data map[string]map[string]bool) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateIfPresentThenRequiredFlagGroups(data map[string]map[string]bool) error {
|
||||
for flagList, flagnameAndStatus := range data {
|
||||
flags := strings.Split(flagList, " ")
|
||||
primaryFlag := flags[0]
|
||||
remainingFlags := flags[1:]
|
||||
|
||||
// Handle missing primary flag entry
|
||||
if _, exists := flagnameAndStatus[primaryFlag]; !exists {
|
||||
flagnameAndStatus[primaryFlag] = false
|
||||
}
|
||||
|
||||
// Check if the primary flag is set
|
||||
if flagnameAndStatus[primaryFlag] {
|
||||
var unset []string
|
||||
for _, flag := range remainingFlags {
|
||||
if !flagnameAndStatus[flag] {
|
||||
unset = append(unset, flag)
|
||||
}
|
||||
}
|
||||
|
||||
// If any dependent flags are unset, trigger an error
|
||||
if len(unset) > 0 {
|
||||
return fmt.Errorf(
|
||||
"if the first flag in the group [%v] is set, all other flags must be set; the following flags are not set: %v",
|
||||
flagList, unset,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func sortedKeys(m map[string]map[string]bool) []string {
|
||||
keys := make([]string, len(m))
|
||||
i := 0
|
||||
@ -221,6 +278,7 @@ func sortedKeys(m map[string]map[string]bool) []string {
|
||||
// - when a flag in a group is present, other flags in the group will be marked required
|
||||
// - when none of the flags in a one-required group are present, all flags in the group will be marked required
|
||||
// - when a flag in a mutually exclusive group is present, other flags in the group will be marked as hidden
|
||||
// - when the first flag in an if-present-then-required group is present, the second flag will be marked as required
|
||||
// This allows the standard completion logic to behave appropriately for flag groups
|
||||
func (c *Command) enforceFlagGroupsForCompletion() {
|
||||
if c.DisableFlagParsing {
|
||||
@ -231,10 +289,12 @@ func (c *Command) enforceFlagGroupsForCompletion() {
|
||||
groupStatus := map[string]map[string]bool{}
|
||||
oneRequiredGroupStatus := map[string]map[string]bool{}
|
||||
mutuallyExclusiveGroupStatus := map[string]map[string]bool{}
|
||||
ifPresentThenRequiredGroupStatus := map[string]map[string]bool{}
|
||||
c.Flags().VisitAll(func(pflag *flag.Flag) {
|
||||
processFlagForGroupAnnotation(flags, pflag, requiredAsGroupAnnotation, groupStatus)
|
||||
processFlagForGroupAnnotation(flags, pflag, oneRequiredAnnotation, oneRequiredGroupStatus)
|
||||
processFlagForGroupAnnotation(flags, pflag, mutuallyExclusiveAnnotation, mutuallyExclusiveGroupStatus)
|
||||
processFlagForGroupAnnotation(flags, pflag, ifPresentThenOthersRequired, ifPresentThenRequiredGroupStatus)
|
||||
})
|
||||
|
||||
// If a flag that is part of a group is present, we make all the other flags
|
||||
@ -287,4 +347,17 @@ func (c *Command) enforceFlagGroupsForCompletion() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If a flag that is marked as if-present-then-required is present, make other flags in the group required
|
||||
for flagList, flagnameAndStatus := range ifPresentThenRequiredGroupStatus {
|
||||
flags := strings.Split(flagList, " ")
|
||||
primaryFlag := flags[0]
|
||||
remainingFlags := flags[1:]
|
||||
|
||||
if flagnameAndStatus[primaryFlag] {
|
||||
for _, fName := range remainingFlags {
|
||||
_ = c.MarkFlagRequired(fName)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -47,9 +47,11 @@ func TestValidateFlagGroups(t *testing.T) {
|
||||
flagGroupsRequired []string
|
||||
flagGroupsOneRequired []string
|
||||
flagGroupsExclusive []string
|
||||
flagGroupsIfPresentThenRequired []string
|
||||
subCmdFlagGroupsRequired []string
|
||||
subCmdFlagGroupsOneRequired []string
|
||||
subCmdFlagGroupsExclusive []string
|
||||
subCmdFlagGroupsIfPresentThenRequired []string
|
||||
args []string
|
||||
expectErr string
|
||||
}{
|
||||
@ -59,6 +61,7 @@ func TestValidateFlagGroups(t *testing.T) {
|
||||
desc: "No flags no problem even with conflicting groups",
|
||||
flagGroupsRequired: []string{"a b"},
|
||||
flagGroupsExclusive: []string{"a b"},
|
||||
flagGroupsIfPresentThenRequired: []string{"a b"},
|
||||
}, {
|
||||
desc: "Required flag group not satisfied",
|
||||
flagGroupsRequired: []string{"a b c"},
|
||||
@ -74,6 +77,11 @@ func TestValidateFlagGroups(t *testing.T) {
|
||||
flagGroupsExclusive: []string{"a b c"},
|
||||
args: []string{"--a=foo", "--b=foo"},
|
||||
expectErr: "if any flags in the group [a b c] are set none of the others can be; [a b] were all set",
|
||||
}, {
|
||||
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]",
|
||||
}, {
|
||||
desc: "Multiple required flag group not satisfied returns first error",
|
||||
flagGroupsRequired: []string{"a b c", "a d"},
|
||||
@ -89,6 +97,12 @@ func TestValidateFlagGroups(t *testing.T) {
|
||||
flagGroupsExclusive: []string{"a b c", "a d"},
|
||||
args: []string{"--a=foo", "--c=foo", "--d=foo"},
|
||||
expectErr: `if any flags in the group [a b c] are set none of the others can be; [a c] were all set`,
|
||||
},
|
||||
{
|
||||
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]`,
|
||||
}, {
|
||||
desc: "Validation of required groups occurs on groups in sorted order",
|
||||
flagGroupsRequired: []string{"a d", "a b", "a c"},
|
||||
@ -182,6 +196,12 @@ func TestValidateFlagGroups(t *testing.T) {
|
||||
for _, flagGroup := range tc.subCmdFlagGroupsExclusive {
|
||||
sub.MarkFlagsMutuallyExclusive(strings.Split(flagGroup, " ")...)
|
||||
}
|
||||
for _, flagGroup := range tc.flagGroupsIfPresentThenRequired {
|
||||
c.MarkIfFlagPresentThenOthersRequired(strings.Split(flagGroup, " ")...)
|
||||
}
|
||||
for _, flagGroup := range tc.subCmdFlagGroupsIfPresentThenRequired {
|
||||
sub.MarkIfFlagPresentThenOthersRequired(strings.Split(flagGroup, " ")...)
|
||||
}
|
||||
c.SetArgs(tc.args)
|
||||
err := c.Execute()
|
||||
switch {
|
||||
|
2
go.mod
2
go.mod
@ -3,7 +3,7 @@ module github.com/spf13/cobra
|
||||
go 1.15
|
||||
|
||||
require (
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.5
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.6
|
||||
github.com/inconshreveable/mousetrap v1.1.0
|
||||
github.com/spf13/pflag v1.0.5
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
|
4
go.sum
4
go.sum
@ -1,5 +1,5 @@
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.5 h1:ZtcqGrnekaHpVLArFSe4HK5DoKx1T0rq2DwVB0alcyc=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.5/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.6 h1:XJtiaUW6dEEqVuZiMTn1ldk455QWwEIsMIJlo5vtkx0=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
|
||||
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
||||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
||||
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
|
||||
|
Loading…
Reference in New Issue
Block a user