Skip to content

Commit

Permalink
Fix how to do defaults in app.go
Browse files Browse the repository at this point in the history
  • Loading branch information
phinnaeus authored Apr 25, 2017
1 parent aa42dbd commit 8906567
Showing 1 changed file with 8 additions and 16 deletions.
24 changes: 8 additions & 16 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ func NewApp() *App {
Action: helpCommand.Action,
Compiled: compileTime(),
Writer: os.Stdout,
ExitErrHandler: HandleExitCoder,
}
}

Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}

Expand Down Expand Up @@ -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())
Expand All @@ -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 {
Expand All @@ -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
}
Expand All @@ -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
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8906567

Please sign in to comment.