Switch from multi-return with exit codes to ExitCoder check

main
Dan Buch 8 years ago
parent b40b62794d
commit b7329f4968
No known key found for this signature in database
GPG Key ID: FAEF12936DD3E3EC

@ -39,13 +39,13 @@ func ApplyInputSourceValues(context *cli.Context, inputSourceContext InputSource
// input source based on the func provided. If there is no error it will then apply the new input source to any flags // input source based on the func provided. If there is no error it will then apply the new input source to any flags
// that are supported by the input source // that are supported by the input source
func InitInputSource(flags []cli.Flag, createInputSource func() (InputSourceContext, error)) cli.BeforeFunc { func InitInputSource(flags []cli.Flag, createInputSource func() (InputSourceContext, error)) cli.BeforeFunc {
return func(context *cli.Context) (int, error) { return func(context *cli.Context) error {
inputSource, err := createInputSource() inputSource, err := createInputSource()
if err != nil { if err != nil {
return cli.DefaultErrorExitCode, fmt.Errorf("Unable to create input source: inner error: \n'%v'", err.Error()) return fmt.Errorf("Unable to create input source: inner error: \n'%v'", err.Error())
} }
return cli.DefaultSuccessExitCode, ApplyInputSourceValues(context, inputSource, flags) return ApplyInputSourceValues(context, inputSource, flags)
} }
} }
@ -53,13 +53,13 @@ func InitInputSource(flags []cli.Flag, createInputSource func() (InputSourceCont
// input source based on the func provided with potentially using existing cli.Context values to initialize itself. If there is // input source based on the func provided with potentially using existing cli.Context values to initialize itself. If there is
// no error it will then apply the new input source to any flags that are supported by the input source // no error it will then apply the new input source to any flags that are supported by the input source
func InitInputSourceWithContext(flags []cli.Flag, createInputSource func(context *cli.Context) (InputSourceContext, error)) cli.BeforeFunc { func InitInputSourceWithContext(flags []cli.Flag, createInputSource func(context *cli.Context) (InputSourceContext, error)) cli.BeforeFunc {
return func(context *cli.Context) (int, error) { return func(context *cli.Context) error {
inputSource, err := createInputSource(context) inputSource, err := createInputSource(context)
if err != nil { if err != nil {
return cli.DefaultErrorExitCode, fmt.Errorf("Unable to create input source with context: inner error: \n'%v'", err.Error()) return fmt.Errorf("Unable to create input source with context: inner error: \n'%v'", err.Error())
} }
return cli.DefaultSuccessExitCode, ApplyInputSourceValues(context, inputSource, flags) return ApplyInputSourceValues(context, inputSource, flags)
} }
} }

@ -29,17 +29,17 @@ func TestCommandYamlFileTest(t *testing.T) {
Aliases: []string{"tc"}, Aliases: []string{"tc"},
Usage: "this is for testing", Usage: "this is for testing",
Description: "testing", Description: "testing",
Action: func(c *cli.Context) int { Action: func(c *cli.Context) error {
val := c.Int("test") val := c.Int("test")
expect(t, val, 15) expect(t, val, 15)
return 0 return nil
}, },
Flags: []cli.Flag{ Flags: []cli.Flag{
NewIntFlag(cli.IntFlag{Name: "test"}), NewIntFlag(cli.IntFlag{Name: "test"}),
cli.StringFlag{Name: "load"}}, cli.StringFlag{Name: "load"}},
} }
command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load")) command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load"))
_, err := command.Run(c) err := command.Run(c)
expect(t, err, nil) expect(t, err, nil)
} }
@ -62,10 +62,10 @@ func TestCommandYamlFileTestGlobalEnvVarWins(t *testing.T) {
Aliases: []string{"tc"}, Aliases: []string{"tc"},
Usage: "this is for testing", Usage: "this is for testing",
Description: "testing", Description: "testing",
Action: func(c *cli.Context) int { Action: func(c *cli.Context) error {
val := c.Int("test") val := c.Int("test")
expect(t, val, 10) expect(t, val, 10)
return 0 return nil
}, },
Flags: []cli.Flag{ Flags: []cli.Flag{
NewIntFlag(cli.IntFlag{Name: "test", EnvVar: "THE_TEST"}), NewIntFlag(cli.IntFlag{Name: "test", EnvVar: "THE_TEST"}),
@ -73,7 +73,7 @@ func TestCommandYamlFileTestGlobalEnvVarWins(t *testing.T) {
} }
command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load")) command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load"))
_, err := command.Run(c) err := command.Run(c)
expect(t, err, nil) expect(t, err, nil)
} }
@ -94,10 +94,10 @@ func TestCommandYamlFileTestSpecifiedFlagWins(t *testing.T) {
Aliases: []string{"tc"}, Aliases: []string{"tc"},
Usage: "this is for testing", Usage: "this is for testing",
Description: "testing", Description: "testing",
Action: func(c *cli.Context) int { Action: func(c *cli.Context) error {
val := c.Int("test") val := c.Int("test")
expect(t, val, 7) expect(t, val, 7)
return 0 return nil
}, },
Flags: []cli.Flag{ Flags: []cli.Flag{
NewIntFlag(cli.IntFlag{Name: "test"}), NewIntFlag(cli.IntFlag{Name: "test"}),
@ -105,7 +105,7 @@ func TestCommandYamlFileTestSpecifiedFlagWins(t *testing.T) {
} }
command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load")) command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load"))
_, err := command.Run(c) err := command.Run(c)
expect(t, err, nil) expect(t, err, nil)
} }
@ -126,10 +126,10 @@ func TestCommandYamlFileTestDefaultValueFileWins(t *testing.T) {
Aliases: []string{"tc"}, Aliases: []string{"tc"},
Usage: "this is for testing", Usage: "this is for testing",
Description: "testing", Description: "testing",
Action: func(c *cli.Context) int { Action: func(c *cli.Context) error {
val := c.Int("test") val := c.Int("test")
expect(t, val, 15) expect(t, val, 15)
return 0 return nil
}, },
Flags: []cli.Flag{ Flags: []cli.Flag{
NewIntFlag(cli.IntFlag{Name: "test", Value: 7}), NewIntFlag(cli.IntFlag{Name: "test", Value: 7}),
@ -137,7 +137,7 @@ func TestCommandYamlFileTestDefaultValueFileWins(t *testing.T) {
} }
command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load")) command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load"))
_, err := command.Run(c) err := command.Run(c)
expect(t, err, nil) expect(t, err, nil)
} }
@ -161,17 +161,17 @@ func TestCommandYamlFileFlagHasDefaultGlobalEnvYamlSetGlobalEnvWins(t *testing.T
Aliases: []string{"tc"}, Aliases: []string{"tc"},
Usage: "this is for testing", Usage: "this is for testing",
Description: "testing", Description: "testing",
Action: func(c *cli.Context) int { Action: func(c *cli.Context) error {
val := c.Int("test") val := c.Int("test")
expect(t, val, 11) expect(t, val, 11)
return 0 return nil
}, },
Flags: []cli.Flag{ Flags: []cli.Flag{
NewIntFlag(cli.IntFlag{Name: "test", Value: 7, EnvVar: "THE_TEST"}), NewIntFlag(cli.IntFlag{Name: "test", Value: 7, EnvVar: "THE_TEST"}),
cli.StringFlag{Name: "load"}}, cli.StringFlag{Name: "load"}},
} }
command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load")) command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load"))
_, err := command.Run(c) err := command.Run(c)
expect(t, err, nil) expect(t, err, nil)
} }

