Skip to content

Commit

Permalink
Merge pull request mitchellh#63 from mitchellh/b-root-help
Browse files Browse the repository at this point in the history
Root help should only print root subcommands
  • Loading branch information
mitchellh authored Sep 5, 2017
2 parents 1764750 + 99851ff commit dff723f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (c *CLI) Run() (int, error) {

// Just print the help when only '-h' or '--help' is passed.
if c.IsHelp() && c.Subcommand() == "" {
c.HelpWriter.Write([]byte(c.HelpFunc(c.Commands) + "\n"))
c.HelpWriter.Write([]byte(c.HelpFunc(c.helpCommands(c.Subcommand())) + "\n"))
return 0, nil
}

Expand Down
50 changes: 50 additions & 0 deletions cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,56 @@ func TestCLIRun_printCommandHelpSubcommandsNestedTwoLevel(t *testing.T) {
}
}

// Test that the root help only prints the root level.
func TestCLIRun_printHelpRootSubcommands(t *testing.T) {
testCases := [][]string{
{"--help"},
{"-h"},
}

for _, args := range testCases {
buf := new(bytes.Buffer)
cli := &CLI{
Args: args,
Commands: map[string]CommandFactory{
"bar": func() (Command, error) {
return &MockCommand{SynopsisText: "hi!"}, nil
},
"foo": func() (Command, error) {
return &MockCommand{SynopsisText: "hi!"}, nil
},
"foo bar": func() (Command, error) {
return &MockCommand{SynopsisText: "hi!"}, nil
},
"foo zip": func() (Command, error) {
return &MockCommand{SynopsisText: "hi!"}, nil
},
},
HelpWriter: buf,
}

exitCode, err := cli.Run()
if err != nil {
t.Fatalf("err: %s", err)
}

if exitCode != 0 {
t.Fatalf("bad exit code: %d", exitCode)
}

expected := `Usage: app [--version] [--help] <command> [<args>]
Available commands are:
bar hi!
foo hi!
`
if buf.String() != expected {
t.Fatalf("bad: %#v\n\n'%#v'\n\n'%#v'", args, buf.String(), expected)
}
}
}

func TestCLIRun_printCommandHelpTemplate(t *testing.T) {
testCases := [][]string{
{"--help", "foo"},
Expand Down

0 comments on commit dff723f

Please sign in to comment.