Skip to content

Commit

Permalink
Fix shell completion
Browse files Browse the repository at this point in the history
  • Loading branch information
marevers committed Jun 13, 2023
1 parent 4af07a5 commit 3fac363
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions flag_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ func sortedKeys(m map[string]map[string]bool) []string {

// enforceFlagGroupsForCompletion will do the following:
// - 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
// This allows the standard completion logic to behave appropriately for flag groups
func (c *Command) enforceFlagGroupsForCompletion() {
Expand All @@ -228,9 +229,11 @@ func (c *Command) enforceFlagGroupsForCompletion() {

flags := c.Flags()
groupStatus := map[string]map[string]bool{}
oneRequiredGroupStatus := map[string]map[string]bool{}
mutuallyExclusiveGroupStatus := map[string]map[string]bool{}
c.Flags().VisitAll(func(pflag *flag.Flag) {
processFlagForGroupAnnotation(flags, pflag, requiredAsGroup, groupStatus)
processFlagForGroupAnnotation(flags, pflag, oneRequired, oneRequiredGroupStatus)
processFlagForGroupAnnotation(flags, pflag, mutuallyExclusive, mutuallyExclusiveGroupStatus)
})

Expand All @@ -247,6 +250,26 @@ func (c *Command) enforceFlagGroupsForCompletion() {
}
}

// If none of the flags of a one-required group are present, we make all the flags
// of that group required so that the shell completion suggests them automatically
for flagList, flagnameAndStatus := range oneRequiredGroupStatus {
set := 0

for _, isSet := range flagnameAndStatus {
if isSet {
set++
}
}

// None of the flags of the group are set, mark all flags in the group
// as required
if set == 0 {
for _, fName := range strings.Split(flagList, " ") {
_ = c.MarkFlagRequired(fName)
}
}
}

// If a flag that is mutually exclusive to others is present, we hide the other
// flags of that group so the shell completion does not suggest them
for flagList, flagnameAndStatus := range mutuallyExclusiveGroupStatus {
Expand Down

0 comments on commit 3fac363

Please sign in to comment.