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
Provenance
- Commit
230787e- Tree
bd384ae- Author
Eric Paris <eparis@redhat.com>- Committer
Eric Paris <eparis@redhat.com>- Parent 1
8b2293c- Parent 2
2244c39
2 files changed+55 added−22 removed
modified:cobra_test.go+33 added-14 removed
215 unmodified lines216 217 return fullTester(c, input)218 }219 220+func noRRSetupTestSilenced(input string) resulter {221+ c := initialize()222+ c.SilenceErrors = true223+ c.SilenceUsage = true224+ return fullTester(c, input)225+}226+227 func noRRSetupTest(input string) resulter {228 c := initialize()229 230 return fullTester(c, input)263 unmodified lines494 if r.Error == nil {495 t.Errorf("invalid flag should generate error")496 }497 491- 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 }501 502 if flagi2 != 99 {503 t.Errorf("flag value should be 99, %d given", flagi2)8 unmodified lines512 513 if r.Error == nil {514 t.Errorf("invalid flag should generate error")515 }509-510- 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 }519 520 // Testing with persistent flag overwritten by child521 noRRSetupTest("echo times --strtwo=child one two")11 unmodified lines533 534 if r.Error == nil {535 t.Errorf("invalid input should generate error")536 }531-532- 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 }541 542 func TestTrailingCommandFlags(t *testing.T) {8 unmodified lines551 cmd := initializeWithRootCmd()552 cmd.AddCommand(cmdTimes)553 554 result := simpleTester(cmd, "times --inttwo=2 --badflag=bar")550-551- checkResultContains(t, result, "unknown flag: --badflag")552-553- 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 }561 562 }250 unmodified lines813 t.Errorf("Unexpected response.\nExpecting to be:\n %q\nGot:\n %q\n", s, r.Output)814 }815 }816 817+func TestRootUnknownCommandSilenced(t *testing.T) {818+ r := noRRSetupTestSilenced("bogus")819+ s := "Run 'cobra-test --help' for usage.\n"820+821+ if r.Output != "" {822+ t.Errorf("Unexpected response.\nExpecting to be: \n\"\"\n Got:\n %q\n", s, r.Output)823+ }824+825+ 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+}830+831 func 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 lines890 }891 892 // 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 }897 898 //With quotes899 x = fullSetupTest("-s=\"walking\" echo")178 unmodified linesmodified:command.go+22 added-8 removed
59 unmodified lines60 // 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 lines629 // 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 }639 640 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 }643-658 return659 }660 661 func (c *Command) initHelpFlag() {516 unmodified lines