This commit is contained in:
Olivier Bellone 2020-03-18 11:17:54 -07:00
parent 95f2f73ed9
commit 578111308e
No known key found for this signature in database
GPG key ID: 11E77E3AA0C40303
2 changed files with 31 additions and 2 deletions

View file

@ -801,7 +801,11 @@ func (c *Command) execute(a []string) (err error) {
} }
if !c.Runnable() { if !c.Runnable() {
return ErrSubCommandRequired if c.IsAdditionalHelpTopicCommand() {
return flag.ErrHelp
} else {
return ErrSubCommandRequired
}
} }
c.preRun() c.preRun()

View file

@ -897,11 +897,36 @@ func TestFlagsInUsage(t *testing.T) {
checkStringContains(t, output, "[flags]") checkStringContains(t, output, "[flags]")
} }
func TestHelpExecutedOnNonRunnableChild(t *testing.T) { func TestHelpExecutedOnAdditionalHelpTopicCommand(t *testing.T) {
rootCmd := &Command{Use: "root", Run: emptyRun} rootCmd := &Command{Use: "root", Run: emptyRun}
childCmd := &Command{Use: "child", Long: "Long description"} childCmd := &Command{Use: "child", Long: "Long description"}
rootCmd.AddCommand(childCmd) 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") output, err := executeCommand(rootCmd, "child")
if err != ErrSubCommandRequired { if err != ErrSubCommandRequired {
t.Errorf("Expected error") t.Errorf("Expected error")