mirror of
https://github.com/spf13/cobra
synced 2024-11-24 22:57:12 +00:00
Merge pull request #169 from apriendeau/silence-errors
[silence-errors]: adds a way for errors to silenced down the stack
This commit is contained in:
commit
230787ee13
2 changed files with 55 additions and 22 deletions
|
@ -217,6 +217,13 @@ func fullSetupTest(input string) resulter {
|
||||||
return fullTester(c, input)
|
return fullTester(c, input)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func noRRSetupTestSilenced(input string) resulter {
|
||||||
|
c := initialize()
|
||||||
|
c.SilenceErrors = true
|
||||||
|
c.SilenceUsage = true
|
||||||
|
return fullTester(c, input)
|
||||||
|
}
|
||||||
|
|
||||||
func noRRSetupTest(input string) resulter {
|
func noRRSetupTest(input string) resulter {
|
||||||
c := initialize()
|
c := initialize()
|
||||||
|
|
||||||
|
@ -488,8 +495,8 @@ func TestChildCommandFlags(t *testing.T) {
|
||||||
t.Errorf("invalid flag should generate error")
|
t.Errorf("invalid flag should generate error")
|
||||||
}
|
}
|
||||||
|
|
||||||
if !strings.Contains(r.Output, "unknown shorthand") {
|
if !strings.Contains(r.Error.Error(), "unknown shorthand") {
|
||||||
t.Errorf("Wrong error message displayed, \n %s", r.Output)
|
t.Errorf("Wrong error message displayed, \n %s", r.Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
if flagi2 != 99 {
|
if flagi2 != 99 {
|
||||||
|
@ -506,9 +513,8 @@ func TestChildCommandFlags(t *testing.T) {
|
||||||
if r.Error == nil {
|
if r.Error == nil {
|
||||||
t.Errorf("invalid flag should generate error")
|
t.Errorf("invalid flag should generate error")
|
||||||
}
|
}
|
||||||
|
if !strings.Contains(r.Error.Error(), "unknown shorthand flag") {
|
||||||
if !strings.Contains(r.Output, "unknown shorthand flag") {
|
t.Errorf("Wrong error message displayed, \n %s", r.Error)
|
||||||
t.Errorf("Wrong error message displayed, \n %s", r.Output)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Testing with persistent flag overwritten by child
|
// Testing with persistent flag overwritten by child
|
||||||
|
@ -528,9 +534,8 @@ func TestChildCommandFlags(t *testing.T) {
|
||||||
if r.Error == nil {
|
if r.Error == nil {
|
||||||
t.Errorf("invalid input should generate error")
|
t.Errorf("invalid input should generate error")
|
||||||
}
|
}
|
||||||
|
if !strings.Contains(r.Error.Error(), "invalid argument \"10E\" for i10E") {
|
||||||
if !strings.Contains(r.Output, "invalid argument \"10E\" for i10E") {
|
t.Errorf("Wrong error message displayed, \n %s", r.Error)
|
||||||
t.Errorf("Wrong error message displayed, \n %s", r.Output)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -547,10 +552,10 @@ func TestInvalidSubcommandFlags(t *testing.T) {
|
||||||
cmd.AddCommand(cmdTimes)
|
cmd.AddCommand(cmdTimes)
|
||||||
|
|
||||||
result := simpleTester(cmd, "times --inttwo=2 --badflag=bar")
|
result := simpleTester(cmd, "times --inttwo=2 --badflag=bar")
|
||||||
|
// given that we are not checking here result.Error we check for
|
||||||
checkResultContains(t, result, "unknown flag: --badflag")
|
// stock usage message
|
||||||
|
checkResultContains(t, result, "cobra-test times [# times]")
|
||||||
if strings.Contains(result.Output, "unknown flag: --inttwo") {
|
if strings.Contains(result.Error.Error(), "unknown flag: --inttwo") {
|
||||||
t.Errorf("invalid --badflag flag shouldn't fail on 'unknown' --inttwo flag")
|
t.Errorf("invalid --badflag flag shouldn't fail on 'unknown' --inttwo flag")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -809,6 +814,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) {
|
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"
|
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"
|
outputWithoutSuggestions := "Error: unknown command \"%s\" for \"cobra-test\"\nRun 'cobra-test --help' for usage.\n"
|
||||||
|
@ -872,8 +891,8 @@ func TestFlagsBeforeCommand(t *testing.T) {
|
||||||
|
|
||||||
// With parsing error properly reported
|
// With parsing error properly reported
|
||||||
x = fullSetupTest("-i10E echo")
|
x = fullSetupTest("-i10E echo")
|
||||||
if !strings.Contains(x.Output, "invalid argument \"10E\" for i10E") {
|
if !strings.Contains(x.Error.Error(), "invalid argument \"10E\" for i10E") {
|
||||||
t.Errorf("Wrong error message displayed, \n %s", x.Output)
|
t.Errorf("Wrong error message displayed, \n %s", x.Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
//With quotes
|
//With quotes
|
||||||
|
|
30
command.go
30
command.go
|
@ -61,6 +61,10 @@ type Command struct {
|
||||||
pflags *flag.FlagSet
|
pflags *flag.FlagSet
|
||||||
// Flags that are declared specifically by this command (not inherited).
|
// Flags that are declared specifically by this command (not inherited).
|
||||||
lflags *flag.FlagSet
|
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:
|
// The *Run functions are executed in the following order:
|
||||||
// * PersistentPreRun()
|
// * PersistentPreRun()
|
||||||
// * PreRun()
|
// * PreRun()
|
||||||
|
@ -626,21 +630,31 @@ func (c *Command) Execute() (err error) {
|
||||||
if cmd != nil {
|
if cmd != nil {
|
||||||
c = cmd
|
c = cmd
|
||||||
}
|
}
|
||||||
c.Println("Error:", err.Error())
|
if !c.SilenceErrors {
|
||||||
c.Printf("Run '%v --help' for usage.\n", c.CommandPath())
|
c.Println("Error:", err.Error())
|
||||||
|
c.Printf("Run '%v --help' for usage.\n", c.CommandPath())
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = cmd.execute(flags)
|
err = cmd.execute(flags)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == flag.ErrHelp {
|
// If root command has SilentUsage flagged,
|
||||||
cmd.HelpFunc()(cmd, args)
|
// all subcommands should respect it
|
||||||
return nil
|
if !cmd.SilenceUsage && !c.SilenceUsage {
|
||||||
|
c.Println(cmd.UsageString())
|
||||||
}
|
}
|
||||||
c.Println(cmd.UsageString())
|
// If root command has SilentErrors flagged,
|
||||||
c.Println("Error:", err.Error())
|
// all subcommands should respect it
|
||||||
|
if !cmd.SilenceErrors && !c.SilenceErrors {
|
||||||
|
if err == flag.ErrHelp {
|
||||||
|
cmd.HelpFunc()(cmd, args)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
c.Println("Error:", err.Error())
|
||||||
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue