Skip to content

Commit

Permalink
fix completion command: use PersistentPreRunE instead of PersistentPr…
Browse files Browse the repository at this point in the history
…eRun
  • Loading branch information
JunNishimura committed Jul 14, 2023
1 parent b370e7b commit 46a2ba8
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ 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) {
PersistentPreRunE: func(cmd *Command, args []string) error {
cmd.Flags().VisitAll(func(flag *pflag.Flag) {
requiredAnnotation, found := flag.Annotations[BashCompOneRequiredFlag]
if found && requiredAnnotation[0] == "true" {
Expand All @@ -691,7 +691,12 @@ See each sub-command's help for details on how to use the generated script.
})
// Adding PersistentPreRun on sub-commands prevents root's PersistentPreRun from being called.
// So it is intentionally called here.
cmd.Root().PersistentPreRun(cmd.Root(), args)
if cmd.Root().PersistentPreRunE != nil {
return cmd.Root().PersistentPreRunE(cmd, args)
} else if cmd.Root().PersistentPreRun != nil {
cmd.Root().PersistentPreRun(cmd, args)
}
return nil
},
}
c.AddCommand(completionCmd)
Expand Down

0 comments on commit 46a2ba8

Please sign in to comment.