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
Provenance
- Commit
b825817- Tree
54fb3a5- Author
Steve Francia <steve.francia@gmail.com>- Committer
Steve Francia <steve.francia@gmail.com>- Parent 1
b1e90a7- Parent 2
9b6c926
2 files changed+53 added−2 removed
modified:cobra_test.go+46 added
134 unmodified lines135 136 return fullTester(c, input)137 }138 139+func rootOnlySetupTest(input string) resulter {140+ c := initializeWithRootCmd()141+142+ return simpleTester(c, input)143+}144+145+func 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, " "))150+151+ err := c.Execute()152+ output := buf.String()153+154+ return resulter{err, output, c}155+}156+157 func fullTester(c *Command, input string) resulter {158 buf := new(bytes.Buffer)159 // Testing flag with invalid input160 c.SetOutput(buf)12 unmodified lines173 t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", check, x.Output)174 }175 }176 177+func 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+}182+183 func checkOutputContains(t *testing.T, c *Command, check string) {184 buf := new(bytes.Buffer)185 c.SetOutput(buf)186 c.Execute()273 unmodified lines460 func TestRootHelp(t *testing.T) {461 x := fullSetupTest("--help")462 463 checkResultContains(t, x, "Available Commands:")464+ checkResultContains(t, x, "for more information about that command")465 466 if strings.Contains(x.Output, "unknown flag: --help") {467 t.Errorf("--help shouldn't trigger an error, Got: \n %s", x.Output)468 }469 470 x = fullSetupTest("echo --help")471 472 checkResultContains(t, x, "Available Commands:")473+ checkResultContains(t, x, "for more information about that command")474 475 if strings.Contains(x.Output, "unknown flag: --help") {476 t.Errorf("--help shouldn't trigger an error, Got: \n %s", x.Output)477 }478 479 }480 481+func TestRootNoCommandHelp(t *testing.T) {482+ x := rootOnlySetupTest("--help")483+484+ checkResultOmits(t, x, "Available Commands:")485+ checkResultOmits(t, x, "for more information about that command")486+487+ if strings.Contains(x.Output, "unknown flag: --help") {488+ t.Errorf("--help shouldn't trigger an error, Got: \n %s", x.Output)489+ }490+491+ x = rootOnlySetupTest("echo --help")492+493+ checkResultOmits(t, x, "Available Commands:")494+ checkResultOmits(t, x, "for more information about that command")495+496+ if strings.Contains(x.Output, "unknown flag: --help") {497+ t.Errorf("--help shouldn't trigger an error, Got: \n %s", x.Output)498+ }499+}500+501 func TestFlagsBeforeCommand(t *testing.T) {502 // short without space503 x := fullSetupTest("-i10 echo")504 if x.Error != nil {49 unmodified linesmodified:command.go+7 added-2 removed
201 unmodified lines202 {{ if .HasFlags}} Available Flags:203 {{.Flags.FlagUsages}}{{end}}{{if .HasParent}}{{if and (gt .Commands 0) (gt .Parent.Commands 1) }}204 Additional 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 }}207 Use "{{.Root.Name}} help [command]" for more information about that command.208-`208+{{end}}`209 }210 }211 212 func (c *Command) HelpTemplate() string {251 unmodified lines464 }465 466 func (c *Command) initHelp() {467 if c.helpCommand == nil {468+ if !c.HasSubCommands() {469+ return470+ }471+472 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 lines482 483 // Used for testing484 func (c *Command) ResetCommands() {485 c.commands = nil486+ c.helpCommand = nil487 }488 489 func (c *Command) Commands() []*Command {490 return c.commands279 unmodified lines