From 011f19e8ec4faa23787b731efa5bd70406c1fc16 Mon Sep 17 00:00:00 2001 From: bogem Date: Fri, 14 Oct 2016 13:41:00 +0200 Subject: [PATCH] Fix condition in HelpFunc I think, it's more obvious, what does this method --- command.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/command.go b/command.go index 8f6580ab..49889318 100644 --- a/command.go +++ b/command.go @@ -231,12 +231,8 @@ func (c *Command) Usage() error { // HelpFunc returns either the function set by SetHelpFunc for this command // or a parent, or it returns a function with default help behavior. func (c *Command) HelpFunc() func(*Command, []string) { - cmd := c - for cmd != nil { - if cmd.helpFunc != nil { - return cmd.helpFunc - } - cmd = cmd.parent + if helpFunc := c.checkHelpFunc(); helpFunc != nil { + return helpFunc } return func(*Command, []string) { c.mergePersistentFlags() @@ -247,6 +243,20 @@ func (c *Command) HelpFunc() func(*Command, []string) { } } +// checkHelpFunc checks if there is helpFunc in ancestors of c. +func (c *Command) checkHelpFunc() func(*Command, []string) { + if c == nil { + return nil + } + if c.helpFunc != nil { + return c.helpFunc + } + if c.HasParent() { + return c.parent.checkHelpFunc() + } + return nil +} + // Help puts out the help for the command. // Used when a user calls help [command]. // Can be defined by user by overriding HelpFunc.