mirror of
https://github.com/spf13/cobra
synced 2025-05-06 05:17:21 +00:00
Fixed Help not showing on non runnable command
This commit is contained in:
parent
fe5e611709
commit
deacc2c027
2 changed files with 7 additions and 6 deletions
|
@ -431,7 +431,7 @@ func (c *Command) HelpTemplate() string {
|
|||
}
|
||||
return `{{with (or .Long .Short)}}{{. | trimTrailingWhitespaces}}
|
||||
|
||||
{{end}}{{if or .Runnable .HasSubCommands}}{{.UsageString}}{{end}}`
|
||||
{{end}}{{if or .Runnable .HasSubCommands .HasFlags}}{{.UsageString}}{{end}}`
|
||||
}
|
||||
|
||||
// VersionTemplate return version template for the command.
|
||||
|
@ -721,6 +721,10 @@ func (c *Command) execute(a []string) (err error) {
|
|||
}
|
||||
}
|
||||
|
||||
if err := c.validateRequiredFlags(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !c.Runnable() {
|
||||
return flag.ErrHelp
|
||||
}
|
||||
|
@ -755,9 +759,6 @@ func (c *Command) execute(a []string) (err error) {
|
|||
c.PreRun(c, argWoFlags)
|
||||
}
|
||||
|
||||
if err := c.validateRequiredFlags(); err != nil {
|
||||
return err
|
||||
}
|
||||
if c.RunE != nil {
|
||||
if err := c.RunE(c, argWoFlags); err != nil {
|
||||
return err
|
||||
|
|
|
@ -835,12 +835,12 @@ func TestHelpExecutedOnNonRunnableChild(t *testing.T) {
|
|||
childCmd := &Command{Use: "child", Long: "Long description"}
|
||||
rootCmd.AddCommand(childCmd)
|
||||
|
||||
output, err := executeCommand(rootCmd, "child")
|
||||
output, err := executeCommand(rootCmd, "child", "--help")
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: %v", err)
|
||||
}
|
||||
|
||||
checkStringContains(t, output, childCmd.Long)
|
||||
checkStringContains(t, output, "Usage")
|
||||
}
|
||||
|
||||
func TestVersionFlagExecuted(t *testing.T) {
|
||||
|
|
Loading…
Add table
Reference in a new issue