Fixed Help not showing on non runnable command

This commit is contained in:
Vinicyus Macedo 2018-11-19 12:52:41 -02:00
parent fe5e611709
commit deacc2c027
2 changed files with 7 additions and 6 deletions

View file

@ -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

View file

@ -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) {