@ -100,7 +100,7 @@ func NewApp() *App {
} }
// Entry point to the cli app. Parses the arguments slice and routes to the proper flag/args combination // Entry point to the cli app. Parses the arguments slice and routes to the proper flag/args combination
func (a *App) Run(arguments []string) (ec int, err error) { func (a *App) Run(arguments []string) (err error) {
if a.Author != "" || a.Email != "" { if a.Author != "" || a.Email != "" {
a.Authors = append(a.Authors, Author{Name: a.Author, Email: a.Email}) a.Authors = append(a.Authors, Author{Name: a.Author, Email: a.Email})
} }
@ -146,40 +146,43 @@ func (a *App) Run(arguments []string) (ec int, err error) {
if nerr != nil { if nerr != nil {
fmt.Fprintln(a.Writer, nerr) fmt.Fprintln(a.Writer, nerr)
ShowAppHelp(context) ShowAppHelp(context)
return DefaultErrorExitCode, nerr return nerr
} }
if checkCompletions(context) { if checkCompletions(context) {
return DefaultSuccessExitCode, nil return nil
} }
if err != nil { if err != nil {
if a.OnUsageError != nil { if a.OnUsageError != nil {
err := a.OnUsageError(context, err, false) err := a.OnUsageError(context, err, false)
if err != nil { if err != nil {
return DefaultErrorExitCode, err if exitErr, ok := err.(ExitCoder); ok {
os.Exit(exitErr.ExitCode())
panic("unreachable")
}
} }
return DefaultSuccessExitCode, err return err
} else { } else {
fmt.Fprintf(a.Writer, "%s\n\n", "Incorrect Usage.") fmt.Fprintf(a.Writer, "%s\n\n", "Incorrect Usage.")
ShowAppHelp(context) ShowAppHelp(context)
return DefaultErrorExitCode, err return err
} }
} }
if !a.HideHelp && checkHelp(context) { if !a.HideHelp && checkHelp(context) {
ShowAppHelp(context) ShowAppHelp(context)
return DefaultSuccessExitCode, nil return nil
} }
if !a.HideVersion && checkVersion(context) { if !a.HideVersion && checkVersion(context) {
ShowVersion(context) ShowVersion(context)
return DefaultSuccessExitCode, nil return nil
} }
if a.After != nil { if a.After != nil {
defer func() { defer func() {
afterEc, afterErr := a.After(context) afterErr := a.After(context)
if afterErr != nil { if afterErr != nil {
if err != nil { if err != nil {
err = NewMultiError(err, afterErr) err = NewMultiError(err, afterErr)
@ -187,16 +190,21 @@ func (a *App) Run(arguments []string) (ec int, err error) {
err = afterErr err = afterErr
} }
} }
ec = afterEc
}() }()
} }
if a.Before != nil { if a.Before != nil {
ec, err = a.Before(context) err = a.Before(context)
if err != nil { if err != nil {
fmt.Fprintf(a.Writer, "%v\n\n", err) fmt.Fprintf(a.Writer, "%v\n\n", err)
ShowAppHelp(context) ShowAppHelp(context)
return ec, err
if exitErr, ok := err.(ExitCoder); ok {
os.Exit(exitErr.ExitCode())
panic("unreachable")
}
return err
} }
} }
@ -210,19 +218,34 @@ func (a *App) Run(arguments []string) (ec int, err error) {
} }
// Run default Action // Run default Action
return a.Action(context), nil err = a.Action(context)
if exitErr, ok := err.(ExitCoder); ok {
os.Exit(exitErr.ExitCode())
panic("unreachable")
}
return err
} }
// Another entry point to the cli app, takes care of passing arguments and error handling // Another entry point to the cli app, takes care of passing arguments and error handling
func (a *App) RunAndExitOnError() { func (a *App) RunAndExitOnError() {
if exitCode, err := a.Run(os.Args); err != nil { err := a.Run(os.Args)
fmt.Fprintln(os.Stderr, err) if exitErr, ok := err.(ExitCoder); ok {
os.Exit(exitCode) os.Exit(exitErr.ExitCode())
panic("unreachable")
} }
if err != nil {
os.Exit(DefaultErrorExitCode)
panic("unreachable")
}
os.Exit(DefaultSuccessExitCode)
} }
// Invokes the subcommand given the context, parses ctx.Args() to generate command-specific flags // Invokes the subcommand given the context, parses ctx.Args() to generate command-specific flags
func (a *App) RunAsSubcommand(ctx *Context) (ec int, err error) { func (a *App) RunAsSubcommand(ctx *Context) (err error) {
// append help to commands // append help to commands
if len(a.Commands) > 0 { if len(a.Commands) > 0 {
if a.Command(helpCommand.Name) == nil && !a.HideHelp { if a.Command(helpCommand.Name) == nil && !a.HideHelp {
@ -262,55 +285,63 @@ func (a *App) RunAsSubcommand(ctx *Context) (ec int, err error) {
} else { } else {
ShowCommandHelp(ctx, context.Args().First()) ShowCommandHelp(ctx, context.Args().First())
} }
return DefaultErrorExitCode, nerr return nerr
} }
if checkCompletions(context) { if checkCompletions(context) {
return DefaultSuccessExitCode, nil return nil
} }
if err != nil { if err != nil {
if a.OnUsageError != nil { if a.OnUsageError != nil {
err = a.OnUsageError(context, err, true) err = a.OnUsageError(context, err, true)
if err != nil { if exitErr, ok := err.(ExitCoder); ok {
return DefaultErrorExitCode, err os.Exit(exitErr.ExitCode())
panic("unreachable")
} }
return DefaultSuccessExitCode, err return nil
} else { } else {
fmt.Fprintf(a.Writer, "%s\n\n", "Incorrect Usage.") fmt.Fprintf(a.Writer, "%s\n\n", "Incorrect Usage.")
ShowSubcommandHelp(context) ShowSubcommandHelp(context)
return DefaultErrorExitCode, err return err
} }
} }
if len(a.Commands) > 0 { if len(a.Commands) > 0 {
if checkSubcommandHelp(context) { if checkSubcommandHelp(context) {
return DefaultSuccessExitCode, nil return nil
} }
} else { } else {
if checkCommandHelp(ctx, context.Args().First()) { if checkCommandHelp(ctx, context.Args().First()) {
return DefaultSuccessExitCode, nil return nil
} }
} }
if a.After != nil { if a.After != nil {
defer func() { defer func() {
afterEc, afterErr := a.After(context) afterErr := a.After(context)
if afterErr != nil { if afterErr != nil {
if exitErr, ok := afterErr.(ExitCoder); ok {
os.Exit(exitErr.ExitCode())
panic("unreachable")
}
if err != nil { if err != nil {
err = NewMultiError(err, afterErr) err = NewMultiError(err, afterErr)
} else { } else {
err = afterErr err = afterErr
} }
} }
ec = afterEc
}() }()
} }
if a.Before != nil { if a.Before != nil {
ec, err = a.Before(context) err = a.Before(context)
if err != nil { if err != nil {
return ec, err if exitErr, ok := err.(ExitCoder); ok {
os.Exit(exitErr.ExitCode())
panic("unreachable")
}
return err
} }
} }
@ -324,7 +355,7 @@ func (a *App) RunAsSubcommand(ctx *Context) (ec int, err error) {
} }
// Run default Action // Run default Action
return a.Action(context), nil return a.Action(context)
} }
// Returns the named command on App. Returns nil if the command does not exist // Returns the named command on App. Returns nil if the command does not exist

@ -22,9 +22,9 @@ func ExampleApp_Run() {
app.Flags = []Flag{ app.Flags = []Flag{
StringFlag{Name: "name", Value: "bob", Usage: "a name to say"}, StringFlag{Name: "name", Value: "bob", Usage: "a name to say"},
} }
app.Action = func(c *Context) int { app.Action = func(c *Context) error {
fmt.Printf("Hello %v\n", c.String("name")) fmt.Printf("Hello %v\n", c.String("name"))
return 0 return nil
} }
app.UsageText = "app [first_arg] [second_arg]" app.UsageText = "app [first_arg] [second_arg]"
app.Author = "Harrison" app.Author = "Harrison"
@ -59,9 +59,9 @@ func ExampleApp_Run_subcommand() {
Usage: "Name of the person to greet", Usage: "Name of the person to greet",
}, },
}, },
Action: func(c *Context) int { Action: func(c *Context) error {
fmt.Println("Hello,", c.String("name")) fmt.Println("Hello,", c.String("name"))
return 0 return nil
}, },
}, },
}, },
@ -88,9 +88,9 @@ func ExampleApp_Run_help() {
Aliases: []string{"d"}, Aliases: []string{"d"},
Usage: "use it to see a description", Usage: "use it to see a description",
Description: "This is how we describe describeit the function", Description: "This is how we describe describeit the function",
Action: func(c *Context) int { Action: func(c *Context) error {
fmt.Printf("i like to describe things") fmt.Printf("i like to describe things")
return 0 return nil
}, },
}, },
} }
@ -119,17 +119,17 @@ func ExampleApp_Run_bashComplete() {
Aliases: []string{"d"}, Aliases: []string{"d"},
Usage: "use it to see a description", Usage: "use it to see a description",
Description: "This is how we describe describeit the function", Description: "This is how we describe describeit the function",
Action: func(c *Context) int { Action: func(c *Context) error {
fmt.Printf("i like to describe things") fmt.Printf("i like to describe things")
return 0 return nil
}, },
}, { }, {
Name: "next", Name: "next",
Usage: "next example", Usage: "next example",
Description: "more stuff to see when generating bash completion", Description: "more stuff to see when generating bash completion",
Action: func(c *Context) int { Action: func(c *Context) error {
fmt.Printf("the next example") fmt.Printf("the next example")
return 0 return nil
}, },
}, },
} }
@ -147,17 +147,15 @@ func TestApp_Run(t *testing.T) {
s := "" s := ""
app := NewApp() app := NewApp()
app.Action = func(c *Context) int { app.Action = func(c *Context) error {
s = s + c.Args().First() s = s + c.Args().First()
return 0 return nil
} }
ec, err := app.Run([]string{"command", "foo"}) err := app.Run([]string{"command", "foo"})
expect(t, err, nil) expect(t, err, nil)
expect(t, ec, 0) err = app.Run([]string{"command", "bar"})
ec, err = app.Run([]string{"command", "bar"})
expect(t, err, nil) expect(t, err, nil)
expect(t, ec, 0)
expect(t, s, "foobar") expect(t, s, "foobar")
} }
@ -196,10 +194,10 @@ func TestApp_CommandWithArgBeforeFlags(t *testing.T) {
Flags: []Flag{ Flags: []Flag{
StringFlag{Name: "option", Value: "", Usage: "some option"}, StringFlag{Name: "option", Value: "", Usage: "some option"},
}, },
Action: func(c *Context) int { Action: func(c *Context) error {
parsedOption = c.String("option") parsedOption = c.String("option")
firstArg = c.Args().First() firstArg = c.Args().First()
return 0 return nil
}, },
} }
app.Commands = []Command{command} app.Commands = []Command{command}
@ -217,9 +215,9 @@ func TestApp_RunAsSubcommandParseFlags(t *testing.T) {
a.Commands = []Command{ a.Commands = []Command{
{ {
Name: "foo", Name: "foo",
Action: func(c *Context) int { Action: func(c *Context) error {
context = c context = c
return 0 return nil
}, },
Flags: []Flag{ Flags: []Flag{
StringFlag{ StringFlag{
@ -228,7 +226,7 @@ func TestApp_RunAsSubcommandParseFlags(t *testing.T) {
Usage: "language for the greeting", Usage: "language for the greeting",
}, },
}, },
Before: func(_ *Context) (int, error) { return 0, nil }, Before: func(_ *Context) error { return nil },
}, },
} }
a.Run([]string{"", "foo", "--lang", "spanish", "abcd"}) a.Run([]string{"", "foo", "--lang", "spanish", "abcd"})
@ -247,10 +245,10 @@ func TestApp_CommandWithFlagBeforeTerminator(t *testing.T) {
Flags: []Flag{ Flags: []Flag{
StringFlag{Name: "option", Value: "", Usage: "some option"}, StringFlag{Name: "option", Value: "", Usage: "some option"},
}, },
Action: func(c *Context) int { Action: func(c *Context) error {
parsedOption = c.String("option") parsedOption = c.String("option")
args = c.Args() args = c.Args()
return 0 return nil
}, },
} }
app.Commands = []Command{command} app.Commands = []Command{command}
@ -269,9 +267,9 @@ func TestApp_CommandWithDash(t *testing.T) {
app := NewApp() app := NewApp()
command := Command{ command := Command{
Name: "cmd", Name: "cmd",
Action: func(c *Context) int { Action: func(c *Context) error {
args = c.Args() args = c.Args()
return 0 return nil
}, },
} }
app.Commands = []Command{command} app.Commands = []Command{command}
@ -288,9 +286,9 @@ func TestApp_CommandWithNoFlagBeforeTerminator(t *testing.T) {
app := NewApp() app := NewApp()
command := Command{ command := Command{
Name: "cmd", Name: "cmd",
Action: func(c *Context) int { Action: func(c *Context) error {
args = c.Args() args = c.Args()
return 0 return nil
}, },
} }
app.Commands = []Command{command} app.Commands = []Command{command}
@ -309,9 +307,9 @@ func TestApp_Float64Flag(t *testing.T) {
app.Flags = []Flag{ app.Flags = []Flag{
Float64Flag{Name: "height", Value: 1.5, Usage: "Set the height, in meters"}, Float64Flag{Name: "height", Value: 1.5, Usage: "Set the height, in meters"},
} }
app.Action = func(c *Context) int { app.Action = func(c *Context) error {
meters = c.Float64("height") meters = c.Float64("height")
return 0 return nil
} }
app.Run([]string{"", "--height", "1.93"}) app.Run([]string{"", "--height", "1.93"})
@ -330,12 +328,12 @@ func TestApp_ParseSliceFlags(t *testing.T) {
IntSliceFlag{Name: "p", Value: &IntSlice{}, Usage: "set one or more ip addr"}, IntSliceFlag{Name: "p", Value: &IntSlice{}, Usage: "set one or more ip addr"},
StringSliceFlag{Name: "ip", Value: &StringSlice{}, Usage: "set one or more ports to open"}, StringSliceFlag{Name: "ip", Value: &StringSlice{}, Usage: "set one or more ports to open"},
}, },
Action: func(c *Context) int { Action: func(c *Context) error {
parsedIntSlice = c.IntSlice("p") parsedIntSlice = c.IntSlice("p")
parsedStringSlice = c.StringSlice("ip") parsedStringSlice = c.StringSlice("ip")
parsedOption = c.String("option") parsedOption = c.String("option")
firstArg = c.Args().First() firstArg = c.Args().First()
return 0 return nil
}, },
} }
app.Commands = []Command{command} app.Commands = []Command{command}
@ -388,10 +386,10 @@ func TestApp_ParseSliceFlagsWithMissingValue(t *testing.T) {
IntSliceFlag{Name: "a", Usage: "set numbers"}, IntSliceFlag{Name: "a", Usage: "set numbers"},
StringSliceFlag{Name: "str", Usage: "set strings"}, StringSliceFlag{Name: "str", Usage: "set strings"},
}, },
Action: func(c *Context) int { Action: func(c *Context) error {
parsedIntSlice = c.IntSlice("a") parsedIntSlice = c.IntSlice("a")
parsedStringSlice = c.StringSlice("str") parsedStringSlice = c.StringSlice("str")
return 0 return nil
}, },
} }
app.Commands = []Command{command} app.Commands = []Command{command}
@ -443,7 +441,7 @@ func TestApp_SetStdout(t *testing.T) {
app.Name = "test" app.Name = "test"
app.Writer = w app.Writer = w
_, err := app.Run([]string{"help"}) err := app.Run([]string{"help"})
if err != nil { if err != nil {
t.Fatalf("Run error: %s", err) t.Fatalf("Run error: %s", err)
@ -461,22 +459,22 @@ func TestApp_BeforeFunc(t *testing.T) {
app := NewApp() app := NewApp()
app.Before = func(c *Context) (int, error) { app.Before = func(c *Context) error {
beforeRun = true beforeRun = true
s := c.String("opt") s := c.String("opt")
if s == "fail" { if s == "fail" {
return DefaultErrorExitCode, beforeError return beforeError
} }
return DefaultSuccessExitCode, nil return nil
} }
app.Commands = []Command{ app.Commands = []Command{
Command{ Command{
Name: "sub", Name: "sub",
Action: func(c *Context) int { Action: func(c *Context) error {
subcommandRun = true subcommandRun = true
return DefaultSuccessExitCode return nil
}, },
}, },
} }
@ -486,7 +484,7 @@ func TestApp_BeforeFunc(t *testing.T) {
} }
// run with the Before() func succeeding // run with the Before() func succeeding
ec, err := app.Run([]string{"command", "--opt", "succeed", "sub"}) err = app.Run([]string{"command", "--opt", "succeed", "sub"})
if err != nil { if err != nil {
t.Fatalf("Run error: %s", err) t.Fatalf("Run error: %s", err)
@ -500,15 +498,11 @@ func TestApp_BeforeFunc(t *testing.T) {
t.Errorf("Subcommand not executed when expected") t.Errorf("Subcommand not executed when expected")
} }
if ec != DefaultSuccessExitCode {
t.Errorf("Expected exit code to be %d but got %d", DefaultSuccessExitCode, ec)
}
// reset // reset
beforeRun, subcommandRun = false, false beforeRun, subcommandRun = false, false
// run with the Before() func failing // run with the Before() func failing
ec, err = app.Run([]string{"command", "--opt", "fail", "sub"}) err = app.Run([]string{"command", "--opt", "fail", "sub"})
// should be the same error produced by the Before func // should be the same error produced by the Before func
if err != beforeError { if err != beforeError {
@ -522,10 +516,6 @@ func TestApp_BeforeFunc(t *testing.T) {
if subcommandRun == true { if subcommandRun == true {
t.Errorf("Subcommand executed when NOT expected") t.Errorf("Subcommand executed when NOT expected")
} }
if ec != DefaultErrorExitCode {
t.Errorf("Expected exit code to be %d but got %d", DefaultErrorExitCode, ec)
}
} }
func TestApp_AfterFunc(t *testing.T) { func TestApp_AfterFunc(t *testing.T) {
@ -535,22 +525,22 @@ func TestApp_AfterFunc(t *testing.T) {
app := NewApp() app := NewApp()
app.After = func(c *Context) (int, error) { app.After = func(c *Context) error {
afterRun = true afterRun = true
s := c.String("opt") s := c.String("opt")
if s == "fail" { if s == "fail" {
return DefaultErrorExitCode, afterError return afterError
} }
return DefaultSuccessExitCode, nil return nil
} }
app.Commands = []Command{ app.Commands = []Command{
Command{ Command{
Name: "sub", Name: "sub",
Action: func(c *Context) int { Action: func(c *Context) error {
subcommandRun = true subcommandRun = true
return DefaultSuccessExitCode return nil
}, },
}, },
} }
@ -560,7 +550,7 @@ func TestApp_AfterFunc(t *testing.T) {
} }
// run with the After() func succeeding // run with the After() func succeeding
ec, err := app.Run([]string{"command", "--opt", "succeed", "sub"}) err = app.Run([]string{"command", "--opt", "succeed", "sub"})
if err != nil { if err != nil {
t.Fatalf("Run error: %s", err) t.Fatalf("Run error: %s", err)
@ -574,15 +564,11 @@ func TestApp_AfterFunc(t *testing.T) {
t.Errorf("Subcommand not executed when expected") t.Errorf("Subcommand not executed when expected")
} }
if ec != DefaultSuccessExitCode {
t.Errorf("Expected exit code to be %d but got %d", DefaultSuccessExitCode, ec)
}
// reset // reset
afterRun, subcommandRun = false, false afterRun, subcommandRun = false, false
// run with the Before() func failing // run with the Before() func failing
ec, err = app.Run([]string{"command", "--opt", "fail", "sub"}) err = app.Run([]string{"command", "--opt", "fail", "sub"})
// should be the same error produced by the Before func // should be the same error produced by the Before func
if err != afterError { if err != afterError {
@ -596,10 +582,6 @@ func TestApp_AfterFunc(t *testing.T) {
if subcommandRun == false { if subcommandRun == false {
t.Errorf("Subcommand not executed when expected") t.Errorf("Subcommand not executed when expected")
} }
if ec != DefaultErrorExitCode {
t.Errorf("Expected exit code to be %d but got %d", DefaultErrorExitCode, ec)
}
} }
func TestAppNoHelpFlag(t *testing.T) { func TestAppNoHelpFlag(t *testing.T) {
@ -612,7 +594,7 @@ func TestAppNoHelpFlag(t *testing.T) {
app := NewApp() app := NewApp()
app.Writer = ioutil.Discard app.Writer = ioutil.Discard
_, err := app.Run([]string{"test", "-h"}) err := app.Run([]string{"test", "-h"})
if err != flag.ErrHelp { if err != flag.ErrHelp {
t.Errorf("expected error about missing help flag, but got: %s (%T)", err, err) t.Errorf("expected error about missing help flag, but got: %s (%T)", err, err)
@ -669,9 +651,9 @@ func TestAppCommandNotFound(t *testing.T) {
app.Commands = []Command{ app.Commands = []Command{
Command{ Command{
Name: "bar", Name: "bar",
Action: func(c *Context) int { Action: func(c *Context) error {
subcommandRun = true subcommandRun = true
return 0 return nil
}, },
}, },
} }
@ -689,10 +671,10 @@ func TestGlobalFlag(t *testing.T) {
app.Flags = []Flag{ app.Flags = []Flag{
StringFlag{Name: "global, g", Usage: "global"}, StringFlag{Name: "global, g", Usage: "global"},
} }
app.Action = func(c *Context) int { app.Action = func(c *Context) error {
globalFlag = c.GlobalString("global") globalFlag = c.GlobalString("global")
globalFlagSet = c.GlobalIsSet("global") globalFlagSet = c.GlobalIsSet("global")
return 0 return nil
} }
app.Run([]string{"command", "-g", "foo"}) app.Run([]string{"command", "-g", "foo"})
expect(t, globalFlag, "foo") expect(t, globalFlag, "foo")
@ -718,14 +700,14 @@ func TestGlobalFlagsInSubcommands(t *testing.T) {
Subcommands: []Command{ Subcommands: []Command{
{ {
Name: "bar", Name: "bar",
Action: func(c *Context) int { Action: func(c *Context) error {
if c.GlobalBool("debug") { if c.GlobalBool("debug") {
subcommandRun = true subcommandRun = true
} }
if c.GlobalBool("parent") { if c.GlobalBool("parent") {
parentFlag = true parentFlag = true
} }
return 0 return nil
}, },
}, },
}, },
@ -767,7 +749,7 @@ func TestApp_Run_CommandWithSubcommandHasHelpTopic(t *testing.T) {
} }
app.Commands = []Command{cmd} app.Commands = []Command{cmd}
_, err := app.Run(flagSet) err := app.Run(flagSet)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
@ -808,7 +790,7 @@ func TestApp_Run_SubcommandFullPath(t *testing.T) {
} }
app.Commands = []Command{cmd} app.Commands = []Command{cmd}
_, err := app.Run([]string{"command", "foo", "bar", "--help"}) err := app.Run([]string{"command", "foo", "bar", "--help"})
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@ -839,7 +821,7 @@ func TestApp_Run_SubcommandHelpName(t *testing.T) {
} }
app.Commands = []Command{cmd} app.Commands = []Command{cmd}
_, err := app.Run([]string{"command", "foo", "bar", "--help"}) err := app.Run([]string{"command", "foo", "bar", "--help"})
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@ -870,7 +852,7 @@ func TestApp_Run_CommandHelpName(t *testing.T) {
} }
app.Commands = []Command{cmd} app.Commands = []Command{cmd}
_, err := app.Run([]string{"command", "foo", "bar", "--help"}) err := app.Run([]string{"command", "foo", "bar", "--help"})
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@ -901,7 +883,7 @@ func TestApp_Run_CommandSubcommandHelpName(t *testing.T) {
} }
app.Commands = []Command{cmd} app.Commands = []Command{cmd}
_, err := app.Run([]string{"command", "foo", "--help"}) err := app.Run([]string{"command", "foo", "--help"})
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@ -927,12 +909,12 @@ func TestApp_Run_Help(t *testing.T) {
app.Name = "boom" app.Name = "boom"
app.Usage = "make an explosive entrance" app.Usage = "make an explosive entrance"
app.Writer = buf app.Writer = buf
app.Action = func(c *Context) int { app.Action = func(c *Context) error {
buf.WriteString("boom I say!") buf.WriteString("boom I say!")
return 0 return nil
} }
_, err := app.Run(args) err := app.Run(args)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@ -959,12 +941,12 @@ func TestApp_Run_Version(t *testing.T) {
app.Usage = "make an explosive entrance" app.Usage = "make an explosive entrance"
app.Version = "0.1.0" app.Version = "0.1.0"
app.Writer = buf app.Writer = buf
app.Action = func(c *Context) int { app.Action = func(c *Context) error {
buf.WriteString("boom I say!") buf.WriteString("boom I say!")
return 0 return nil
} }
_, err := app.Run(args) err := app.Run(args)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@ -1029,11 +1011,11 @@ func TestApp_Run_Categories(t *testing.T) {
func TestApp_Run_DoesNotOverwriteErrorFromBefore(t *testing.T) { func TestApp_Run_DoesNotOverwriteErrorFromBefore(t *testing.T) {
app := NewApp() app := NewApp()
app.Action = func(c *Context) int { return 0 } app.Action = func(c *Context) error { return nil }
app.Before = func(c *Context) (int, error) { return 1, fmt.Errorf("before error") } app.Before = func(c *Context) error { return fmt.Errorf("before error") }
app.After = func(c *Context) (int, error) { return 2, fmt.Errorf("after error") } app.After = func(c *Context) error { return fmt.Errorf("after error") }
ec, err := app.Run([]string{"foo"}) err := app.Run([]string{"foo"})
if err == nil { if err == nil {
t.Fatalf("expected to receive error from Run, got none") t.Fatalf("expected to receive error from Run, got none")
} }
@ -1044,10 +1026,6 @@ func TestApp_Run_DoesNotOverwriteErrorFromBefore(t *testing.T) {
if !strings.Contains(err.Error(), "after error") { if !strings.Contains(err.Error(), "after error") {
t.Errorf("expected text of error from After method, but got none in \"%v\"", err) t.Errorf("expected text of error from After method, but got none in \"%v\"", err)
} }
if ec != 2 {
t.Errorf("Expected exit code to be %d but got %d", 2, ec)
}
} }
func TestApp_Run_SubcommandDoesNotOverwriteErrorFromBefore(t *testing.T) { func TestApp_Run_SubcommandDoesNotOverwriteErrorFromBefore(t *testing.T) {
@ -1060,12 +1038,12 @@ func TestApp_Run_SubcommandDoesNotOverwriteErrorFromBefore(t *testing.T) {
}, },
}, },
Name: "bar", Name: "bar",
Before: func(c *Context) (int, error) { return 1, fmt.Errorf("before error") }, Before: func(c *Context) error { return fmt.Errorf("before error") },
After: func(c *Context) (int, error) { return 2, fmt.Errorf("after error") }, After: func(c *Context) error { return fmt.Errorf("after error") },
}, },
} }
ec, err := app.Run([]string{"foo", "bar"}) err := app.Run([]string{"foo", "bar"})
if err == nil { if err == nil {
t.Fatalf("expected to receive error from Run, got none") t.Fatalf("expected to receive error from Run, got none")
} }
@ -1076,10 +1054,6 @@ func TestApp_Run_SubcommandDoesNotOverwriteErrorFromBefore(t *testing.T) {
if !strings.Contains(err.Error(), "after error") { if !strings.Contains(err.Error(), "after error") {
t.Errorf("expected text of error from After method, but got none in \"%v\"", err) t.Errorf("expected text of error from After method, but got none in \"%v\"", err)
} }
if ec != 2 {
t.Errorf("Expected exit code to be %d but got %d", 2, ec)
}
} }
func TestApp_OnUsageError_WithWrongFlagValue(t *testing.T) { func TestApp_OnUsageError_WithWrongFlagValue(t *testing.T) {
@ -1102,7 +1076,7 @@ func TestApp_OnUsageError_WithWrongFlagValue(t *testing.T) {
}, },
} }
_, err := app.Run([]string{"foo", "--flag=wrong"}) err := app.Run([]string{"foo", "--flag=wrong"})
if err == nil { if err == nil {
t.Fatalf("expected to receive error from Run, got none") t.Fatalf("expected to receive error from Run, got none")
} }
@ -1132,7 +1106,7 @@ func TestApp_OnUsageError_WithWrongFlagValue_ForSubcommand(t *testing.T) {
}, },
} }
_, err := app.Run([]string{"foo", "--flag=wrong", "bar"}) err := app.Run([]string{"foo", "--flag=wrong", "bar"})
if err == nil { if err == nil {
t.Fatalf("expected to receive error from Run, got none") t.Fatalf("expected to receive error from Run, got none")
} }

@ -19,6 +19,7 @@
package cli package cli
import ( import (
"fmt"
"strings" "strings"
) )
@ -38,3 +39,31 @@ func (m MultiError) Error() string {
return strings.Join(errs, "\n") return strings.Join(errs, "\n")
} }
type ExitCoder interface {
ExitCode() int
}
type ExitError struct {
exitCode int
message string
}
func NewExitError(message string, exitCode int) *ExitError {
return &ExitError{
exitCode: exitCode,
message: message,
}
}
func (ee *ExitError) Error() string {
return ee.message
}
func (ee *ExitError) String() string {
return fmt.Sprintf("%s exitcode=%v", ee.message, ee.exitCode)
}
func (ee *ExitError) ExitCode() int {
return ee.exitCode
}

@ -3,6 +3,7 @@ package cli
import ( import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os"
"sort" "sort"
"strings" "strings"
) )
@ -63,7 +64,7 @@ func (c Command) FullName() string {
type Commands []Command type Commands []Command
// Invokes the command given the context, parses ctx.Args() to generate command-specific flags // Invokes the command given the context, parses ctx.Args() to generate command-specific flags
func (c Command) Run(ctx *Context) (ec int, err error) { func (c Command) Run(ctx *Context) (err error) {
if len(c.Subcommands) > 0 { if len(c.Subcommands) > 0 {
return c.startApp(ctx) return c.startApp(ctx)
} }
@ -124,15 +125,16 @@ func (c Command) Run(ctx *Context) (ec int, err error) {
if err != nil { if err != nil {
if c.OnUsageError != nil { if c.OnUsageError != nil {
err := c.OnUsageError(ctx, err, false) err := c.OnUsageError(ctx, err, false)
if err != nil { if exitErr, ok := err.(ExitCoder); ok {
return DefaultErrorExitCode, err os.Exit(exitErr.ExitCode())
panic("unreachable")
} }
return DefaultSuccessExitCode, err return err
} else { } else {
fmt.Fprintln(ctx.App.Writer, "Incorrect Usage.") fmt.Fprintln(ctx.App.Writer, "Incorrect Usage.")
fmt.Fprintln(ctx.App.Writer) fmt.Fprintln(ctx.App.Writer)
ShowCommandHelp(ctx, c.Name) ShowCommandHelp(ctx, c.Name)
return DefaultErrorExitCode, err return err
} }
} }
@ -141,47 +143,59 @@ func (c Command) Run(ctx *Context) (ec int, err error) {
fmt.Fprintln(ctx.App.Writer, nerr) fmt.Fprintln(ctx.App.Writer, nerr)
fmt.Fprintln(ctx.App.Writer) fmt.Fprintln(ctx.App.Writer)
ShowCommandHelp(ctx, c.Name) ShowCommandHelp(ctx, c.Name)
return DefaultErrorExitCode, nerr return nerr
} }
context := NewContext(ctx.App, set, ctx) context := NewContext(ctx.App, set, ctx)
if checkCommandCompletions(context, c.Name) { if checkCommandCompletions(context, c.Name) {
return DefaultSuccessExitCode, nil return nil
} }
if checkCommandHelp(context, c.Name) { if checkCommandHelp(context, c.Name) {
return DefaultSuccessExitCode, nil return nil
} }
if c.After != nil { if c.After != nil {
defer func() { defer func() {
afterEc, afterErr := c.After(context) afterErr := c.After(context)
if afterErr != nil { if afterErr != nil {
if exitErr, ok := afterErr.(ExitCoder); ok {
os.Exit(exitErr.ExitCode())
panic("unreachable")
}
if err != nil { if err != nil {
err = NewMultiError(err, afterErr) err = NewMultiError(err, afterErr)
} else { } else {
err = afterErr err = afterErr
} }
ec = afterEc
} }
}() }()
} }
if c.Before != nil { if c.Before != nil {
ec, err = c.Before(context) err = c.Before(context)
if err != nil { if err != nil {
fmt.Fprintln(ctx.App.Writer, err) fmt.Fprintln(ctx.App.Writer, err)
fmt.Fprintln(ctx.App.Writer) fmt.Fprintln(ctx.App.Writer)
ShowCommandHelp(ctx, c.Name) ShowCommandHelp(ctx, c.Name)
return ec, err if exitErr, ok := err.(ExitCoder); ok {
os.Exit(exitErr.ExitCode())
panic("unreachable")
}
return err
} }
} }
context.Command = c context.Command = c
ec = c.Action(context) err = c.Action(context)
return ec, err if err != nil {
if exitErr, ok := err.(ExitCoder); ok {
os.Exit(exitErr.ExitCode())
panic("unreachable")
}
}
return err
} }
func (c Command) Names() []string { func (c Command) Names() []string {
@ -204,7 +218,7 @@ func (c Command) HasName(name string) bool {
return false return false
} }
func (c Command) startApp(ctx *Context) (int, error) { func (c Command) startApp(ctx *Context) error {
app := NewApp() app := NewApp()
// set the name and usage // set the name and usage

@ -34,12 +34,12 @@ func TestCommandFlagParsing(t *testing.T) {
Aliases: []string{"tc"}, Aliases: []string{"tc"},
Usage: "this is for testing", Usage: "this is for testing",
Description: "testing", Description: "testing",
Action: func(_ *Context) int { return 0 }, Action: func(_ *Context) error { return nil },
} }
command.SkipFlagParsing = c.skipFlagParsing command.SkipFlagParsing = c.skipFlagParsing
_, err := command.Run(context) err := command.Run(context)
expect(t, err, c.expectedErr) expect(t, err, c.expectedErr)
expect(t, []string(context.Args()), c.testArgs) expect(t, []string(context.Args()), c.testArgs)
@ -51,16 +51,16 @@ func TestCommand_Run_DoesNotOverwriteErrorFromBefore(t *testing.T) {
app.Commands = []Command{ app.Commands = []Command{
Command{ Command{
Name: "bar", Name: "bar",
Before: func(c *Context) (int, error) { Before: func(c *Context) error {
return 1, fmt.Errorf("before error") return fmt.Errorf("before error")
}, },
After: func(c *Context) (int, error) { After: func(c *Context) error {
return 1, fmt.Errorf("after error") return fmt.Errorf("after error")
}, },
}, },
} }
_, err := app.Run([]string{"foo", "bar"}) err := app.Run([]string{"foo", "bar"})
if err == nil { if err == nil {
t.Fatalf("expected to receive error from Run, got none") t.Fatalf("expected to receive error from Run, got none")
} }
@ -90,7 +90,7 @@ func TestCommand_OnUsageError_WithWrongFlagValue(t *testing.T) {
}, },
} }
_, err := app.Run([]string{"foo", "bar", "--flag=wrong"}) err := app.Run([]string{"foo", "bar", "--flag=wrong"})
if err == nil { if err == nil {
t.Fatalf("expected to receive error from Run, got none") t.Fatalf("expected to receive error from Run, got none")
} }

@ -323,14 +323,14 @@ func TestParseMultiString(t *testing.T) {
Flags: []Flag{ Flags: []Flag{
StringFlag{Name: "serve, s"}, StringFlag{Name: "serve, s"},
}, },
Action: func(ctx *Context) int { Action: func(ctx *Context) error {
if ctx.String("serve") != "10" { if ctx.String("serve") != "10" {
t.Errorf("main name not set") t.Errorf("main name not set")
} }
if ctx.String("s") != "10" { if ctx.String("s") != "10" {
t.Errorf("short name not set") t.Errorf("short name not set")
} }
return 0 return nil
}, },
}).Run([]string{"run", "-s", "10"}) }).Run([]string{"run", "-s", "10"})
} }
@ -344,11 +344,11 @@ func TestParseDestinationString(t *testing.T) {
Destination: &dest, Destination: &dest,
}, },
}, },
Action: func(ctx *Context) int { Action: func(ctx *Context) error {
if dest != "10" { if dest != "10" {
t.Errorf("expected destination String 10") t.Errorf("expected destination String 10")
} }
return 0 return nil
}, },
} }
a.Run([]string{"run", "--dest", "10"}) a.Run([]string{"run", "--dest", "10"})
@ -361,14 +361,14 @@ func TestParseMultiStringFromEnv(t *testing.T) {
Flags: []Flag{ Flags: []Flag{
StringFlag{Name: "count, c", EnvVar: "APP_COUNT"}, StringFlag{Name: "count, c", EnvVar: "APP_COUNT"},
}, },
Action: func(ctx *Context) int { Action: func(ctx *Context) error {
if ctx.String("count") != "20" { if ctx.String("count") != "20" {
t.Errorf("main name not set") t.Errorf("main name not set")
} }
if ctx.String("c") != "20" { if ctx.String("c") != "20" {
t.Errorf("short name not set") t.Errorf("short name not set")
} }
return 0 return nil
}, },
}).Run([]string{"run"}) }).Run([]string{"run"})
} }
@ -380,14 +380,14 @@ func TestParseMultiStringFromEnvCascade(t *testing.T) {
Flags: []Flag{ Flags: []Flag{
StringFlag{Name: "count, c", EnvVar: "COMPAT_COUNT,APP_COUNT"}, StringFlag{Name: "count, c", EnvVar: "COMPAT_COUNT,APP_COUNT"},
}, },
Action: func(ctx *Context) int { Action: func(ctx *Context) error {
if ctx.String("count") != "20" { if ctx.String("count") != "20" {
t.Errorf("main name not set") t.Errorf("main name not set")
} }
if ctx.String("c") != "20" { if ctx.String("c") != "20" {
t.Errorf("short name not set") t.Errorf("short name not set")
} }
return 0 return nil
}, },
}).Run([]string{"run"}) }).Run([]string{"run"})
} }
@ -397,14 +397,14 @@ func TestParseMultiStringSlice(t *testing.T) {
Flags: []Flag{ Flags: []Flag{
StringSliceFlag{Name: "serve, s", Value: &StringSlice{}}, StringSliceFlag{Name: "serve, s", Value: &StringSlice{}},
}, },
Action: func(ctx *Context) int { Action: func(ctx *Context) error {
if !reflect.DeepEqual(ctx.StringSlice("serve"), []string{"10", "20"}) { if !reflect.DeepEqual(ctx.StringSlice("serve"), []string{"10", "20"}) {
t.Errorf("main name not set") t.Errorf("main name not set")
} }
if !reflect.DeepEqual(ctx.StringSlice("s"), []string{"10", "20"}) { if !reflect.DeepEqual(ctx.StringSlice("s"), []string{"10", "20"}) {
t.Errorf("short name not set") t.Errorf("short name not set")
} }
return 0 return nil
}, },
}).Run([]string{"run", "-s", "10", "-s", "20"}) }).Run([]string{"run", "-s", "10", "-s", "20"})
} }
@ -417,14 +417,14 @@ func TestParseMultiStringSliceFromEnv(t *testing.T) {
Flags: []Flag{ Flags: []Flag{
StringSliceFlag{Name: "intervals, i", Value: &StringSlice{}, EnvVar: "APP_INTERVALS"}, StringSliceFlag{Name: "intervals, i", Value: &StringSlice{}, EnvVar: "APP_INTERVALS"},
}, },
Action: func(ctx *Context) int { Action: func(ctx *Context) error {
if !reflect.DeepEqual(ctx.StringSlice("intervals"), []string{"20", "30", "40"}) { if !reflect.DeepEqual(ctx.StringSlice("intervals"), []string{"20", "30", "40"}) {
t.Errorf("main name not set from env") t.Errorf("main name not set from env")
} }
if !reflect.DeepEqual(ctx.StringSlice("i"), []string{"20", "30", "40"}) { if !reflect.DeepEqual(ctx.StringSlice("i"), []string{"20", "30", "40"}) {
t.Errorf("short name not set from env") t.Errorf("short name not set from env")
} }
return 0 return nil
}, },
}).Run([]string{"run"}) }).Run([]string{"run"})
} }
@ -437,14 +437,14 @@ func TestParseMultiStringSliceFromEnvCascade(t *testing.T) {
Flags: []Flag{ Flags: []Flag{
StringSliceFlag{Name: "intervals, i", Value: &StringSlice{}, EnvVar: "COMPAT_INTERVALS,APP_INTERVALS"}, StringSliceFlag{Name: "intervals, i", Value: &StringSlice{}, EnvVar: "COMPAT_INTERVALS,APP_INTERVALS"},
}, },
Action: func(ctx *Context) int { Action: func(ctx *Context) error {
if !reflect.DeepEqual(ctx.StringSlice("intervals"), []string{"20", "30", "40"}) { if !reflect.DeepEqual(ctx.StringSlice("intervals"), []string{"20", "30", "40"}) {
t.Errorf("main name not set from env") t.Errorf("main name not set from env")
} }
if !reflect.DeepEqual(ctx.StringSlice("i"), []string{"20", "30", "40"}) { if !reflect.DeepEqual(ctx.StringSlice("i"), []string{"20", "30", "40"}) {
t.Errorf("short name not set from env") t.Errorf("short name not set from env")
} }
return 0 return nil
}, },
}).Run([]string{"run"}) }).Run([]string{"run"})
} }
@ -454,14 +454,14 @@ func TestParseMultiInt(t *testing.T) {
Flags: []Flag{ Flags: []Flag{
IntFlag{Name: "serve, s"}, IntFlag{Name: "serve, s"},
}, },
Action: func(ctx *Context) int { Action: func(ctx *Context) error {
if ctx.Int("serve") != 10 { if ctx.Int("serve") != 10 {
t.Errorf("main name not set") t.Errorf("main name not set")
} }
if ctx.Int("s") != 10 { if ctx.Int("s") != 10 {
t.Errorf("short name not set") t.Errorf("short name not set")
} }
return 0 return nil
}, },
} }
a.Run([]string{"run", "-s", "10"}) a.Run([]string{"run", "-s", "10"})
@ -476,11 +476,11 @@ func TestParseDestinationInt(t *testing.T) {
Destination: &dest, Destination: &dest,
}, },
}, },
Action: func(ctx *Context) int { Action: func(ctx *Context) error {
if dest != 10 { if dest != 10 {
t.Errorf("expected destination Int 10") t.Errorf("expected destination Int 10")
} }
return 0 return nil
}, },
} }
a.Run([]string{"run", "--dest", "10"}) a.Run([]string{"run", "--dest", "10"})
@ -493,14 +493,14 @@ func TestParseMultiIntFromEnv(t *testing.T) {
Flags: []Flag{ Flags: []Flag{
IntFlag{Name: "timeout, t", EnvVar: "APP_TIMEOUT_SECONDS"}, IntFlag{Name: "timeout, t", EnvVar: "APP_TIMEOUT_SECONDS"},
}, },
Action: func(ctx *Context) int { Action: func(ctx *Context) error {
if ctx.Int("timeout") != 10 { if ctx.Int("timeout") != 10 {
t.Errorf("main name not set") t.Errorf("main name not set")
} }
if ctx.Int("t") != 10 { if ctx.Int("t") != 10 {
t.Errorf("short name not set") t.Errorf("short name not set")
} }
return 0 return nil
}, },
} }
a.Run([]string{"run"}) a.Run([]string{"run"})
@ -513,14 +513,14 @@ func TestParseMultiIntFromEnvCascade(t *testing.T) {
Flags: []Flag{ Flags: []Flag{
IntFlag{Name: "timeout, t", EnvVar: "COMPAT_TIMEOUT_SECONDS,APP_TIMEOUT_SECONDS"}, IntFlag{Name: "timeout, t", EnvVar: "COMPAT_TIMEOUT_SECONDS,APP_TIMEOUT_SECONDS"},
}, },
Action: func(ctx *Context) int { Action: func(ctx *Context) error {
if ctx.Int("timeout") != 10 { if ctx.Int("timeout") != 10 {
t.Errorf("main name not set") t.Errorf("main name not set")
} }
if ctx.Int("t") != 10 { if ctx.Int("t") != 10 {
t.Errorf("short name not set") t.Errorf("short name not set")
} }
return 0 return nil
}, },
} }
a.Run([]string{"run"}) a.Run([]string{"run"})
@ -531,14 +531,14 @@ func TestParseMultiIntSlice(t *testing.T) {
Flags: []Flag{ Flags: []Flag{
IntSliceFlag{Name: "serve, s", Value: &IntSlice{}}, IntSliceFlag{Name: "serve, s", Value: &IntSlice{}},
}, },
Action: func(ctx *Context) int { Action: func(ctx *Context) error {
if !reflect.DeepEqual(ctx.IntSlice("serve"), []int{10, 20}) { if !reflect.DeepEqual(ctx.IntSlice("serve"), []int{10, 20}) {
t.Errorf("main name not set") t.Errorf("main name not set")
} }
if !reflect.DeepEqual(ctx.IntSlice("s"), []int{10, 20}) { if !reflect.DeepEqual(ctx.IntSlice("s"), []int{10, 20}) {
t.Errorf("short name not set") t.Errorf("short name not set")
} }
return 0 return nil
}, },
}).Run([]string{"run", "-s", "10", "-s", "20"}) }).Run([]string{"run", "-s", "10", "-s", "20"})
} }
@ -551,14 +551,14 @@ func TestParseMultiIntSliceFromEnv(t *testing.T) {
Flags: []Flag{ Flags: []Flag{
IntSliceFlag{Name: "intervals, i", Value: &IntSlice{}, EnvVar: "APP_INTERVALS"}, IntSliceFlag{Name: "intervals, i", Value: &IntSlice{}, EnvVar: "APP_INTERVALS"},
}, },
Action: func(ctx *Context) int { Action: func(ctx *Context) error {
if !reflect.DeepEqual(ctx.IntSlice("intervals"), []int{20, 30, 40}) { if !reflect.DeepEqual(ctx.IntSlice("intervals"), []int{20, 30, 40}) {
t.Errorf("main name not set from env") t.Errorf("main name not set from env")
} }
if !reflect.DeepEqual(ctx.IntSlice("i"), []int{20, 30, 40}) { if !reflect.DeepEqual(ctx.IntSlice("i"), []int{20, 30, 40}) {
t.Errorf("short name not set from env") t.Errorf("short name not set from env")
} }
return 0 return nil
}, },
}).Run([]string{"run"}) }).Run([]string{"run"})
} }
@ -571,14 +571,14 @@ func TestParseMultiIntSliceFromEnvCascade(t *testing.T) {
Flags: []Flag{ Flags: []Flag{
IntSliceFlag{Name: "intervals, i", Value: &IntSlice{}, EnvVar: "COMPAT_INTERVALS,APP_INTERVALS"}, IntSliceFlag{Name: "intervals, i", Value: &IntSlice{}, EnvVar: "COMPAT_INTERVALS,APP_INTERVALS"},
}, },
Action: func(ctx *Context) int { Action: func(ctx *Context) error {
if !reflect.DeepEqual(ctx.IntSlice("intervals"), []int{20, 30, 40}) { if !reflect.DeepEqual(ctx.IntSlice("intervals"), []int{20, 30, 40}) {
t.Errorf("main name not set from env") t.Errorf("main name not set from env")
} }
if !reflect.DeepEqual(ctx.IntSlice("i"), []int{20, 30, 40}) { if !reflect.DeepEqual(ctx.IntSlice("i"), []int{20, 30, 40}) {
t.Errorf("short name not set from env") t.Errorf("short name not set from env")
} }
return 0 return nil
}, },
}).Run([]string{"run"}) }).Run([]string{"run"})
} }
@ -588,14 +588,14 @@ func TestParseMultiFloat64(t *testing.T) {
Flags: []Flag{ Flags: []Flag{
Float64Flag{Name: "serve, s"}, Float64Flag{Name: "serve, s"},
}, },
Action: func(ctx *Context) int { Action: func(ctx *Context) error {
if ctx.Float64("serve") != 10.2 { if ctx.Float64("serve") != 10.2 {
t.Errorf("main name not set") t.Errorf("main name not set")
} }
if ctx.Float64("s") != 10.2 { if ctx.Float64("s") != 10.2 {
t.Errorf("short name not set") t.Errorf("short name not set")
} }
return 0 return nil
}, },
} }
a.Run([]string{"run", "-s", "10.2"}) a.Run([]string{"run", "-s", "10.2"})
@ -610,11 +610,11 @@ func TestParseDestinationFloat64(t *testing.T) {
Destination: &dest, Destination: &dest,
}, },
}, },
Action: func(ctx *Context) int { Action: func(ctx *Context) error {
if dest != 10.2 { if dest != 10.2 {
t.Errorf("expected destination Float64 10.2") t.Errorf("expected destination Float64 10.2")
} }
return 0 return nil
}, },
} }
a.Run([]string{"run", "--dest", "10.2"}) a.Run([]string{"run", "--dest", "10.2"})
@ -627,14 +627,14 @@ func TestParseMultiFloat64FromEnv(t *testing.T) {
Flags: []Flag{ Flags: []Flag{
Float64Flag{Name: "timeout, t", EnvVar: "APP_TIMEOUT_SECONDS"}, Float64Flag{Name: "timeout, t", EnvVar: "APP_TIMEOUT_SECONDS"},
}, },
Action: func(ctx *Context) int { Action: func(ctx *Context) error {
if ctx.Float64("timeout") != 15.5 { if ctx.Float64("timeout") != 15.5 {
t.Errorf("main name not set") t.Errorf("main name not set")
} }
if ctx.Float64("t") != 15.5 { if ctx.Float64("t") != 15.5 {
t.Errorf("short name not set") t.Errorf("short name not set")
} }
return 0 return nil
}, },
} }
a.Run([]string{"run"}) a.Run([]string{"run"})
@ -647,14 +647,14 @@ func TestParseMultiFloat64FromEnvCascade(t *testing.T) {
Flags: []Flag{ Flags: []Flag{
Float64Flag{Name: "timeout, t", EnvVar: "COMPAT_TIMEOUT_SECONDS,APP_TIMEOUT_SECONDS"}, Float64Flag{Name: "timeout, t", EnvVar: "COMPAT_TIMEOUT_SECONDS,APP_TIMEOUT_SECONDS"},
}, },
Action: func(ctx *Context) int { Action: func(ctx *Context) error {
if ctx.Float64("timeout") != 15.5 { if ctx.Float64("timeout") != 15.5 {
t.Errorf("main name not set") t.Errorf("main name not set")
} }
if ctx.Float64("t") != 15.5 { if ctx.Float64("t") != 15.5 {
t.Errorf("short name not set") t.Errorf("short name not set")
} }
return 0 return nil
}, },
} }
a.Run([]string{"run"}) a.Run([]string{"run"})
@ -665,14 +665,14 @@ func TestParseMultiBool(t *testing.T) {
Flags: []Flag{ Flags: []Flag{
BoolFlag{Name: "serve, s"}, BoolFlag{Name: "serve, s"},
}, },
Action: func(ctx *Context) int { Action: func(ctx *Context) error {
if ctx.Bool("serve") != true { if ctx.Bool("serve") != true {
t.Errorf("main name not set") t.Errorf("main name not set")
} }
if ctx.Bool("s") != true { if ctx.Bool("s") != true {
t.Errorf("short name not set") t.Errorf("short name not set")
} }
return 0 return nil
}, },
} }
a.Run([]string{"run", "--serve"}) a.Run([]string{"run", "--serve"})
@ -687,11 +687,11 @@ func TestParseDestinationBool(t *testing.T) {
Destination: &dest, Destination: &dest,
}, },
}, },
Action: func(ctx *Context) int { Action: func(ctx *Context) error {
if dest != true { if dest != true {
t.Errorf("expected destination Bool true") t.Errorf("expected destination Bool true")
} }
return 0 return nil
}, },
} }
a.Run([]string{"run", "--dest"}) a.Run([]string{"run", "--dest"})
@ -704,14 +704,14 @@ func TestParseMultiBoolFromEnv(t *testing.T) {
Flags: []Flag{ Flags: []Flag{
BoolFlag{Name: "debug, d", EnvVar: "APP_DEBUG"}, BoolFlag{Name: "debug, d", EnvVar: "APP_DEBUG"},
}, },
Action: func(ctx *Context) int { Action: func(ctx *Context) error {
if ctx.Bool("debug") != true { if ctx.Bool("debug") != true {
t.Errorf("main name not set from env") t.Errorf("main name not set from env")
} }
if ctx.Bool("d") != true { if ctx.Bool("d") != true {
t.Errorf("short name not set from env") t.Errorf("short name not set from env")
} }
return 0 return nil
}, },
} }
a.Run([]string{"run"}) a.Run([]string{"run"})
@ -724,14 +724,14 @@ func TestParseMultiBoolFromEnvCascade(t *testing.T) {
Flags: []Flag{ Flags: []Flag{
BoolFlag{Name: "debug, d", EnvVar: "COMPAT_DEBUG,APP_DEBUG"}, BoolFlag{Name: "debug, d", EnvVar: "COMPAT_DEBUG,APP_DEBUG"},
}, },
Action: func(ctx *Context) int { Action: func(ctx *Context) error {
if ctx.Bool("debug") != true { if ctx.Bool("debug") != true {
t.Errorf("main name not set from env") t.Errorf("main name not set from env")
} }
if ctx.Bool("d") != true { if ctx.Bool("d") != true {
t.Errorf("short name not set from env") t.Errorf("short name not set from env")
} }
return 0 return nil
}, },
} }
a.Run([]string{"run"}) a.Run([]string{"run"})
@ -742,14 +742,14 @@ func TestParseMultiBoolT(t *testing.T) {
Flags: []Flag{ Flags: []Flag{
BoolTFlag{Name: "serve, s"}, BoolTFlag{Name: "serve, s"},
}, },
Action: func(ctx *Context) int { Action: func(ctx *Context) error {
if ctx.BoolT("serve") != true { if ctx.BoolT("serve") != true {
t.Errorf("main name not set") t.Errorf("main name not set")
} }
if ctx.BoolT("s") != true { if ctx.BoolT("s") != true {
t.Errorf("short name not set") t.Errorf("short name not set")
} }
return 0 return nil
}, },
} }
a.Run([]string{"run", "--serve"}) a.Run([]string{"run", "--serve"})
@ -764,11 +764,11 @@ func TestParseDestinationBoolT(t *testing.T) {
Destination: &dest, Destination: &dest,
}, },
}, },
Action: func(ctx *Context) int { Action: func(ctx *Context) error {
if dest != true { if dest != true {
t.Errorf("expected destination BoolT true") t.Errorf("expected destination BoolT true")
} }
return 0 return nil
}, },
} }
a.Run([]string{"run", "--dest"}) a.Run([]string{"run", "--dest"})
@ -781,14 +781,14 @@ func TestParseMultiBoolTFromEnv(t *testing.T) {
Flags: []Flag{ Flags: []Flag{
BoolTFlag{Name: "debug, d", EnvVar: "APP_DEBUG"}, BoolTFlag{Name: "debug, d", EnvVar: "APP_DEBUG"},
}, },
Action: func(ctx *Context) int { Action: func(ctx *Context) error {
if ctx.BoolT("debug") != false { if ctx.BoolT("debug") != false {
t.Errorf("main name not set from env") t.Errorf("main name not set from env")
} }
if ctx.BoolT("d") != false { if ctx.BoolT("d") != false {
t.Errorf("short name not set from env") t.Errorf("short name not set from env")
} }
return 0 return nil
}, },
} }
a.Run([]string{"run"}) a.Run([]string{"run"})
@ -801,14 +801,14 @@ func TestParseMultiBoolTFromEnvCascade(t *testing.T) {
Flags: []Flag{ Flags: []Flag{
BoolTFlag{Name: "debug, d", EnvVar: "COMPAT_DEBUG,APP_DEBUG"}, BoolTFlag{Name: "debug, d", EnvVar: "COMPAT_DEBUG,APP_DEBUG"},
}, },
Action: func(ctx *Context) int { Action: func(ctx *Context) error {
if ctx.BoolT("debug") != false { if ctx.BoolT("debug") != false {
t.Errorf("main name not set from env") t.Errorf("main name not set from env")
} }
if ctx.BoolT("d") != false { if ctx.BoolT("d") != false {
t.Errorf("short name not set from env") t.Errorf("short name not set from env")
} }
return 0 return nil
}, },
} }
a.Run([]string{"run"}) a.Run([]string{"run"})
@ -837,14 +837,14 @@ func TestParseGeneric(t *testing.T) {
Flags: []Flag{ Flags: []Flag{
GenericFlag{Name: "serve, s", Value: &Parser{}}, GenericFlag{Name: "serve, s", Value: &Parser{}},
}, },
Action: func(ctx *Context) int { Action: func(ctx *Context) error {
if !reflect.DeepEqual(ctx.Generic("serve"), &Parser{"10", "20"}) { if !reflect.DeepEqual(ctx.Generic("serve"), &Parser{"10", "20"}) {
t.Errorf("main name not set") t.Errorf("main name not set")
} }
if !reflect.DeepEqual(ctx.Generic("s"), &Parser{"10", "20"}) { if !reflect.DeepEqual(ctx.Generic("s"), &Parser{"10", "20"}) {
t.Errorf("short name not set") t.Errorf("short name not set")
} }
return 0 return nil
}, },
} }
a.Run([]string{"run", "-s", "10,20"}) a.Run([]string{"run", "-s", "10,20"})
@ -857,14 +857,14 @@ func TestParseGenericFromEnv(t *testing.T) {
Flags: []Flag{ Flags: []Flag{
GenericFlag{Name: "serve, s", Value: &Parser{}, EnvVar: "APP_SERVE"}, GenericFlag{Name: "serve, s", Value: &Parser{}, EnvVar: "APP_SERVE"},
}, },
Action: func(ctx *Context) int { Action: func(ctx *Context) error {
if !reflect.DeepEqual(ctx.Generic("serve"), &Parser{"20", "30"}) { if !reflect.DeepEqual(ctx.Generic("serve"), &Parser{"20", "30"}) {
t.Errorf("main name not set from env") t.Errorf("main name not set from env")
} }
if !reflect.DeepEqual(ctx.Generic("s"), &Parser{"20", "30"}) { if !reflect.DeepEqual(ctx.Generic("s"), &Parser{"20", "30"}) {
t.Errorf("short name not set from env") t.Errorf("short name not set from env")
} }
return 0 return nil
}, },
} }
a.Run([]string{"run"}) a.Run([]string{"run"})
@ -877,11 +877,11 @@ func TestParseGenericFromEnvCascade(t *testing.T) {
Flags: []Flag{ Flags: []Flag{
GenericFlag{Name: "foos", Value: &Parser{}, EnvVar: "COMPAT_FOO,APP_FOO"}, GenericFlag{Name: "foos", Value: &Parser{}, EnvVar: "COMPAT_FOO,APP_FOO"},
}, },
Action: func(ctx *Context) int { Action: func(ctx *Context) error {
if !reflect.DeepEqual(ctx.Generic("foos"), &Parser{"99", "2000"}) { if !reflect.DeepEqual(ctx.Generic("foos"), &Parser{"99", "2000"}) {
t.Errorf("value not set from env") t.Errorf("value not set from env")
} }
return 0 return nil
}, },
} }
a.Run([]string{"run"}) a.Run([]string{"run"})

