From 46a2ba8c4179e52e76c08a3fab78af888bdf38a6 Mon Sep 17 00:00:00 2001 From: Jun Nishimura Date: Fri, 14 Jul 2023 23:50:49 +0900 Subject: [PATCH] fix completion command: use PersistentPreRunE instead of PersistentPreRun --- completions.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/completions.go b/completions.go index 23d09bc..58bc770 100644 --- a/completions.go +++ b/completions.go @@ -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" { @@ -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)