mirror of
https://github.com/spf13/cobra
synced 2025-05-05 04:47:22 +00:00
feat: optimise markflagsdependson/markflagdependsonany by removing specialStatusInfo (#1759)
This commit is contained in:
parent
3ebb4c3c31
commit
d5bea272b1
1 changed files with 12 additions and 20 deletions
|
@ -105,17 +105,12 @@ func (c *Command) setFlagAnnotation(flag string, annotation string, message stri
|
||||||
|
|
||||||
// The 'special-ness' of a group means that the first member of the group carries
|
// The 'special-ness' of a group means that the first member of the group carries
|
||||||
// special meaning. In contrast to the other group types, where all members are equal.
|
// special meaning. In contrast to the other group types, where all members are equal.
|
||||||
type specialStatusInfo struct {
|
type specialStatusInfoData map[string]bool
|
||||||
isSet bool
|
|
||||||
isSpecial bool
|
|
||||||
}
|
|
||||||
type specialStatusInfoData map[string]*specialStatusInfo
|
|
||||||
|
|
||||||
type specialGroupInfo struct {
|
type specialGroupInfo struct {
|
||||||
special string
|
special string
|
||||||
others []string
|
others []string
|
||||||
// maps the flag name to special status info
|
data specialStatusInfoData // maps the flag name to special status info
|
||||||
data specialStatusInfoData
|
|
||||||
}
|
}
|
||||||
type specialGroupInfoCollection map[string]*specialGroupInfo
|
type specialGroupInfoCollection map[string]*specialGroupInfo
|
||||||
|
|
||||||
|
@ -220,7 +215,7 @@ func processFlagForSpecialGroupAnnotation(flags *flag.FlagSet, pflag *flag.Flag,
|
||||||
flagnames := strings.Split(group, " ")
|
flagnames := strings.Split(group, " ")
|
||||||
// it's important to know that the order of the flags is established
|
// it's important to know that the order of the flags is established
|
||||||
// in setFlagAnnotation, which makes the assumption of the first
|
// in setFlagAnnotation, which makes the assumption of the first
|
||||||
// item being sepcial, being valid
|
// item being special, being valid
|
||||||
special := flagnames[0]
|
special := flagnames[0]
|
||||||
others := flagnames[1:]
|
others := flagnames[1:]
|
||||||
isFlagSpecial := pflag.Name == special
|
isFlagSpecial := pflag.Name == special
|
||||||
|
@ -237,10 +232,9 @@ func processFlagForSpecialGroupAnnotation(flags *flag.FlagSet, pflag *flag.Flag,
|
||||||
|
|
||||||
groupStatus[group] = newSpecialGroup(special, others)
|
groupStatus[group] = newSpecialGroup(special, others)
|
||||||
for _, name := range flagnames {
|
for _, name := range flagnames {
|
||||||
groupStatus[group].data[name] = &specialStatusInfo{}
|
groupStatus[group].data[name] = false
|
||||||
|
|
||||||
if name == special {
|
if name == special {
|
||||||
groupStatus[group].data[special].isSpecial = true
|
|
||||||
break // short circuit after finding special
|
break // short circuit after finding special
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -249,9 +243,9 @@ func processFlagForSpecialGroupAnnotation(flags *flag.FlagSet, pflag *flag.Flag,
|
||||||
// group exists, but we still need to check if the flag exists in the group,
|
// group exists, but we still need to check if the flag exists in the group,
|
||||||
// because the previous loop is short circuited as soon as we find the special.
|
// because the previous loop is short circuited as soon as we find the special.
|
||||||
if _, found := groupStatus[group].data[pflag.Name]; !found {
|
if _, found := groupStatus[group].data[pflag.Name]; !found {
|
||||||
groupStatus[group].data[pflag.Name] = &specialStatusInfo{}
|
groupStatus[group].data[pflag.Name] = false
|
||||||
}
|
}
|
||||||
groupStatus[group].data[pflag.Name].isSet = pflag.Changed
|
groupStatus[group].data[pflag.Name] = pflag.Changed
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -306,17 +300,16 @@ func validateDependsOnFlagGroups(data specialGroupInfoCollection) error {
|
||||||
for _, flagList := range keys {
|
for _, flagList := range keys {
|
||||||
flagnameAndStatus := data[flagList]
|
flagnameAndStatus := data[flagList]
|
||||||
|
|
||||||
if flagnameAndStatus.data[flagnameAndStatus.special].isSet {
|
if flagnameAndStatus.data[flagnameAndStatus.special] {
|
||||||
// rule is satisfied, because the special flag is present, regardless of
|
// rule is satisfied, because the special flag is present, regardless of
|
||||||
// the presence of the other members in the group
|
// the presence of the other members in the group
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// we have a problem if at least one of present is set, because special
|
// we have a problem if at least one of present is set, because special is not set
|
||||||
// is not set
|
|
||||||
present := []string{}
|
present := []string{}
|
||||||
for _, o := range flagnameAndStatus.others {
|
for _, o := range flagnameAndStatus.others {
|
||||||
if flagnameAndStatus.data[o].isSet {
|
if flagnameAndStatus.data[o] {
|
||||||
present = append(present, o)
|
present = append(present, o)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -338,13 +331,13 @@ func validateDependsOnAnyFlagGroups(data specialGroupInfoCollection) error {
|
||||||
|
|
||||||
for _, flagList := range keys {
|
for _, flagList := range keys {
|
||||||
flagnameAndStatus := data[flagList]
|
flagnameAndStatus := data[flagList]
|
||||||
if !flagnameAndStatus.data[flagnameAndStatus.special].isSet {
|
if !flagnameAndStatus.data[flagnameAndStatus.special] {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
present := []string{}
|
present := []string{}
|
||||||
for _, o := range flagnameAndStatus.others {
|
for _, o := range flagnameAndStatus.others {
|
||||||
if flagnameAndStatus.data[o].isSet {
|
if flagnameAndStatus.data[o] {
|
||||||
present = append(present, o)
|
present = append(present, o)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -436,11 +429,10 @@ func (c *Command) enforceFlagGroupsForCompletion() {
|
||||||
// if any of others is set, then mark special as required
|
// if any of others is set, then mark special as required
|
||||||
for _, flagnameAndStatus := range dependsOnSpecialGroupStatus {
|
for _, flagnameAndStatus := range dependsOnSpecialGroupStatus {
|
||||||
for _, o := range flagnameAndStatus.others {
|
for _, o := range flagnameAndStatus.others {
|
||||||
if flagnameAndStatus.data[o].isSet {
|
if flagnameAndStatus.data[o] {
|
||||||
c.MarkFlagRequired(flagnameAndStatus.special)
|
c.MarkFlagRequired(flagnameAndStatus.special)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// we can't aid the completion process for dependsOnAny
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue