Skip to content
Myr

Merge pull request #32 from smarterclayton/hide_subcommand_help_for_root_only_command

Before adoption: this commit came in with the history the project adopted, so no landing records it.

When no subcommands are registered, omit command help output

authored by Steve Francia · committed by Steve Francia · parents b1e90a7, 9b6c926 · Browse files · 2 files+53 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+53 added−2 removed

  • other:+53 added−2 removed

2 files+53 added−2 removed

modified:cobra_test.go+46 added
134 unmodified lines
135136 return fullTester(c, input)137}138139func rootOnlySetupTest(input string) resulter {140 c := initializeWithRootCmd()141142 return simpleTester(c, input)143}144145func simpleTester(c *Command, input string) resulter {146 buf := new(bytes.Buffer)147 // Testing flag with invalid input148 c.SetOutput(buf)149 c.SetArgs(strings.Split(input, " "))150151 err := c.Execute()152 output := buf.String()153154 return resulter{err, output, c}155}156157func fullTester(c *Command, input string) resulter {158 buf := new(bytes.Buffer)159 // Testing flag with invalid input160 c.SetOutput(buf)
12 unmodified lines
173 t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", check, x.Output)174 }175}176177func checkResultOmits(t *testing.T, x resulter, check string) {178 if strings.Contains(x.Output, check) {179 t.Errorf("Unexpected response.\nExpecting to omit: \n %q\nGot:\n %q\n", check, x.Output)180 }181}182183func checkOutputContains(t *testing.T, c *Command, check string) {184 buf := new(bytes.Buffer)185 c.SetOutput(buf)186 c.Execute()
273 unmodified lines
460func TestRootHelp(t *testing.T) {461 x := fullSetupTest("--help")462463 checkResultContains(t, x, "Available Commands:")464 checkResultContains(t, x, "for more information about that command")465466 if strings.Contains(x.Output, "unknown flag: --help") {467 t.Errorf("--help shouldn't trigger an error, Got: \n %s", x.Output)468 }469470 x = fullSetupTest("echo --help")471472 checkResultContains(t, x, "Available Commands:")473 checkResultContains(t, x, "for more information about that command")474475 if strings.Contains(x.Output, "unknown flag: --help") {476 t.Errorf("--help shouldn't trigger an error, Got: \n %s", x.Output)477 }478479}480481func TestRootNoCommandHelp(t *testing.T) {482 x := rootOnlySetupTest("--help")483484 checkResultOmits(t, x, "Available Commands:")485 checkResultOmits(t, x, "for more information about that command")486487 if strings.Contains(x.Output, "unknown flag: --help") {488 t.Errorf("--help shouldn't trigger an error, Got: \n %s", x.Output)489 }490491 x = rootOnlySetupTest("echo --help")492493 checkResultOmits(t, x, "Available Commands:")494 checkResultOmits(t, x, "for more information about that command")495496 if strings.Contains(x.Output, "unknown flag: --help") {497 t.Errorf("--help shouldn't trigger an error, Got: \n %s", x.Output)498 }499}500501func TestFlagsBeforeCommand(t *testing.T) {502 // short without space503 x := fullSetupTest("-i10 echo")504 if x.Error != nil {
49 unmodified lines
modified:command.go+7 added-2 removed
201 unmodified lines
202{{ if .HasFlags}} Available Flags:203{{.Flags.FlagUsages}}{{end}}{{if .HasParent}}{{if and (gt .Commands 0) (gt .Parent.Commands 1) }}204Additional help topics: {{if gt .Commands 0 }}{{range .Commands}}{{if not .Runnable}} {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if gt .Parent.Commands 1 }}{{range .Parent.Commands}}{{if .Runnable}}{{if not (eq .Name $cmd.Name) }}{{end}}205 {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{end}}206{{end}}206{{end}}{{ if .HasSubCommands }}207Use "{{.Root.Name}} help [command]" for more information about that command.208`208{{end}}`209 }210}211212func (c *Command) HelpTemplate() string {
251 unmodified lines
464}465466func (c *Command) initHelp() {467 if c.helpCommand == nil {468 if !c.HasSubCommands() {469 return470 }471472 c.helpCommand = &Command{473 Use: "help [command]",474 Short: "Help about any command",475 Long: `Help provides help for any command in the application.
6 unmodified lines
482483// Used for testing484func (c *Command) ResetCommands() {485 c.commands = nil486 c.helpCommand = nil487}488489func (c *Command) Commands() []*Command {490 return c.commands
279 unmodified lines