diff --git a/app_test.go b/app_test.go index 3bede53..ae8bb0f 100644 --- a/app_test.go +++ b/app_test.go @@ -1,10 +1,12 @@ package cli_test import ( + "bytes" "flag" "fmt" "io" "os" + "strings" "testing" "github.com/codegangsta/cli" @@ -621,3 +623,57 @@ func TestGlobalFlagsInSubcommands(t *testing.T) { expect(t, subcommandRun, true) } + +func TestApp_Run_CommandWithSubcommandHasHelpTopic(t *testing.T) { + var subcommandHelpTopics = [][]string{ + {"command", "foo", "--help"}, + {"command", "foo", "-h"}, + {"command", "foo", "help"}, + } + + for _, flagSet := range subcommandHelpTopics { + t.Logf("==> checking with flags %v", flagSet) + + app := cli.NewApp() + buf := new(bytes.Buffer) + app.Writer = buf + + subCmdBar := cli.Command{ + Name: "bar", + Usage: "does bar things", + } + subCmdBaz := cli.Command{ + Name: "baz", + Usage: "does baz things", + } + cmd := cli.Command{ + Name: "foo", + Description: "descriptive wall of text about how it does foo things", + Subcommands: []cli.Command{subCmdBar, subCmdBaz}, + } + + app.Commands = []cli.Command{cmd} + err := app.Run(flagSet) + + if err != nil { + t.Error(err) + } + + output := buf.String() + t.Logf("output: %q\n", buf.Bytes()) + + if strings.Contains(output, "No help topic for") { + t.Errorf("expect a help topic, got none: \n%q", output) + } + + for _, shouldContain := range []string{ + cmd.Name, cmd.Description, + subCmdBar.Name, subCmdBar.Usage, + subCmdBaz.Name, subCmdBaz.Usage, + } { + if !strings.Contains(output, shouldContain) { + t.Errorf("want help to contain %q, did not: \n%q", shouldContain, output) + } + } + } +} diff --git a/command.go b/command.go index b61691c..132a1e3 100644 --- a/command.go +++ b/command.go @@ -157,6 +157,7 @@ func (c Command) startApp(ctx *Context) error { app.Commands = c.Subcommands app.Flags = c.Flags app.HideHelp = c.HideHelp + app.Writer = ctx.App.Writer // bash completion app.EnableBashCompletion = ctx.App.EnableBashCompletion