Fix missing recursion

Signed-off-by: Marc Khouzam <marc.khouzam@gmail.com>
This commit is contained in:
Marc Khouzam 2025-01-26 06:33:50 -05:00
parent 0f94115d1c
commit dbc168cce1
2 changed files with 47 additions and 12 deletions

View file

@ -18,6 +18,7 @@ import (
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"runtime"
"strings" "strings"
"testing" "testing"
"text/template" "text/template"

View file

@ -437,10 +437,7 @@ func (c *Command) UsageFunc() (f func(*Command) error) {
} }
return func(c *Command) error { return func(c *Command) error {
c.mergePersistentFlags() c.mergePersistentFlags()
fn := defaultUsageFunc fn := c.getUsageTemplateFunc()
if c.usageTemplate != nil {
fn = c.usageTemplate.fn
}
err := fn(c.OutOrStderr(), c) err := fn(c.OutOrStderr(), c)
if err != nil { if err != nil {
c.PrintErrln(err) c.PrintErrln(err)
@ -449,6 +446,19 @@ func (c *Command) UsageFunc() (f func(*Command) error) {
} }
} }
// getUsageTemplateFunc returns the usage template function for the command
// going up the command tree if necessary.
func (c *Command) getUsageTemplateFunc() func(w io.Writer, data interface{}) error {
if c.usageTemplate != nil {
return c.usageTemplate.fn
}
if c.HasParent() {
return c.parent.getUsageTemplateFunc()
}
return defaultUsageFunc
}
// Usage puts out the usage for the command. // Usage puts out the usage for the command.
// Used when a user provides invalid input. // Used when a user provides invalid input.
// Can be defined by user by overriding UsageFunc. // Can be defined by user by overriding UsageFunc.
@ -467,10 +477,7 @@ func (c *Command) HelpFunc() func(*Command, []string) {
} }
return func(c *Command, a []string) { return func(c *Command, a []string) {
c.mergePersistentFlags() c.mergePersistentFlags()
fn := defaultHelpFunc fn := c.getHelpTemplateFunc()
if c.helpTemplate != nil {
fn = c.helpTemplate.fn
}
// The help should be sent to stdout // The help should be sent to stdout
// See https://github.com/spf13/cobra/issues/1002 // See https://github.com/spf13/cobra/issues/1002
err := fn(c.OutOrStdout(), c) err := fn(c.OutOrStdout(), c)
@ -480,6 +487,20 @@ func (c *Command) HelpFunc() func(*Command, []string) {
} }
} }
// getHelpTemplateFunc returns the help template function for the command
// going up the command tree if necessary.
func (c *Command) getHelpTemplateFunc() func(w io.Writer, data interface{}) error {
if c.helpTemplate != nil {
return c.helpTemplate.fn
}
if c.HasParent() {
return c.parent.getHelpTemplateFunc()
}
return defaultHelpFunc
}
// 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.
@ -554,6 +575,7 @@ func (c *Command) NamePadding() int {
} }
// UsageTemplate returns usage template for the command. // UsageTemplate returns usage template for the command.
// This function is kept for backwards-compatibility reasons.
func (c *Command) UsageTemplate() string { func (c *Command) UsageTemplate() string {
if c.usageTemplate != nil { if c.usageTemplate != nil {
return c.usageTemplate.tmpl return c.usageTemplate.tmpl
@ -566,6 +588,7 @@ func (c *Command) UsageTemplate() string {
} }
// HelpTemplate return help template for the command. // HelpTemplate return help template for the command.
// This function is kept for backwards-compatibility reasons.
func (c *Command) HelpTemplate() string { func (c *Command) HelpTemplate() string {
if c.helpTemplate != nil { if c.helpTemplate != nil {
return c.helpTemplate.tmpl return c.helpTemplate.tmpl
@ -578,6 +601,7 @@ func (c *Command) HelpTemplate() string {
} }
// VersionTemplate return version template for the command. // VersionTemplate return version template for the command.
// This function is kept for backwards-compatibility reasons.
func (c *Command) VersionTemplate() string { func (c *Command) VersionTemplate() string {
if c.versionTemplate != nil { if c.versionTemplate != nil {
return c.versionTemplate.tmpl return c.versionTemplate.tmpl
@ -589,6 +613,19 @@ func (c *Command) VersionTemplate() string {
return defaultVersionTemplate return defaultVersionTemplate
} }
// getVersionTemplateFunc returns the version template function for the command
// going up the command tree if necessary.
func (c *Command) getVersionTemplateFunc() func(w io.Writer, data interface{}) error {
if c.versionTemplate != nil {
return c.versionTemplate.fn
}
if c.HasParent() {
return c.parent.getVersionTemplateFunc()
}
return defaultVersionFunc
}
// ErrPrefix return error message prefix for the command // ErrPrefix return error message prefix for the command
func (c *Command) ErrPrefix() string { func (c *Command) ErrPrefix() string {
if c.errPrefix != "" { if c.errPrefix != "" {
@ -893,10 +930,7 @@ func (c *Command) execute(a []string) (err error) {
return err return err
} }
if versionVal { if versionVal {
fn := defaultVersionFunc fn := c.getVersionTemplateFunc()
if c.versionTemplate != nil {
fn = c.versionTemplate.fn
}
err := fn(c.OutOrStdout(), c) err := fn(c.OutOrStdout(), c)
if err != nil { if err != nil {
c.Println(err) c.Println(err)