From 94a3cb14391954971fd697497a7e8d1515bffda3 Mon Sep 17 00:00:00 2001 From: Jun Nishimura Date: Tue, 11 Jul 2023 00:05:27 +0900 Subject: [PATCH] add PersistentPreRun for completion command disable any persistent required flags for the completion command --- completions.go | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/completions.go b/completions.go index f8b5167..92e17f6 100644 --- a/completions.go +++ b/completions.go @@ -681,6 +681,15 @@ See each sub-command's help for details on how to use the generated script. ValidArgsFunction: NoFileCompletions, Hidden: c.CompletionOptions.HiddenDefaultCmd, GroupID: c.completionCommandGroupID, + PersistentPreRun: func(cmd *Command, args []string) { + cmd.Flags().VisitAll(func(flag *pflag.Flag) { + requiredAnnotation, found := flag.Annotations[BashCompOneRequiredFlag] + if found && requiredAnnotation[0] == "true" { + // Disable any persistent required flags for the completion command + flag.Annotations[BashCompOneRequiredFlag] = []string{"false"} + } + }) + }, } c.AddCommand(completionCmd) @@ -713,7 +722,6 @@ You will need to start a new shell for this setup to take effect. `, c.Root().Name()), Args: NoArgs, DisableFlagsInUseLine: true, - DisableFlagParsing: true, ValidArgsFunction: NoFileCompletions, RunE: func(cmd *Command, args []string) error { return cmd.Root().GenBashCompletionV2(out, !noDesc) @@ -749,9 +757,8 @@ To load completions for every new session, execute once: You will need to start a new shell for this setup to take effect. `, c.Root().Name()), - Args: NoArgs, - DisableFlagParsing: true, - ValidArgsFunction: NoFileCompletions, + Args: NoArgs, + ValidArgsFunction: NoFileCompletions, RunE: func(cmd *Command, args []string) error { if noDesc { return cmd.Root().GenZshCompletionNoDesc(out) @@ -778,9 +785,8 @@ To load completions for every new session, execute once: You will need to start a new shell for this setup to take effect. `, c.Root().Name()), - Args: NoArgs, - DisableFlagParsing: true, - ValidArgsFunction: NoFileCompletions, + Args: NoArgs, + ValidArgsFunction: NoFileCompletions, RunE: func(cmd *Command, args []string) error { return cmd.Root().GenFishCompletion(out, !noDesc) }, @@ -801,9 +807,8 @@ To load completions in your current shell session: To load completions for every new session, add the output of the above command to your powershell profile. `, c.Root().Name()), - Args: NoArgs, - DisableFlagParsing: true, - ValidArgsFunction: NoFileCompletions, + Args: NoArgs, + ValidArgsFunction: NoFileCompletions, RunE: func(cmd *Command, args []string) error { if noDesc { return cmd.Root().GenPowerShellCompletion(out)