Added exit code support
Now the exit code can be returned by BeforeFn, ActionFn and AfterFn. The `os.Exit` function is not called by this packaged This closes #66 and closes #164
This commit is contained in:
15
command.go
15
command.go
@@ -50,7 +50,7 @@ func (c Command) FullName() string {
|
||||
}
|
||||
|
||||
// Invokes the command given the context, parses ctx.Args() to generate command-specific flags
|
||||
func (c Command) Run(ctx *Context) error {
|
||||
func (c Command) Run(ctx *Context) (int, error) {
|
||||
if len(c.Subcommands) > 0 || c.Before != nil || c.After != nil {
|
||||
return c.startApp(ctx)
|
||||
}
|
||||
@@ -104,7 +104,7 @@ func (c Command) Run(ctx *Context) error {
|
||||
fmt.Fprintln(ctx.App.Writer, "Incorrect Usage.")
|
||||
fmt.Fprintln(ctx.App.Writer)
|
||||
ShowCommandHelp(ctx, c.Name)
|
||||
return err
|
||||
return DefaultExitCode, err
|
||||
}
|
||||
|
||||
nerr := normalizeFlags(c.Flags, set)
|
||||
@@ -112,20 +112,19 @@ func (c Command) Run(ctx *Context) error {
|
||||
fmt.Fprintln(ctx.App.Writer, nerr)
|
||||
fmt.Fprintln(ctx.App.Writer)
|
||||
ShowCommandHelp(ctx, c.Name)
|
||||
return nerr
|
||||
return DefaultExitCode, nerr
|
||||
}
|
||||
context := NewContext(ctx.App, set, ctx)
|
||||
|
||||
if checkCommandCompletions(context, c.Name) {
|
||||
return nil
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
if checkCommandHelp(context, c.Name) {
|
||||
return nil
|
||||
return 0, nil
|
||||
}
|
||||
context.Command = c
|
||||
c.Action(context)
|
||||
return nil
|
||||
return c.Action(context), nil
|
||||
}
|
||||
|
||||
func (c Command) Names() []string {
|
||||
@@ -148,7 +147,7 @@ func (c Command) HasName(name string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (c Command) startApp(ctx *Context) error {
|
||||
func (c Command) startApp(ctx *Context) (int, error) {
|
||||
app := NewApp()
|
||||
|
||||
// set the name and usage
|
||||
|
||||
Reference in New Issue
Block a user