diff --git a/command.go b/command.go index 55ddc50fa..ad30996d1 100644 --- a/command.go +++ b/command.go @@ -801,7 +801,11 @@ func (c *Command) execute(a []string) (err error) { } if !c.Runnable() { - return ErrSubCommandRequired + if c.IsAdditionalHelpTopicCommand() { + return flag.ErrHelp + } else { + return ErrSubCommandRequired + } } c.preRun() diff --git a/command_test.go b/command_test.go index 121559374..c84117e9f 100644 --- a/command_test.go +++ b/command_test.go @@ -897,11 +897,36 @@ func TestFlagsInUsage(t *testing.T) { checkStringContains(t, output, "[flags]") } -func TestHelpExecutedOnNonRunnableChild(t *testing.T) { +func TestHelpExecutedOnAdditionalHelpTopicCommand(t *testing.T) { rootCmd := &Command{Use: "root", Run: emptyRun} childCmd := &Command{Use: "child", Long: "Long description"} + rootCmd.AddCommand(childCmd) + if !childCmd.IsAdditionalHelpTopicCommand() { + t.Errorf("child should be an additional help topic command") + } + + output, err := executeCommand(rootCmd, "child") + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + + checkStringContains(t, output, childCmd.Long) +} + +func TestHelpExecutedOnNonRunnableChildWithGrandchild(t *testing.T) { + rootCmd := &Command{Use: "root", Run: emptyRun} + childCmd := &Command{Use: "child", Long: "Long description"} + granchildCmd := &Command{Use: "grandchild", Run: emptyRun} + + childCmd.AddCommand(granchildCmd) + rootCmd.AddCommand(childCmd) + + if childCmd.IsAdditionalHelpTopicCommand() { + t.Errorf("child should not be an additional help topic command") + } + output, err := executeCommand(rootCmd, "child") if err != ErrSubCommandRequired { t.Errorf("Expected error")