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:
Tarcísio Gruppi
2015-07-28 20:05:14 +02:00
parent 9c0db3f4ac
commit 49c1229409
4 changed files with 43 additions and 38 deletions

View File

@@ -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