add PersistentPreRun for completion command

disable any persistent required flags for the completion command
This commit is contained in:
Jun Nishimura 2023-07-11 00:05:27 +09:00
parent 4b81cd58b2
commit 94a3cb1439
1 changed files with 15 additions and 10 deletions

View File

@ -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)
@ -750,7 +758,6 @@ 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,
RunE: func(cmd *Command, args []string) error {
if noDesc {
@ -779,7 +786,6 @@ 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,
RunE: func(cmd *Command, args []string) error {
return cmd.Root().GenFishCompletion(out, !noDesc)
@ -802,7 +808,6 @@ 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,
RunE: func(cmd *Command, args []string) error {
if noDesc {