diff --git a/app.go b/app.go index 764aaef7df..8321ceae04 100644 --- a/app.go +++ b/app.go @@ -115,6 +115,7 @@ func NewApp() *App { Action: helpCommand.Action, Compiled: compileTime(), Writer: os.Stdout, + ExitErrHandler: HandleExitCoder, } } @@ -204,7 +205,7 @@ func (a *App) Run(arguments []string) (err error) { if err != nil { if a.OnUsageError != nil { err := a.OnUsageError(context, err, false) - a.handleExitCoder(err) + a.ExitErrHandler(err) return err } fmt.Fprintf(a.Writer, "%s %s\n\n", "Incorrect Usage.", err.Error()) @@ -237,9 +238,8 @@ func (a *App) Run(arguments []string) (err error) { if a.Before != nil { beforeErr := a.Before(context) if beforeErr != nil { - fmt.Fprintf(a.Writer, "%v\n\n", beforeErr) ShowAppHelp(context) - a.handleExitCoder(beforeErr) + a.ExitErrHandler(beforeErr) err = beforeErr return err } @@ -261,7 +261,7 @@ func (a *App) Run(arguments []string) (err error) { // Run default Action err = HandleAction(a.Action, context) - a.handleExitCoder(err) + a.ExitErrHandler(err) return err } @@ -328,7 +328,7 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) { if err != nil { if a.OnUsageError != nil { err = a.OnUsageError(context, err, true) - a.handleExitCoder(err) + a.ExitErrHandler(err) return err } fmt.Fprintf(a.Writer, "%s %s\n\n", "Incorrect Usage.", err.Error()) @@ -350,7 +350,7 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) { defer func() { afterErr := a.After(context) if afterErr != nil { - a.handleExitCoder(err) + a.ExitErrHandler(err) if err != nil { err = NewMultiError(err, afterErr) } else { @@ -363,7 +363,7 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) { if a.Before != nil { beforeErr := a.Before(context) if beforeErr != nil { - a.handleExitCoder(beforeErr) + a.ExitErrHandler(beforeErr) err = beforeErr return err } @@ -381,7 +381,7 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) { // Run default Action err = HandleAction(a.Action, context) - a.handleExitCoder(err) + a.ExitErrHandler(err) return err } @@ -462,14 +462,6 @@ func (a *App) appendFlag(flag Flag) { } } -func (a *App) handleExitCoder(err error) { - if a.ExitErrHandler != nil { - a.ExitErrHandler(err) - } else { - HandleExitCoder(err) - } -} - // Author represents someone who has contributed to a cli project. type Author struct { Name string // The Authors name