From f6e1a2df92640371f70d01ef6a1e80a15d8358c8 Mon Sep 17 00:00:00 2001 From: Austin Riendeau Date: Sun, 25 Oct 2015 19:58:53 -0600 Subject: [PATCH 1/4] [silence-errors]: -m adds a way for errors to silenced down the stack --- cobra_test.go | 46 ++++++++++++++++++++++++++++++++-------------- command.go | 22 ++++++++++++++-------- 2 files changed, 46 insertions(+), 22 deletions(-) diff --git a/cobra_test.go b/cobra_test.go index f4814ead..66e44e4b 100644 --- a/cobra_test.go +++ b/cobra_test.go @@ -217,6 +217,12 @@ func fullSetupTest(input string) resulter { return fullTester(c, input) } +func noRRSetupTestSilenced(input string) resulter { + c := initialize() + c.SilenceErrors = true + return fullTester(c, input) +} + func noRRSetupTest(input string) resulter { c := initialize() @@ -488,8 +494,8 @@ func TestChildCommandFlags(t *testing.T) { t.Errorf("invalid flag should generate error") } - if !strings.Contains(r.Output, "unknown shorthand") { - t.Errorf("Wrong error message displayed, \n %s", r.Output) + if !strings.Contains(r.Error.Error(), "unknown shorthand") { + t.Errorf("Wrong error message displayed, \n %s", r.Error) } if flagi2 != 99 { @@ -506,9 +512,8 @@ func TestChildCommandFlags(t *testing.T) { if r.Error == nil { t.Errorf("invalid flag should generate error") } - - if !strings.Contains(r.Output, "unknown shorthand flag") { - t.Errorf("Wrong error message displayed, \n %s", r.Output) + if !strings.Contains(r.Error.Error(), "unknown shorthand flag") { + t.Errorf("Wrong error message displayed, \n %s", r.Error) } // Testing with persistent flag overwritten by child @@ -528,9 +533,8 @@ func TestChildCommandFlags(t *testing.T) { if r.Error == nil { t.Errorf("invalid input should generate error") } - - if !strings.Contains(r.Output, "invalid argument \"10E\" for i10E") { - t.Errorf("Wrong error message displayed, \n %s", r.Output) + if !strings.Contains(r.Error.Error(), "invalid argument \"10E\" for i10E") { + t.Errorf("Wrong error message displayed, \n %s", r.Error) } } @@ -547,10 +551,10 @@ func TestInvalidSubcommandFlags(t *testing.T) { cmd.AddCommand(cmdTimes) result := simpleTester(cmd, "times --inttwo=2 --badflag=bar") - - checkResultContains(t, result, "unknown flag: --badflag") - - if strings.Contains(result.Output, "unknown flag: --inttwo") { + // given that we are not checking here result.Error we check for + // stock usage message + checkResultContains(t, result, "cobra-test times [# times]") + if strings.Contains(result.Error.Error(), "unknown flag: --inttwo") { t.Errorf("invalid --badflag flag shouldn't fail on 'unknown' --inttwo flag") } @@ -809,6 +813,20 @@ func TestRootUnknownCommand(t *testing.T) { } } +func TestRootUnknownCommandSilenced(t *testing.T) { + r := noRRSetupTestSilenced("bogus") + s := "Run 'cobra-test --help' for usage.\n" + + if r.Output != "" { + t.Errorf("Unexpected response.\nExpecting to be: \n\"\"\n Got:\n %q\n", s, r.Output) + } + + r = noRRSetupTestSilenced("--strtwo=a bogus") + if r.Output != "" { + t.Errorf("Unexpected response.\nExpecting to be:\n\"\"\nGot:\n %q\n", s, r.Output) + } +} + func TestRootSuggestions(t *testing.T) { outputWithSuggestions := "Error: unknown command \"%s\" for \"cobra-test\"\n\nDid you mean this?\n\t%s\n\nRun 'cobra-test --help' for usage.\n" outputWithoutSuggestions := "Error: unknown command \"%s\" for \"cobra-test\"\nRun 'cobra-test --help' for usage.\n" @@ -872,8 +890,8 @@ func TestFlagsBeforeCommand(t *testing.T) { // With parsing error properly reported x = fullSetupTest("-i10E echo") - if !strings.Contains(x.Output, "invalid argument \"10E\" for i10E") { - t.Errorf("Wrong error message displayed, \n %s", x.Output) + if !strings.Contains(x.Error.Error(), "invalid argument \"10E\" for i10E") { + t.Errorf("Wrong error message displayed, \n %s", x.Error) } //With quotes diff --git a/command.go b/command.go index 20412439..89854cba 100644 --- a/command.go +++ b/command.go @@ -61,6 +61,8 @@ type Command struct { pflags *flag.FlagSet // Flags that are declared specifically by this command (not inherited). lflags *flag.FlagSet + // SilenceErrors is an option to quiet errors down stream + SilenceErrors bool // The *Run functions are executed in the following order: // * PersistentPreRun() // * PreRun() @@ -626,21 +628,25 @@ func (c *Command) Execute() (err error) { if cmd != nil { c = cmd } - c.Println("Error:", err.Error()) - c.Printf("Run '%v --help' for usage.\n", c.CommandPath()) + if !c.SilenceErrors { + c.Println("Error:", err.Error()) + c.Printf("Run '%v --help' for usage.\n", c.CommandPath()) + } return err } err = cmd.execute(flags) if err != nil { - if err == flag.ErrHelp { - cmd.HelpFunc()(cmd, args) - return nil + if !cmd.SilenceErrors { + if err == flag.ErrHelp { + cmd.HelpFunc()(cmd, args) + return nil + } + c.Println(cmd.UsageString()) + c.Println("Error:", err.Error()) } - c.Println(cmd.UsageString()) - c.Println("Error:", err.Error()) + return err } - return } From 03aabcda72c59d327a9090c0e156b42e46bdd323 Mon Sep 17 00:00:00 2001 From: Austin Riendeau Date: Sun, 25 Oct 2015 21:17:39 -0600 Subject: [PATCH 2/4] adds inheritance for SilenceErrors --- command.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/command.go b/command.go index 89854cba..9fdb1821 100644 --- a/command.go +++ b/command.go @@ -637,7 +637,8 @@ func (c *Command) Execute() (err error) { err = cmd.execute(flags) if err != nil { - if !cmd.SilenceErrors { + // If root is silenced, all subcommands should have the same + if !cmd.SilenceErrors && !c.SilenceErrors { if err == flag.ErrHelp { cmd.HelpFunc()(cmd, args) return nil From 4729b374ae40ca6714f87797f63ff446434ef38f Mon Sep 17 00:00:00 2001 From: Austin Riendeau Date: Mon, 26 Oct 2015 17:38:08 -0600 Subject: [PATCH 3/4] [silence-errors]: adds Silence Usage --- cobra_test.go | 1 + command.go | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/cobra_test.go b/cobra_test.go index 66e44e4b..c26633ed 100644 --- a/cobra_test.go +++ b/cobra_test.go @@ -220,6 +220,7 @@ func fullSetupTest(input string) resulter { func noRRSetupTestSilenced(input string) resulter { c := initialize() c.SilenceErrors = true + c.SilenceUsage = true return fullTester(c, input) } diff --git a/command.go b/command.go index 9fdb1821..6d1f56e0 100644 --- a/command.go +++ b/command.go @@ -63,6 +63,8 @@ type Command struct { lflags *flag.FlagSet // SilenceErrors is an option to quiet errors down stream SilenceErrors bool + // Silence Usage is an option to silence usage when an error occurs. + SilenceUsage bool // The *Run functions are executed in the following order: // * PersistentPreRun() // * PreRun() @@ -638,12 +640,14 @@ func (c *Command) Execute() (err error) { err = cmd.execute(flags) if err != nil { // If root is silenced, all subcommands should have the same + if !cmd.SilenceUsage && !c.SilenceUsage { + c.Println(cmd.UsageString()) + } if !cmd.SilenceErrors && !c.SilenceErrors { if err == flag.ErrHelp { cmd.HelpFunc()(cmd, args) return nil } - c.Println(cmd.UsageString()) c.Println("Error:", err.Error()) } return err From 2244c3923a93b045f65a3a56172ce48100baa50e Mon Sep 17 00:00:00 2001 From: Austin Riendeau Date: Mon, 26 Oct 2015 17:42:06 -0600 Subject: [PATCH 4/4] [silence-errors]: adds comments around code --- command.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/command.go b/command.go index 6d1f56e0..f0587f9b 100644 --- a/command.go +++ b/command.go @@ -639,10 +639,13 @@ func (c *Command) Execute() (err error) { err = cmd.execute(flags) if err != nil { - // If root is silenced, all subcommands should have the same + // If root command has SilentUsage flagged, + // all subcommands should respect it if !cmd.SilenceUsage && !c.SilenceUsage { c.Println(cmd.UsageString()) } + // If root command has SilentErrors flagged, + // all subcommands should respect it if !cmd.SilenceErrors && !c.SilenceErrors { if err == flag.ErrHelp { cmd.HelpFunc()(cmd, args)