@ -8,6 +8,11 @@ import (
"time"
)
var (
// Set to 125 which is the highest number not used in most shells
DefaultExitCode int = 0
)
// App is the main structure of a cli application. It is recomended that
// an app be created with the cli.NewApp() function
type App struct {
@ -77,7 +82,7 @@ func NewApp() *App {
}
// Entry point to the cli app. Parses the arguments slice and routes to the proper flag/args combination
func ( a * App ) Run ( arguments [ ] string ) ( e rr error ) {
func ( a * App ) Run ( arguments [ ] string ) ( e c int , e rr error ) {
if a . Author != "" || a . Email != "" {
a . Authors = append ( a . Authors , Author { Name : a . Author , Email : a . Email } )
}
@ -108,7 +113,7 @@ func (a *App) Run(arguments []string) (err error) {
fmt . Fprintln ( a . Writer , nerr )
context := NewContext ( a , set , nil )
ShowAppHelp ( context )
return nerr
return DefaultExitCode, nerr
}
context := NewContext ( a , set , nil )
@ -116,24 +121,24 @@ func (a *App) Run(arguments []string) (err error) {
fmt . Fprintln ( a . Writer , "Incorrect Usage." )
fmt . Fprintln ( a . Writer )
ShowAppHelp ( context )
return err
return DefaultExitCode, err
}
if checkCompletions ( context ) {
return nil
return 0 , nil
}
if checkHelp ( context ) {
return nil
return 0 , nil
}
if checkVersion ( context ) {
return nil
return 0 , nil
}
if a . After != nil {
defer func ( ) {
afterE rr := a . After ( context )
afterE c, afterE rr := a . After ( context )
if afterErr != nil {
if err != nil {
err = NewMultiError ( err , afterErr )
@ -141,13 +146,14 @@ func (a *App) Run(arguments []string) (err error) {
err = afterErr
}
}
ec = afterEc
} ( )
}
if a . Before != nil {
e rr : = a . Before ( context )
e c, err = a . Before ( context )
if err != nil {
return e rr
return e c, e rr
}
}
@ -161,20 +167,19 @@ func (a *App) Run(arguments []string) (err error) {
}
// Run default Action
a . Action ( context )
return nil
return a . Action ( context ) , nil
}
// Another entry point to the cli app, takes care of passing arguments and error handling
func ( a * App ) RunAndExitOnError ( ) {
if e rr := a . Run ( os . Args ) ; err != nil {
if e xitCode, e rr := a . Run ( os . Args ) ; err != nil {
fmt . Fprintln ( os . Stderr , err )
os . Exit ( 1 )
os . Exit ( exitCode )
}
}
// Invokes the subcommand given the context, parses ctx.Args() to generate command-specific flags
func ( a * App ) RunAsSubcommand ( ctx * Context ) ( e rr error ) {
func ( a * App ) RunAsSubcommand ( ctx * Context ) ( e c int , e rr error ) {
// append help to commands
if len ( a . Commands ) > 0 {
if a . Command ( helpCommand . Name ) == nil && ! a . HideHelp {
@ -205,33 +210,33 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
} else {
ShowCommandHelp ( ctx , context . Args ( ) . First ( ) )
}
return nerr
return DefaultExitCode, nerr
}
if err != nil {
fmt . Fprintln ( a . Writer , "Incorrect Usage." )
fmt . Fprintln ( a . Writer )
ShowSubcommandHelp ( context )
return err
return DefaultExitCode, err
}
if checkCompletions ( context ) {
return nil
return 0 , nil
}
if len ( a . Commands ) > 0 {
if checkSubcommandHelp ( context ) {
return nil
return 0 , nil
}
} else {
if checkCommandHelp ( ctx , context . Args ( ) . First ( ) ) {
return nil
return 0 , nil
}
}
if a . After != nil {
defer func ( ) {
afterE rr := a . After ( context )
afterE c, afterE rr := a . After ( context )
if afterErr != nil {
if err != nil {
err = NewMultiError ( err , afterErr )
@ -239,13 +244,14 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
err = afterErr
}
}
ec = afterEc
} ( )
}
if a . Before != nil {
e rr : = a . Before ( context )
e c, err = a . Before ( context )
if err != nil {
return e rr
return e c, e rr
}
}
@ -259,9 +265,7 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
}
// Run default Action
a . Action ( context )
return nil
return a . Action ( context ) , nil
}
// Returns the named command on App. Returns nil if the command does not exist