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

View file

@ -681,6 +681,15 @@ See each sub-command's help for details on how to use the generated script.
ValidArgsFunction: NoFileCompletions, ValidArgsFunction: NoFileCompletions,
Hidden: c.CompletionOptions.HiddenDefaultCmd, Hidden: c.CompletionOptions.HiddenDefaultCmd,
GroupID: c.completionCommandGroupID, 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) c.AddCommand(completionCmd)
@ -713,7 +722,6 @@ You will need to start a new shell for this setup to take effect.
`, c.Root().Name()), `, c.Root().Name()),
Args: NoArgs, Args: NoArgs,
DisableFlagsInUseLine: true, DisableFlagsInUseLine: true,
DisableFlagParsing: true,
ValidArgsFunction: NoFileCompletions, ValidArgsFunction: NoFileCompletions,
RunE: func(cmd *Command, args []string) error { RunE: func(cmd *Command, args []string) error {
return cmd.Root().GenBashCompletionV2(out, !noDesc) 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. You will need to start a new shell for this setup to take effect.
`, c.Root().Name()), `, c.Root().Name()),
Args: NoArgs, Args: NoArgs,
DisableFlagParsing: true,
ValidArgsFunction: NoFileCompletions, ValidArgsFunction: NoFileCompletions,
RunE: func(cmd *Command, args []string) error { RunE: func(cmd *Command, args []string) error {
if noDesc { 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. You will need to start a new shell for this setup to take effect.
`, c.Root().Name()), `, c.Root().Name()),
Args: NoArgs, Args: NoArgs,
DisableFlagParsing: true,
ValidArgsFunction: NoFileCompletions, ValidArgsFunction: NoFileCompletions,
RunE: func(cmd *Command, args []string) error { RunE: func(cmd *Command, args []string) error {
return cmd.Root().GenFishCompletion(out, !noDesc) 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. to your powershell profile.
`, c.Root().Name()), `, c.Root().Name()),
Args: NoArgs, Args: NoArgs,
DisableFlagParsing: true,
ValidArgsFunction: NoFileCompletions, ValidArgsFunction: NoFileCompletions,
RunE: func(cmd *Command, args []string) error { RunE: func(cmd *Command, args []string) error {
if noDesc { if noDesc {