@ -5,14 +5,14 @@ type BashCompleteFunc func(*Context)
// An action to execute before any subcommands are run, but after the context is ready // An action to execute before any subcommands are run, but after the context is ready
// If a non-nil error is returned, no subcommands are run // If a non-nil error is returned, no subcommands are run
type BeforeFunc func(*Context) (int, error) type BeforeFunc func(*Context) error
// An action to execute after any subcommands are run, but after the subcommand has finished // An action to execute after any subcommands are run, but after the subcommand has finished
// It is run even if Action() panics // It is run even if Action() panics
type AfterFunc func(*Context) (int, error) type AfterFunc func(*Context) error
// The action to execute when no subcommands are specified // The action to execute when no subcommands are specified
type ActionFunc func(*Context) int type ActionFunc func(*Context) error
// Execute this function if the proper command cannot be found // Execute this function if the proper command cannot be found
type CommandNotFoundFunc func(*Context, string) type CommandNotFoundFunc func(*Context, string)

@ -78,14 +78,14 @@ var helpCommand = Command{
Aliases: []string{"h"}, Aliases: []string{"h"},
Usage: "Shows a list of commands or help for one command", Usage: "Shows a list of commands or help for one command",
ArgsUsage: "[command]", ArgsUsage: "[command]",
Action: func(c *Context) int { Action: func(c *Context) error {
args := c.Args() args := c.Args()
if args.Present() { if args.Present() {
ShowCommandHelp(c, args.First()) ShowCommandHelp(c, args.First())
} else { } else {
ShowAppHelp(c) ShowAppHelp(c)
} }
return 0 return nil
}, },
} }
@ -94,14 +94,14 @@ var helpSubcommand = Command{
Aliases: []string{"h"}, Aliases: []string{"h"},
Usage: "Shows a list of commands or help for one command", Usage: "Shows a list of commands or help for one command",
ArgsUsage: "[command]", ArgsUsage: "[command]",
Action: func(c *Context) int { Action: func(c *Context) error {
args := c.Args() args := c.Args()
if args.Present() { if args.Present() {
ShowCommandHelp(c, args.First()) ShowCommandHelp(c, args.First())
} else { } else {
ShowSubcommandHelp(c) ShowSubcommandHelp(c)
} }
return 0 return nil
}, },
} }

@ -66,11 +66,11 @@ func Test_Help_Custom_Flags(t *testing.T) {
Flags: []Flag{ Flags: []Flag{
BoolFlag{Name: "foo, h"}, BoolFlag{Name: "foo, h"},
}, },
Action: func(ctx *Context) int { Action: func(ctx *Context) error {
if ctx.Bool("h") != true { if ctx.Bool("h") != true {
t.Errorf("custom help flag not set") t.Errorf("custom help flag not set")
} }
return 0 return nil
}, },
} }
output := new(bytes.Buffer) output := new(bytes.Buffer)
@ -96,11 +96,11 @@ func Test_Version_Custom_Flags(t *testing.T) {
Flags: []Flag{ Flags: []Flag{
BoolFlag{Name: "foo, v"}, BoolFlag{Name: "foo, v"},
}, },
Action: func(ctx *Context) int { Action: func(ctx *Context) error {
if ctx.Bool("v") != true { if ctx.Bool("v") != true {
t.Errorf("custom version flag not set") t.Errorf("custom version flag not set")
} }
return 0 return nil
}, },
} }
output := new(bytes.Buffer) output := new(bytes.Buffer)

Loading…
Cancel
Save