Merge pull request #353 from bogem/helpfunc

Fix condition in HelpFunc
This commit is contained in:
Eric Paris 2016-10-14 18:20:36 -04:00 committed by GitHub
commit 856b96dcb4

View file

@ -231,12 +231,8 @@ func (c *Command) Usage() error {
// HelpFunc returns either the function set by SetHelpFunc for this command // HelpFunc returns either the function set by SetHelpFunc for this command
// or a parent, or it returns a function with default help behavior. // or a parent, or it returns a function with default help behavior.
func (c *Command) HelpFunc() func(*Command, []string) { func (c *Command) HelpFunc() func(*Command, []string) {
cmd := c if helpFunc := c.checkHelpFunc(); helpFunc != nil {
for cmd != nil { return helpFunc
if cmd.helpFunc != nil {
return cmd.helpFunc
}
cmd = cmd.parent
} }
return func(*Command, []string) { return func(*Command, []string) {
c.mergePersistentFlags() 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. // Help puts out the help for the command.
// Used when a user calls help [command]. // Used when a user calls help [command].
// Can be defined by user by overriding HelpFunc. // Can be defined by user by overriding HelpFunc.