[silence-errors]: adds Silence Usage

This commit is contained in:
Austin Riendeau 2015-10-26 17:38:08 -06:00
parent 03aabcda72
commit 4729b374ae
2 changed files with 6 additions and 1 deletions

View File

@ -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)
}

View File

@ -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