From 7f3b34a38f667b82bc4268daf1ccf60646079224 Mon Sep 17 00:00:00 2001 From: Hosh Sadiq Date: Wed, 24 Aug 2022 19:56:24 +0100 Subject: [PATCH] Adjust flag groups completion for zulu --- completions.go | 4 ++++ completions_test.go | 36 ++++++++++++++++++------------------ flag_groups.go | 7 ++++--- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/completions.go b/completions.go index e774b54..1ebf1c0 100644 --- a/completions.go +++ b/completions.go @@ -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: diff --git a/completions_test.go b/completions_test.go index 0bfae69..1f39b13 100644 --- a/completions_test.go +++ b/completions_test.go @@ -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) @@ -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 { @@ -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) @@ -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 { diff --git a/flag_groups.go b/flag_groups.go index 5a4f582..8713d6d 100644 --- a/flag_groups.go +++ b/flag_groups.go @@ -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) }) @@ -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) } } } @@ -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) } } }