From 29c05659ce1d29d0d08a85eee4061072014f4cc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20Gul=C3=A1csi?= Date: Fri, 9 Oct 2026 09:27:01 +0200 Subject: [PATCH] Export error printing HandleErrHelp This enables replicating ParseAndRun with the decomposed Parse + HandleErrHelp + Run combination. (Help printing is unavailable otherwise). --- cli.go | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/cli.go b/cli.go index 22ce30a..9dc6285 100644 --- a/cli.go +++ b/cli.go @@ -279,21 +279,38 @@ func Run(ctx context.Context, root *Command, options *RunOptions) error { // } func ParseAndRun(ctx context.Context, root *Command, args []string, options *RunOptions) error { if err := Parse(root, args); err != nil { - if errors.Is(err, flag.ErrHelp) { - options = checkAndSetRunOptions(options) - _, _ = fmt.Fprintln(options.Stdout, help(root)) - return nil - } - if usageErr, ok := errors.AsType[*usageError](err); ok { - options = checkAndSetRunOptions(options) - _, _ = fmt.Fprintf(options.Stderr, "%s\n\n", help(root)) - return usageErr.Unwrap() - } - return err + return HandleErrHelp(err, root, options) } return Run(ctx, root, options) } +// HandleErrHelp handles ErrHelp/usageError by printing the help. +// +// If the has been handled like that, then it returns nil. +// For other errors, err is returned. +func HandleErrHelp(err error, root *Command, options *RunOptions) error { + if errors.Is(err, flag.ErrHelp) { + options = checkAndSetRunOptions(options) + PrintHelp(options.Stdout, root) + return nil + } + if usageErr, ok := errors.AsType[*usageError](err); ok { + options = checkAndSetRunOptions(options) + PrintHelp(options.Stderr, root) + _, _ = options.Stderr.Write([]byte{'\n'}) + return usageErr.Unwrap() + } + return err +} + +// PrintHelp prints the help message to the given writer (os.Stderr if nil). +func PrintHelp(w io.Writer, root *Command) { + if w == nil { + w = os.Stderr + } + _, _ = fmt.Fprintln(w, help(root)) +} + // UsageErrorf returns an error for invalid command arguments or flag combinations. [Run] prints the // command's help before returning the underlying error. //