mirror of https://github.com/spf13/cobra.git
[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 {
|
func noRRSetupTestSilenced(input string) resulter {
|
||||||
c := initialize()
|
c := initialize()
|
||||||
c.SilenceErrors = true
|
c.SilenceErrors = true
|
||||||
|
c.SilenceUsage = true
|
||||||
return fullTester(c, input)
|
return fullTester(c, input)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -63,6 +63,8 @@ type Command struct {
|
||||||
lflags *flag.FlagSet
|
lflags *flag.FlagSet
|
||||||
// SilenceErrors is an option to quiet errors down stream
|
// SilenceErrors is an option to quiet errors down stream
|
||||||
SilenceErrors bool
|
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()
|
||||||
|
@ -638,12 +640,14 @@ func (c *Command) Execute() (err error) {
|
||||||
err = cmd.execute(flags)
|
err = cmd.execute(flags)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// If root is silenced, all subcommands should have the same
|
// 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 !cmd.SilenceErrors && !c.SilenceErrors {
|
||||||
if err == flag.ErrHelp {
|
if err == flag.ErrHelp {
|
||||||
cmd.HelpFunc()(cmd, args)
|
cmd.HelpFunc()(cmd, args)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
c.Println(cmd.UsageString())
|
|
||||||
c.Println("Error:", err.Error())
|
c.Println("Error:", err.Error())
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
|
|
Loading…
Reference in New Issue