From b370e7b9af77863720df919db204f20097186aaf Mon Sep 17 00:00:00 2001 From: Jun Nishimura Date: Fri, 14 Jul 2023 23:50:21 +0900 Subject: [PATCH] fix help command: use PersistentPreRunE instead of PersistentPreRun --- command.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/command.go b/command.go index 43916321..acb3752d 100644 --- a/command.go +++ b/command.go @@ -1209,7 +1209,7 @@ Simply type ` + c.Name() + ` help [path to command] for full details.`, } return completions, ShellCompDirectiveNoFileComp }, - PersistentPreRun: func(cmd *Command, args []string) { + PersistentPreRunE: func(cmd *Command, args []string) error { cmd.Flags().VisitAll(func(pflag *flag.Flag) { requiredAnnotation, found := pflag.Annotations[BashCompOneRequiredFlag] if found && requiredAnnotation[0] == "true" { @@ -1219,7 +1219,12 @@ Simply type ` + c.Name() + ` help [path to command] for full details.`, }) // 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 }, Run: func(c *Command, args []string) { cmd, _, e := c.Root().Find(args)