Skip to content

Commit

Permalink
Adjust flag groups completion for zulu
Browse files Browse the repository at this point in the history
  • Loading branch information
hoshsadiq committed Aug 24, 2022
1 parent 978c760 commit 7f3b34a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
4 changes: 4 additions & 0 deletions completions.go
Expand Up @@ -672,6 +672,10 @@ to enable it. You can execute the following once:
echo "autoload -U compinit; compinit" >> ~/.zshrc
To load completions in your current shell session:
source <(%[1]s completion zsh); compdef _%[1]s %[1]s
To load completions for every new session, execute once:
#### Linux:
Expand Down
36 changes: 18 additions & 18 deletions completions_test.go
Expand Up @@ -2635,17 +2635,17 @@ func TestFixedCompletions(t *testing.T) {
}

func TestCompletionForGroupedFlags(t *testing.T) {
getCmd := func() *Command {
rootCmd := &Command{
Use: "root",
Run: emptyRun,
getCmd := func() *zulu.Command {
rootCmd := &zulu.Command{
Use: "root",
RunE: emptyRun,
}
childCmd := &Command{
childCmd := &zulu.Command{
Use: "child",
ValidArgsFunction: func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
return []string{"subArg"}, ShellCompDirectiveNoFileComp
ValidArgsFunction: func(cmd *zulu.Command, args []string, toComplete string) ([]string, zulu.ShellCompDirective) {
return []string{"subArg"}, zulu.ShellCompDirectiveNoFileComp
},
Run: emptyRun,
RunE: emptyRun,
}
rootCmd.AddCommand(childCmd)

Expand Down Expand Up @@ -2719,7 +2719,7 @@ func TestCompletionForGroupedFlags(t *testing.T) {
for _, tc := range testcases {
t.Run(tc.desc, func(t *testing.T) {
c := getCmd()
args := []string{ShellCompNoDescRequestCmd}
args := []string{zulu.ShellCompNoDescRequestCmd}
args = append(args, tc.args...)
output, err := executeCommand(c, args...)
switch {
Expand All @@ -2733,17 +2733,17 @@ func TestCompletionForGroupedFlags(t *testing.T) {
}

func TestCompletionForMutuallyExclusiveFlags(t *testing.T) {
getCmd := func() *Command {
rootCmd := &Command{
Use: "root",
Run: emptyRun,
getCmd := func() *zulu.Command {
rootCmd := &zulu.Command{
Use: "root",
RunE: emptyRun,
}
childCmd := &Command{
childCmd := &zulu.Command{
Use: "child",
ValidArgsFunction: func(cmd *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
return []string{"subArg"}, ShellCompDirectiveNoFileComp
ValidArgsFunction: func(cmd *zulu.Command, args []string, toComplete string) ([]string, zulu.ShellCompDirective) {
return []string{"subArg"}, zulu.ShellCompDirectiveNoFileComp
},
Run: emptyRun,
RunE: emptyRun,
}
rootCmd.AddCommand(childCmd)

Expand Down Expand Up @@ -2807,7 +2807,7 @@ func TestCompletionForMutuallyExclusiveFlags(t *testing.T) {
for _, tc := range testcases {
t.Run(tc.desc, func(t *testing.T) {
c := getCmd()
args := []string{ShellCompNoDescRequestCmd}
args := []string{zulu.ShellCompNoDescRequestCmd}
args = append(args, tc.args...)
output, err := executeCommand(c, args...)
switch {
Expand Down
7 changes: 4 additions & 3 deletions flag_groups.go
Expand Up @@ -178,7 +178,7 @@ func (c *Command) enforceFlagGroupsForCompletion() {
flags := c.Flags()
groupStatus := map[string]map[string]bool{}
mutuallyExclusiveGroupStatus := map[string]map[string]bool{}
c.Flags().VisitAll(func(pflag *flag.Flag) {
c.Flags().VisitAll(func(pflag *zflag.Flag) {
processFlagForGroupAnnotation(flags, pflag, requiredAsGroup, groupStatus)
processFlagForGroupAnnotation(flags, pflag, mutuallyExclusive, mutuallyExclusiveGroupStatus)
})
Expand All @@ -190,7 +190,8 @@ func (c *Command) enforceFlagGroupsForCompletion() {
if isSet {
// One of the flags of the group is set, mark the other ones as required
for _, fName := range strings.Split(flagList, " ") {
_ = c.MarkFlagRequired(fName)
flag := c.Flags().Lookup(fName)
_ = FlagOptRequired()(flag)
}
}
}
Expand All @@ -207,7 +208,7 @@ func (c *Command) enforceFlagGroupsForCompletion() {
for _, fName := range strings.Split(flagList, " ") {
if fName != flagName {
flag := c.Flags().Lookup(fName)
flag.Hidden = true
_ = zflag.OptHidden()(flag)
}
}
}
Expand Down

0 comments on commit 7f3b34a

Please sign in to comment.