forked from mirror/cobra
[silence-errors]: adds Silence Usage
This commit is contained in:
parent
03aabcda72
commit
4729b374ae
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue