Skip to content

Commit

Permalink
add PersistentPreRun for completion command
Browse files Browse the repository at this point in the history
disable any persistent required flags for the completion command
  • Loading branch information
JunNishimura committed Jul 10, 2023
1 parent 4b81cd5 commit 94a3cb1
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
},
Expand All @@ -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)
Expand Down

0 comments on commit 94a3cb1

Please sign in to comment.