mirror of
https://github.com/spf13/cobra
synced 2025-05-06 05:17:21 +00:00
add: user defined error function
This commit is contained in:
parent
67fc4837d2
commit
0a20c1e0ec
1 changed files with 33 additions and 2 deletions
35
command.go
35
command.go
|
@ -179,6 +179,8 @@ type Command struct {
|
||||||
|
|
||||||
// output is an output writer defined by user.
|
// output is an output writer defined by user.
|
||||||
output io.Writer
|
output io.Writer
|
||||||
|
// errorFunc is error func defined by user.
|
||||||
|
errorFunc func(error)
|
||||||
// usageFunc is usage func defined by user.
|
// usageFunc is usage func defined by user.
|
||||||
usageFunc func(*Command) error
|
usageFunc func(*Command) error
|
||||||
// usageTemplate is usage template defined by user.
|
// usageTemplate is usage template defined by user.
|
||||||
|
@ -836,7 +838,7 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
|
||||||
c = cmd
|
c = cmd
|
||||||
}
|
}
|
||||||
if !c.SilenceErrors {
|
if !c.SilenceErrors {
|
||||||
c.Println("Error:", err.Error())
|
c.printError(err)
|
||||||
c.Printf("Run '%v --help' for usage.\n", c.CommandPath())
|
c.Printf("Run '%v --help' for usage.\n", c.CommandPath())
|
||||||
}
|
}
|
||||||
return c, err
|
return c, err
|
||||||
|
@ -859,7 +861,7 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
|
||||||
// If root command has SilentErrors flagged,
|
// If root command has SilentErrors flagged,
|
||||||
// all subcommands should respect it
|
// all subcommands should respect it
|
||||||
if !cmd.SilenceErrors && !c.SilenceErrors {
|
if !cmd.SilenceErrors && !c.SilenceErrors {
|
||||||
c.Println("Error:", err.Error())
|
c.printError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// If root command has SilentUsage flagged,
|
// If root command has SilentUsage flagged,
|
||||||
|
@ -871,6 +873,35 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
|
||||||
return cmd, err
|
return cmd, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ErrorFunc returns either the function set by SetErrorFunc for this command
|
||||||
|
// or a parent, or it returns a default error function.
|
||||||
|
func (c *Command) ErrorFunc() func(error) {
|
||||||
|
if c.errorFunc != nil {
|
||||||
|
return c.errorFunc
|
||||||
|
}
|
||||||
|
if c.HasParent() {
|
||||||
|
return c.Parent().ErrorFunc()
|
||||||
|
}
|
||||||
|
return func(err error) {
|
||||||
|
c.Println("Error:", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetErrorFunc sets error function.
|
||||||
|
func (c *Command) SetErrorFunc(errFunc func(error)) {
|
||||||
|
c.errorFunc = errFunc
|
||||||
|
}
|
||||||
|
|
||||||
|
// printError would pick user defined error function if existed
|
||||||
|
// else it would use it's default way to print
|
||||||
|
func (c *Command) printError(err error) {
|
||||||
|
if c.errorFunc != nil {
|
||||||
|
c.errorFunc(err)
|
||||||
|
} else {
|
||||||
|
c.Println("Error:", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Command) ValidateArgs(args []string) error {
|
func (c *Command) ValidateArgs(args []string) error {
|
||||||
if c.Args == nil {
|
if c.Args == nil {
|
||||||
return nil
|
return nil
|
||||||
|
|
Loading…
Add table
Reference in a new issue