Skip to content
Myr

Merge pull request #169 from apriendeau/silence-errors

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

[silence-errors]: adds a way for errors to silenced down the stack

authored by Eric Paris · committed by Eric Paris · parents 8b2293c, 2244c39 · Browse files · 2 files+55 added−22 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+55 added−22 removed

  • other:+55 added−22 removed

2 files+55 added−22 removed

modified:cobra_test.go+33 added-14 removed
215 unmodified lines
216217 return fullTester(c, input)218}219220func noRRSetupTestSilenced(input string) resulter {221 c := initialize()222 c.SilenceErrors = true223 c.SilenceUsage = true224 return fullTester(c, input)225}226227func noRRSetupTest(input string) resulter {228 c := initialize()229230 return fullTester(c, input)
263 unmodified lines
494 if r.Error == nil {495 t.Errorf("invalid flag should generate error")496 }497491 if !strings.Contains(r.Output, "unknown shorthand") {492 t.Errorf("Wrong error message displayed, \n %s", r.Output)498 if !strings.Contains(r.Error.Error(), "unknown shorthand") {499 t.Errorf("Wrong error message displayed, \n %s", r.Error)500 }501502 if flagi2 != 99 {503 t.Errorf("flag value should be 99, %d given", flagi2)
8 unmodified lines
512513 if r.Error == nil {514 t.Errorf("invalid flag should generate error")515 }509510 if !strings.Contains(r.Output, "unknown shorthand flag") {511 t.Errorf("Wrong error message displayed, \n %s", r.Output)516 if !strings.Contains(r.Error.Error(), "unknown shorthand flag") {517 t.Errorf("Wrong error message displayed, \n %s", r.Error)518 }519520 // Testing with persistent flag overwritten by child521 noRRSetupTest("echo times --strtwo=child one two")
11 unmodified lines
533534 if r.Error == nil {535 t.Errorf("invalid input should generate error")536 }531532 if !strings.Contains(r.Output, "invalid argument \"10E\" for i10E") {533 t.Errorf("Wrong error message displayed, \n %s", r.Output)537 if !strings.Contains(r.Error.Error(), "invalid argument \"10E\" for i10E") {538 t.Errorf("Wrong error message displayed, \n %s", r.Error)539 }540}541542func TestTrailingCommandFlags(t *testing.T) {
8 unmodified lines
551 cmd := initializeWithRootCmd()552 cmd.AddCommand(cmdTimes)553554 result := simpleTester(cmd, "times --inttwo=2 --badflag=bar")550551 checkResultContains(t, result, "unknown flag: --badflag")552553 if strings.Contains(result.Output, "unknown flag: --inttwo") {555 // given that we are not checking here result.Error we check for556 // stock usage message557 checkResultContains(t, result, "cobra-test times [# times]")558 if strings.Contains(result.Error.Error(), "unknown flag: --inttwo") {559 t.Errorf("invalid --badflag flag shouldn't fail on 'unknown' --inttwo flag")560 }561562}
250 unmodified lines
813 t.Errorf("Unexpected response.\nExpecting to be:\n %q\nGot:\n %q\n", s, r.Output)814 }815}816817func TestRootUnknownCommandSilenced(t *testing.T) {818 r := noRRSetupTestSilenced("bogus")819 s := "Run 'cobra-test --help' for usage.\n"820821 if r.Output != "" {822 t.Errorf("Unexpected response.\nExpecting to be: \n\"\"\n Got:\n %q\n", s, r.Output)823 }824825 r = noRRSetupTestSilenced("--strtwo=a bogus")826 if r.Output != "" {827 t.Errorf("Unexpected response.\nExpecting to be:\n\"\"\nGot:\n %q\n", s, r.Output)828 }829}830831func TestRootSuggestions(t *testing.T) {832 outputWithSuggestions := "Error: unknown command \"%s\" for \"cobra-test\"\n\nDid you mean this?\n\t%s\n\nRun 'cobra-test --help' for usage.\n"833 outputWithoutSuggestions := "Error: unknown command \"%s\" for \"cobra-test\"\nRun 'cobra-test --help' for usage.\n"834
55 unmodified lines
890 }891892 // With parsing error properly reported893 x = fullSetupTest("-i10E echo")875 if !strings.Contains(x.Output, "invalid argument \"10E\" for i10E") {876 t.Errorf("Wrong error message displayed, \n %s", x.Output)894 if !strings.Contains(x.Error.Error(), "invalid argument \"10E\" for i10E") {895 t.Errorf("Wrong error message displayed, \n %s", x.Error)896 }897898 //With quotes899 x = fullSetupTest("-s=\"walking\" echo")
178 unmodified lines
modified:command.go+22 added-8 removed
59 unmodified lines
60 // Set of flags childrens of this command will inherit61 pflags *flag.FlagSet62 // Flags that are declared specifically by this command (not inherited).63 lflags *flag.FlagSet64 // SilenceErrors is an option to quiet errors down stream65 SilenceErrors bool66 // Silence Usage is an option to silence usage when an error occurs.67 SilenceUsage bool68 // The *Run functions are executed in the following order:69 // * PersistentPreRun()70 // * PreRun()71 // * Run()
557 unmodified lines
629 // If found parse to a subcommand and then failed, talk about the subcommand630 if cmd != nil {631 c = cmd632 }633 if !c.SilenceErrors {629 c.Println("Error:", err.Error())630 c.Printf("Run '%v --help' for usage.\n", c.CommandPath())634 c.Println("Error:", err.Error())635 c.Printf("Run '%v --help' for usage.\n", c.CommandPath())636 }637 return err638 }639640 err = cmd.execute(flags)641 if err != nil {636 if err == flag.ErrHelp {637 cmd.HelpFunc()(cmd, args)638 return nil642 // If root command has SilentUsage flagged,643 // all subcommands should respect it644 if !cmd.SilenceUsage && !c.SilenceUsage {645 c.Println(cmd.UsageString())646 }640 c.Println(cmd.UsageString())641 c.Println("Error:", err.Error())647 // If root command has SilentErrors flagged,648 // all subcommands should respect it649 if !cmd.SilenceErrors && !c.SilenceErrors {650 if err == flag.ErrHelp {651 cmd.HelpFunc()(cmd, args)652 return nil653 }654 c.Println("Error:", err.Error())655 }656 return err657 }643658 return659}660661func (c *Command) initHelpFlag() {
516 unmodified lines