Skip to content
Myr

Cobra behavior is now more consistent. Invalid flags cause Usage to be printed.

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

authored by spf13 · committed by spf13 · parent 7ab4fd7 · Browse files · 2 files+25 added−12 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+25 added−12 removed

  • other:+25 added−12 removed

2 files+25 added−12 removed

modified:cobra_test.go+22 added-12 removed
117 unmodified lines
118 commandInit()119 return cmdRootWithRun120}121122func checkOutputContains(t *testing.T, c *Command, check string) {123 buf := new(bytes.Buffer)124 c.SetOutput(buf)125 c.Execute()126127 if !strings.Contains(buf.String(), check) {128 t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", check, buf.String())129 }130}131132func TestSingleCommand(t *testing.T) {133 c := initialize()134 c.AddCommand(cmdPrint, cmdEcho)135 c.SetArgs(strings.Split("print one two", " "))
251 unmodified lines
387 }388}389390func TestHelpCommand(t *testing.T) {381 buf := new(bytes.Buffer)391 c := initialize()392 cmdEcho.AddCommand(cmdTimes)393 c.AddCommand(cmdPrint, cmdEcho)394 c.SetArgs(strings.Split("help echo", " "))386 c.SetOutput(buf)387 c.Execute()395389 if !strings.Contains(buf.String(), cmdEcho.Long) {390 t.Errorf("Wrong error message displayed, \n %s", buf.String())391 }396 checkOutputContains(t, c, cmdEcho.Long)397393 buf.Reset()398 c = initialize()399 cmdEcho.AddCommand(cmdTimes)400 c.AddCommand(cmdPrint, cmdEcho)401 c.SetArgs(strings.Split("help echo times", " "))398 c.SetOutput(buf)399 c.Execute()402401 if !strings.Contains(buf.String(), cmdTimes.Long) {402 t.Errorf("Wrong error message displayed, \n %s", buf.String())403 }403 checkOutputContains(t, c, cmdTimes.Long)404}405406func TestRunnableRootCommand(t *testing.T) {407 c := initializeWithRootCmd()
18 unmodified lines
426427 if flagir != 17 {428 t.Errorf("flag value should be 17, %d given", flagir)429 }430}431432func TestRootHelp(t *testing.T) {433 fmt.Println("testing root help")434 c := initializeWithRootCmd()435 c.AddCommand(cmdPrint, cmdEcho)436 c.SetArgs(strings.Split("--help", " "))437 e := c.Execute()438 fmt.Println(e)439440 checkOutputContains(t, c, "Available Commands:")441}
modified:command.go+3 added
342 unmodified lines
343 // Now handle the case where the root is runnable and only flags are provided344 if err != nil && c.Runnable() {345 e := c.ParseFlags(args)346 if e != nil {347 // Flags parsing had an error.348 fmt.Println(e)349 c.Usage()350 return e351 } else {352 argWoFlags := c.Flags().Args()353 if len(argWoFlags) > 0 {
302 unmodified lines