Merge pull request #361 from codegangsta/txgruppi-develop

Optional exit code support
This commit is contained in:
Dan Buch 2016-04-30 08:14:08 -04:00
commit 663fc0b623
16 changed files with 561 additions and 170 deletions

View File

@ -13,6 +13,10 @@ matrix:
allow_failures: allow_failures:
- go: tip - go: tip
before_script:
- go get github.com/meatballhat/gfmxr/...
script: script:
- go vet ./... - go vet ./...
- go test -v ./... - go test -v ./...
- gfmxr -c $(grep -c 'package main' README.md) -s README.md

View File

@ -7,6 +7,28 @@
### Added ### Added
- Support for placeholders in flag usage strings - Support for placeholders in flag usage strings
### Changed
- The `App.Action` and `Command.Action` now prefer a return signature of
`func(*cli.Context) error`, as defined by `cli.ActionFunc`. If a non-nil
`error` is returned, there may be two outcomes:
- If the error fulfills `cli.ExitCoder`, then `os.Exit` will be called
automatically
- Else the error is bubbled up and returned from `App.Run`
- Specifying an `Action` with the legacy return signature of
`func(*cli.Context)` will produce a deprecation message to stderr
- Specifying an `Action` that is not a `func` type will produce a non-zero exit
from `App.Run`
- Specifying an `Action` func that has an invalid (input) signature will
produce a non-zero exit from `App.Run`
### Deprecated
- <a name="deprecated-cli-app-runandexitonerror"></a>
`cli.App.RunAndExitOnError`, which should now be done by returning an error
that fulfills `cli.ExitCoder` to `cli.App.Run`.
- <a name="deprecated-cli-app-action-signature"></a> the legacy signature for
`cli.App.Action` of `func(*cli.Context)`, which should now have a return
signature of `func(*cli.Context) error`, as defined by `cli.ActionFunc`.
### Fixed ### Fixed
- Added missing `*cli.Context.GlobalFloat64` method - Added missing `*cli.Context.GlobalFloat64` method

132
README.md
View File

@ -50,7 +50,9 @@ This app will run and show help text, but is not very useful. Let's give an acti
package main package main
import ( import (
"fmt"
"os" "os"
"github.com/codegangsta/cli" "github.com/codegangsta/cli"
) )
@ -58,8 +60,9 @@ func main() {
app := cli.NewApp() app := cli.NewApp()
app.Name = "boom" app.Name = "boom"
app.Usage = "make an explosive entrance" app.Usage = "make an explosive entrance"
app.Action = func(c *cli.Context) { app.Action = func(c *cli.Context) error {
println("boom! I say!") fmt.Println("boom! I say!")
return nil
} }
app.Run(os.Args) app.Run(os.Args)
@ -78,7 +81,9 @@ Start by creating a directory named `greet`, and within it, add a file, `greet.g
package main package main
import ( import (
"fmt"
"os" "os"
"github.com/codegangsta/cli" "github.com/codegangsta/cli"
) )
@ -86,8 +91,9 @@ func main() {
app := cli.NewApp() app := cli.NewApp()
app.Name = "greet" app.Name = "greet"
app.Usage = "fight the loneliness!" app.Usage = "fight the loneliness!"
app.Action = func(c *cli.Context) { app.Action = func(c *cli.Context) error {
println("Hello friend!") fmt.Println("Hello friend!")
return nil
} }
app.Run(os.Args) app.Run(os.Args)
@ -133,8 +139,9 @@ You can lookup arguments by calling the `Args` function on `cli.Context`.
``` go ``` go
... ...
app.Action = func(c *cli.Context) { app.Action = func(c *cli.Context) error {
println("Hello", c.Args()[0]) fmt.Println("Hello", c.Args()[0])
return nil
} }
... ...
``` ```
@ -152,16 +159,17 @@ app.Flags = []cli.Flag {
Usage: "language for the greeting", Usage: "language for the greeting",
}, },
} }
app.Action = func(c *cli.Context) { app.Action = func(c *cli.Context) error {
name := "someone" name := "someone"
if c.NArg() > 0 { if c.NArg() > 0 {
name = c.Args()[0] name = c.Args()[0]
} }
if c.String("lang") == "spanish" { if c.String("lang") == "spanish" {
println("Hola", name) fmt.Println("Hola", name)
} else { } else {
println("Hello", name) fmt.Println("Hello", name)
} }
return nil
} }
... ...
``` ```
@ -179,16 +187,17 @@ app.Flags = []cli.Flag {
Destination: &language, Destination: &language,
}, },
} }
app.Action = func(c *cli.Context) { app.Action = func(c *cli.Context) error {
name := "someone" name := "someone"
if c.NArg() > 0 { if c.NArg() > 0 {
name = c.Args()[0] name = c.Args()[0]
} }
if language == "spanish" { if language == "spanish" {
println("Hola", name) fmt.Println("Hola", name)
} else { } else {
println("Hello", name) fmt.Println("Hello", name)
} }
return nil
} }
... ...
``` ```
@ -286,20 +295,20 @@ altsrc.InputSourceContext for their given sources.
Here is a more complete sample of a command using YAML support: Here is a more complete sample of a command using YAML support:
``` go ``` go
command := &cli.Command{ command := &cli.Command{
Name: "test-cmd", Name: "test-cmd",
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) { Action: func(c *cli.Context) error {
// Action to run // Action to run
}, },
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)
``` ```
### Subcommands ### Subcommands
@ -314,7 +323,7 @@ app.Commands = []cli.Command{
Aliases: []string{"a"}, Aliases: []string{"a"},
Usage: "add a task to the list", Usage: "add a task to the list",
Action: func(c *cli.Context) { Action: func(c *cli.Context) {
println("added task: ", c.Args().First()) fmt.Println("added task: ", c.Args().First())
}, },
}, },
{ {
@ -322,7 +331,7 @@ app.Commands = []cli.Command{
Aliases: []string{"c"}, Aliases: []string{"c"},
Usage: "complete a task on the list", Usage: "complete a task on the list",
Action: func(c *cli.Context) { Action: func(c *cli.Context) {
println("completed task: ", c.Args().First()) fmt.Println("completed task: ", c.Args().First())
}, },
}, },
{ {
@ -334,14 +343,14 @@ app.Commands = []cli.Command{
Name: "add", Name: "add",
Usage: "add a new template", Usage: "add a new template",
Action: func(c *cli.Context) { Action: func(c *cli.Context) {
println("new task template: ", c.Args().First()) fmt.Println("new task template: ", c.Args().First())
}, },
}, },
{ {
Name: "remove", Name: "remove",
Usage: "remove an existing template", Usage: "remove an existing template",
Action: func(c *cli.Context) { Action: func(c *cli.Context) {
println("removed task template: ", c.Args().First()) fmt.Println("removed task template: ", c.Args().First())
}, },
}, },
}, },
@ -360,19 +369,19 @@ E.g.
```go ```go
... ...
app.Commands = []cli.Command{ app.Commands = []cli.Command{
{ {
Name: "noop", Name: "noop",
}, },
{ {
Name: "add", Name: "add",
Category: "template", Category: "template",
}, },
{ {
Name: "remove", Name: "remove",
Category: "template", Category: "template",
}, },
} }
... ...
``` ```
@ -389,6 +398,41 @@ COMMANDS:
... ...
``` ```
### Exit code
Calling `App.Run` will not automatically call `os.Exit`, which means that by
default the exit code will "fall through" to being `0`. An explicit exit code
may be set by returning a non-nil error that fulfills `cli.ExitCoder`, *or* a
`cli.MultiError` that includes an error that fulfills `cli.ExitCoder`, e.g.:
``` go
package main
import (
"os"
"github.com/codegangsta/cli"
)
func main() {
app := cli.NewApp()
app.Flags = []cli.Flag{
cli.BoolTFlag{
Name: "ginger-crouton",
Usage: "is it in the soup?",
},
}
app.Action = func(ctx *cli.Context) error {
if !ctx.Bool("ginger-crouton") {
return cli.NewExitError("it is not in the soup", 86)
}
return nil
}
app.Run(os.Args)
}
```
### Bash Completion ### Bash Completion
You can enable completion commands by setting the `EnableBashCompletion` You can enable completion commands by setting the `EnableBashCompletion`
@ -407,7 +451,7 @@ app.Commands = []cli.Command{
Aliases: []string{"c"}, Aliases: []string{"c"},
Usage: "complete a task on the list", Usage: "complete a task on the list",
Action: func(c *cli.Context) { Action: func(c *cli.Context) {
println("completed task: ", c.Args().First()) fmt.Println("completed task: ", c.Args().First())
}, },
BashComplete: func(c *cli.Context) { BashComplete: func(c *cli.Context) {
// This will complete if no args are passed // This will complete if no args are passed

View File

@ -38,7 +38,7 @@ func ApplyInputSourceValues(context *cli.Context, inputSourceContext InputSource
// InitInputSource is used to to setup an InputSourceContext on a cli.Command Before method. It will create a new // InitInputSource is used to to setup an InputSourceContext on a cli.Command Before method. It will create a new
// 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)) func(context *cli.Context) error { func InitInputSource(flags []cli.Flag, createInputSource func() (InputSourceContext, error)) cli.BeforeFunc {
return func(context *cli.Context) error { return func(context *cli.Context) error {
inputSource, err := createInputSource() inputSource, err := createInputSource()
if err != nil { if err != nil {
@ -52,7 +52,7 @@ func InitInputSource(flags []cli.Flag, createInputSource func() (InputSourceCont
// InitInputSourceWithContext is used to to setup an InputSourceContext on a cli.Command Before method. It will create a new // InitInputSourceWithContext is used to to setup an InputSourceContext on a cli.Command Before method. It will create a new
// 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)) func(context *cli.Context) error { func InitInputSourceWithContext(flags []cli.Flag, createInputSource func(context *cli.Context) (InputSourceContext, error)) cli.BeforeFunc {
return func(context *cli.Context) error { return func(context *cli.Context) error {
inputSource, err := createInputSource(context) inputSource, err := createInputSource(context)
if err != nil { if err != nil {

View File

@ -29,9 +29,10 @@ 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) { Action: func(c *cli.Context) error {
val := c.Int("test") val := c.Int("test")
expect(t, val, 15) expect(t, val, 15)
return nil
}, },
Flags: []cli.Flag{ Flags: []cli.Flag{
NewIntFlag(cli.IntFlag{Name: "test"}), NewIntFlag(cli.IntFlag{Name: "test"}),
@ -61,9 +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) { Action: func(c *cli.Context) error {
val := c.Int("test") val := c.Int("test")
expect(t, val, 10) expect(t, val, 10)
return nil
}, },
Flags: []cli.Flag{ Flags: []cli.Flag{
NewIntFlag(cli.IntFlag{Name: "test", EnvVar: "THE_TEST"}), NewIntFlag(cli.IntFlag{Name: "test", EnvVar: "THE_TEST"}),
@ -92,9 +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) { Action: func(c *cli.Context) error {
val := c.Int("test") val := c.Int("test")
expect(t, val, 7) expect(t, val, 7)
return nil
}, },
Flags: []cli.Flag{ Flags: []cli.Flag{
NewIntFlag(cli.IntFlag{Name: "test"}), NewIntFlag(cli.IntFlag{Name: "test"}),
@ -123,9 +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) { Action: func(c *cli.Context) error {
val := c.Int("test") val := c.Int("test")
expect(t, val, 15) expect(t, val, 15)
return nil
}, },
Flags: []cli.Flag{ Flags: []cli.Flag{
NewIntFlag(cli.IntFlag{Name: "test", Value: 7}), NewIntFlag(cli.IntFlag{Name: "test", Value: 7}),
@ -157,9 +161,10 @@ 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) { Action: func(c *cli.Context) error {
val := c.Int("test") val := c.Int("test")
expect(t, val, 11) expect(t, val, 11)
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"}),

111
app.go
View File

@ -6,10 +6,24 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"reflect"
"sort" "sort"
"time" "time"
) )
var (
appActionDeprecationURL = "https://github.com/codegangsta/cli/blob/master/CHANGELOG.md#deprecated-cli-app-action-signature"
contactSysadmin = "This is an error in the application. Please contact the distributor of this application if this is not you."
errNonFuncAction = NewExitError("ERROR invalid Action type. "+
fmt.Sprintf("Must be a func of type `cli.ActionFunc`. %s", contactSysadmin)+
fmt.Sprintf("See %s", appActionDeprecationURL), 2)
errInvalidActionSignature = NewExitError("ERROR invalid Action signature. "+
fmt.Sprintf("Must be `cli.ActionFunc`. %s", contactSysadmin)+
fmt.Sprintf("See %s", appActionDeprecationURL), 2)
)
// App is the main structure of a cli application. It is recommended that // App is the main structure of a cli application. It is recommended that
// an app be created with the cli.NewApp() function // an app be created with the cli.NewApp() function
type App struct { type App struct {
@ -38,21 +52,22 @@ type App struct {
// Populate on app startup, only gettable throught method Categories() // Populate on app startup, only gettable throught method Categories()
categories CommandCategories categories CommandCategories
// An action to execute when the bash-completion flag is set // An action to execute when the bash-completion flag is set
BashComplete func(context *Context) BashComplete BashCompleteFunc
// 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
Before func(context *Context) error Before BeforeFunc
// 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
After func(context *Context) error After AfterFunc
// The action to execute when no subcommands are specified // The action to execute when no subcommands are specified
Action func(context *Context) Action interface{}
// TODO: replace `Action: interface{}` with `Action: ActionFunc` once some kind
// of deprecation period has passed, maybe?
// Execute this function if the proper command cannot be found // Execute this function if the proper command cannot be found
CommandNotFound func(context *Context, command string) CommandNotFound CommandNotFoundFunc
// Execute this function, if an usage error occurs. This is useful for displaying customized usage error messages. // Execute this function if an usage error occurs
// This function is able to replace the original error messages. OnUsageError OnUsageErrorFunc
// If this function is not set, the "Incorrect usage" is displayed and the execution is interrupted.
OnUsageError func(context *Context, err error, isSubcommand bool) error
// Compilation date // Compilation date
Compiled time.Time Compiled time.Time
// List of all authors who contributed // List of all authors who contributed
@ -149,6 +164,9 @@ func (a *App) Run(arguments []string) (err error) {
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 {
HandleExitCoder(err)
}
return err return err
} else { } else {
fmt.Fprintf(a.Writer, "%s\n\n", "Incorrect Usage.") fmt.Fprintf(a.Writer, "%s\n\n", "Incorrect Usage.")
@ -180,10 +198,12 @@ func (a *App) Run(arguments []string) (err error) {
} }
if a.Before != nil { if a.Before != nil {
err = a.Before(context) beforeErr := a.Before(context)
if err != nil { if beforeErr != nil {
fmt.Fprintf(a.Writer, "%v\n\n", err) fmt.Fprintf(a.Writer, "%v\n\n", beforeErr)
ShowAppHelp(context) ShowAppHelp(context)
HandleExitCoder(beforeErr)
err = beforeErr
return err return err
} }
} }
@ -198,12 +218,19 @@ func (a *App) Run(arguments []string) (err error) {
} }
// Run default Action // Run default Action
a.Action(context) err = HandleAction(a.Action, context)
return nil
if err != nil {
HandleExitCoder(err)
}
return err
} }
// Another entry point to the cli app, takes care of passing arguments and error handling // DEPRECATED: Another entry point to the cli app, takes care of passing arguments and error handling
func (a *App) RunAndExitOnError() { func (a *App) RunAndExitOnError() {
fmt.Fprintln(os.Stderr,
"DEPRECATED cli.App.RunAndExitOnError. "+
"See https://github.com/codegangsta/cli/blob/master/CHANGELOG.md#deprecated-cli-app-runandexitonerror")
if err := a.Run(os.Args); err != nil { if err := a.Run(os.Args); err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
os.Exit(1) os.Exit(1)
@ -261,6 +288,7 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
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)
HandleExitCoder(err)
return err return err
} else { } else {
fmt.Fprintf(a.Writer, "%s\n\n", "Incorrect Usage.") fmt.Fprintf(a.Writer, "%s\n\n", "Incorrect Usage.")
@ -283,6 +311,7 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
defer func() { defer func() {
afterErr := a.After(context) afterErr := a.After(context)
if afterErr != nil { if afterErr != nil {
HandleExitCoder(err)
if err != nil { if err != nil {
err = NewMultiError(err, afterErr) err = NewMultiError(err, afterErr)
} else { } else {
@ -293,8 +322,10 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
} }
if a.Before != nil { if a.Before != nil {
err := a.Before(context) beforeErr := a.Before(context)
if err != nil { if beforeErr != nil {
HandleExitCoder(beforeErr)
err = beforeErr
return err return err
} }
} }
@ -309,9 +340,12 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
} }
// Run default Action // Run default Action
a.Action(context) err = HandleAction(a.Action, context)
return nil if err != nil {
HandleExitCoder(err)
}
return err
} }
// 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
@ -361,3 +395,42 @@ func (a Author) String() string {
return fmt.Sprintf("%v %v", a.Name, e) return fmt.Sprintf("%v %v", a.Name, e)
} }
// HandleAction uses ✧✧✧reflection✧✧✧ to figure out if the given Action is an
// ActionFunc, a func with the legacy signature for Action, or some other
// invalid thing. If it's an ActionFunc or a func with the legacy signature for
// Action, the func is run!
func HandleAction(action interface{}, context *Context) (err error) {
defer func() {
if r := recover(); r != nil {
switch r.(type) {
case error:
err = r.(error)
default:
err = NewExitError(fmt.Sprintf("ERROR unknown Action error: %v. See %s", r, appActionDeprecationURL), 2)
}
}
}()
if reflect.TypeOf(action).Kind() != reflect.Func {
return errNonFuncAction
}
vals := reflect.ValueOf(action).Call([]reflect.Value{reflect.ValueOf(context)})
if len(vals) == 0 {
fmt.Fprintln(os.Stderr,
"DEPRECATED Action signature. Must be `cli.ActionFunc`")
return nil
}
if len(vals) > 1 {
return errInvalidActionSignature
}
if retErr, ok := reflect.ValueOf(vals[0]).Interface().(error); ok {
return retErr
}
return err
}

View File

@ -26,8 +26,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) { app.Action = func(c *Context) error {
fmt.Printf("Hello %v\n", c.String("name")) fmt.Printf("Hello %v\n", c.String("name"))
return nil
} }
app.UsageText = "app [first_arg] [second_arg]" app.UsageText = "app [first_arg] [second_arg]"
app.Author = "Harrison" app.Author = "Harrison"
@ -62,8 +63,9 @@ func ExampleApp_Run_subcommand() {
Usage: "Name of the person to greet", Usage: "Name of the person to greet",
}, },
}, },
Action: func(c *Context) { Action: func(c *Context) error {
fmt.Println("Hello,", c.String("name")) fmt.Println("Hello,", c.String("name"))
return nil
}, },
}, },
}, },
@ -90,8 +92,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) { Action: func(c *Context) error {
fmt.Printf("i like to describe things") fmt.Printf("i like to describe things")
return nil
}, },
}, },
} }
@ -120,15 +123,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) { Action: func(c *Context) error {
fmt.Printf("i like to describe things") fmt.Printf("i like to describe things")
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) { Action: func(c *Context) error {
fmt.Printf("the next example") fmt.Printf("the next example")
return nil
}, },
}, },
} }
@ -146,8 +151,9 @@ func TestApp_Run(t *testing.T) {
s := "" s := ""
app := NewApp() app := NewApp()
app.Action = func(c *Context) { app.Action = func(c *Context) error {
s = s + c.Args().First() s = s + c.Args().First()
return nil
} }
err := app.Run([]string{"command", "foo"}) err := app.Run([]string{"command", "foo"})
@ -192,9 +198,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) { Action: func(c *Context) error {
parsedOption = c.String("option") parsedOption = c.String("option")
firstArg = c.Args().First() firstArg = c.Args().First()
return nil
}, },
} }
app.Commands = []Command{command} app.Commands = []Command{command}
@ -212,8 +219,9 @@ func TestApp_RunAsSubcommandParseFlags(t *testing.T) {
a.Commands = []Command{ a.Commands = []Command{
{ {
Name: "foo", Name: "foo",
Action: func(c *Context) { Action: func(c *Context) error {
context = c context = c
return nil
}, },
Flags: []Flag{ Flags: []Flag{
StringFlag{ StringFlag{
@ -241,9 +249,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) { Action: func(c *Context) error {
parsedOption = c.String("option") parsedOption = c.String("option")
args = c.Args() args = c.Args()
return nil
}, },
} }
app.Commands = []Command{command} app.Commands = []Command{command}
@ -262,8 +271,9 @@ func TestApp_CommandWithDash(t *testing.T) {
app := NewApp() app := NewApp()
command := Command{ command := Command{
Name: "cmd", Name: "cmd",
Action: func(c *Context) { Action: func(c *Context) error {
args = c.Args() args = c.Args()
return nil
}, },
} }
app.Commands = []Command{command} app.Commands = []Command{command}
@ -280,8 +290,9 @@ func TestApp_CommandWithNoFlagBeforeTerminator(t *testing.T) {
app := NewApp() app := NewApp()
command := Command{ command := Command{
Name: "cmd", Name: "cmd",
Action: func(c *Context) { Action: func(c *Context) error {
args = c.Args() args = c.Args()
return nil
}, },
} }
app.Commands = []Command{command} app.Commands = []Command{command}
@ -300,8 +311,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) { app.Action = func(c *Context) error {
meters = c.Float64("height") meters = c.Float64("height")
return nil
} }
app.Run([]string{"", "--height", "1.93"}) app.Run([]string{"", "--height", "1.93"})
@ -320,11 +332,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) { 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 nil
}, },
} }
app.Commands = []Command{command} app.Commands = []Command{command}
@ -377,9 +390,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) { Action: func(c *Context) error {
parsedIntSlice = c.IntSlice("a") parsedIntSlice = c.IntSlice("a")
parsedStringSlice = c.StringSlice("str") parsedStringSlice = c.StringSlice("str")
return nil
}, },
} }
app.Commands = []Command{command} app.Commands = []Command{command}
@ -463,9 +477,10 @@ func TestApp_BeforeFunc(t *testing.T) {
app.Commands = []Command{ app.Commands = []Command{
Command{ Command{
Name: "sub", Name: "sub",
Action: func(c *Context) { Action: func(c *Context) error {
counts.Total++ counts.Total++
counts.SubCommand = counts.Total counts.SubCommand = counts.Total
return nil
}, },
}, },
} }
@ -508,6 +523,29 @@ func TestApp_BeforeFunc(t *testing.T) {
t.Errorf("Subcommand executed when NOT expected") t.Errorf("Subcommand executed when NOT expected")
} }
// reset
counts = &opCounts{}
afterError := errors.New("fail again")
app.After = func(_ *Context) error {
return afterError
}
// run with the Before() func failing, wrapped by After()
err = app.Run([]string{"command", "--opt", "fail", "sub"})
// should be the same error produced by the Before func
if _, ok := err.(MultiError); !ok {
t.Errorf("MultiError expected, but not received")
}
if counts.Before != 1 {
t.Errorf("Before() not executed when expected")
}
if counts.SubCommand != 0 {
t.Errorf("Subcommand executed when NOT expected")
}
} }
func TestApp_AfterFunc(t *testing.T) { func TestApp_AfterFunc(t *testing.T) {
@ -531,9 +569,10 @@ func TestApp_AfterFunc(t *testing.T) {
app.Commands = []Command{ app.Commands = []Command{
Command{ Command{
Name: "sub", Name: "sub",
Action: func(c *Context) { Action: func(c *Context) error {
counts.Total++ counts.Total++
counts.SubCommand = counts.Total counts.SubCommand = counts.Total
return nil
}, },
}, },
} }
@ -645,9 +684,10 @@ func TestApp_CommandNotFound(t *testing.T) {
app.Commands = []Command{ app.Commands = []Command{
Command{ Command{
Name: "bar", Name: "bar",
Action: func(c *Context) { Action: func(c *Context) error {
counts.Total++ counts.Total++
counts.SubCommand = counts.Total counts.SubCommand = counts.Total
return nil
}, },
}, },
} }
@ -670,6 +710,7 @@ func TestApp_OrderOfOperations(t *testing.T) {
counts.Total++ counts.Total++
counts.BashComplete = counts.Total counts.BashComplete = counts.Total
} }
app.OnUsageError = func(c *Context, err error, isSubcommand bool) error { app.OnUsageError = func(c *Context, err error, isSubcommand bool) error {
counts.Total++ counts.Total++
counts.OnUsageError = counts.Total counts.OnUsageError = counts.Total
@ -710,16 +751,18 @@ func TestApp_OrderOfOperations(t *testing.T) {
app.Commands = []Command{ app.Commands = []Command{
Command{ Command{
Name: "bar", Name: "bar",
Action: func(c *Context) { Action: func(c *Context) error {
counts.Total++ counts.Total++
counts.SubCommand = counts.Total counts.SubCommand = counts.Total
return nil
}, },
}, },
} }
app.Action = func(c *Context) { app.Action = func(c *Context) error {
counts.Total++ counts.Total++
counts.Action = counts.Total counts.Action = counts.Total
return nil
} }
_ = app.Run([]string{"command", "--nope"}) _ = app.Run([]string{"command", "--nope"})
@ -986,8 +1029,9 @@ 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) { app.Action = func(c *Context) error {
buf.WriteString("boom I say!") buf.WriteString("boom I say!")
return nil
} }
err := app.Run(args) err := app.Run(args)
@ -1017,8 +1061,9 @@ 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) { app.Action = func(c *Context) error {
buf.WriteString("boom I say!") buf.WriteString("boom I say!")
return nil
} }
err := app.Run(args) err := app.Run(args)
@ -1086,7 +1131,7 @@ 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) {} app.Action = func(c *Context) error { return nil }
app.Before = func(c *Context) error { return fmt.Errorf("before error") } app.Before = func(c *Context) error { return fmt.Errorf("before error") }
app.After = func(c *Context) error { return fmt.Errorf("after error") } app.After = func(c *Context) error { return fmt.Errorf("after error") }
@ -1190,3 +1235,75 @@ func TestApp_OnUsageError_WithWrongFlagValue_ForSubcommand(t *testing.T) {
t.Errorf("Expect an intercepted error, but got \"%v\"", err) t.Errorf("Expect an intercepted error, but got \"%v\"", err)
} }
} }
func TestHandleAction_WithNonFuncAction(t *testing.T) {
app := NewApp()
app.Action = 42
err := HandleAction(app.Action, NewContext(app, flagSet(app.Name, app.Flags), nil))
if err == nil {
t.Fatalf("expected to receive error from Run, got none")
}
exitErr, ok := err.(*ExitError)
if !ok {
t.Fatalf("expected to receive a *ExitError")
}
if !strings.HasPrefix(exitErr.Error(), "ERROR invalid Action type") {
t.Fatalf("expected an unknown Action error, but got: %v", exitErr.Error())
}
if exitErr.ExitCode() != 2 {
t.Fatalf("expected error exit code to be 2, but got: %v", exitErr.ExitCode())
}
}
func TestHandleAction_WithInvalidFuncSignature(t *testing.T) {
app := NewApp()
app.Action = func() string { return "" }
err := HandleAction(app.Action, NewContext(app, flagSet(app.Name, app.Flags), nil))
if err == nil {
t.Fatalf("expected to receive error from Run, got none")
}
exitErr, ok := err.(*ExitError)
if !ok {
t.Fatalf("expected to receive a *ExitError")
}
if !strings.HasPrefix(exitErr.Error(), "ERROR unknown Action error") {
t.Fatalf("expected an unknown Action error, but got: %v", exitErr.Error())
}
if exitErr.ExitCode() != 2 {
t.Fatalf("expected error exit code to be 2, but got: %v", exitErr.ExitCode())
}
}
func TestHandleAction_WithInvalidFuncReturnSignature(t *testing.T) {
app := NewApp()
app.Action = func(_ *Context) (int, error) { return 0, nil }
err := HandleAction(app.Action, NewContext(app, flagSet(app.Name, app.Flags), nil))
if err == nil {
t.Fatalf("expected to receive error from Run, got none")
}
exitErr, ok := err.(*ExitError)
if !ok {
t.Fatalf("expected to receive a *ExitError")
}
if !strings.HasPrefix(exitErr.Error(), "ERROR invalid Action signature") {
t.Fatalf("expected an invalid Action signature error, but got: %v", exitErr.Error())
}
if exitErr.ExitCode() != 2 {
t.Fatalf("expected error exit code to be 2, but got: %v", exitErr.ExitCode())
}
}

21
cli.go
View File

@ -17,24 +17,3 @@
// app.Run(os.Args) // app.Run(os.Args)
// } // }
package cli package cli
import (
"strings"
)
type MultiError struct {
Errors []error
}
func NewMultiError(err ...error) MultiError {
return MultiError{Errors: err}
}
func (m MultiError) Error() string {
errs := make([]string, len(m.Errors))
for i, err := range m.Errors {
errs[i] = err.Error()
}
return strings.Join(errs, "\n")
}

View File

@ -26,19 +26,20 @@ type Command struct {
// The category the command is part of // The category the command is part of
Category string Category string
// The function to call when checking for bash command completions // The function to call when checking for bash command completions
BashComplete func(context *Context) BashComplete BashCompleteFunc
// An action to execute before any sub-subcommands are run, but after the context is ready // An action to execute before any sub-subcommands are run, but after the context is ready
// If a non-nil error is returned, no sub-subcommands are run // If a non-nil error is returned, no sub-subcommands are run
Before func(context *Context) error Before BeforeFunc
// An action to execute after any subcommands are run, but before 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
After func(context *Context) error After AfterFunc
// The function to call when this command is invoked // The function to call when this command is invoked
Action func(context *Context) Action interface{}
// Execute this function, if an usage error occurs. This is useful for displaying customized usage error messages. // TODO: replace `Action: interface{}` with `Action: ActionFunc` once some kind
// This function is able to replace the original error messages. // of deprecation period has passed, maybe?
// If this function is not set, the "Incorrect usage" is displayed and the execution is interrupted.
OnUsageError func(context *Context, err error) error // Execute this function if a usage error occurs.
OnUsageError OnUsageErrorFunc
// List of child commands // List of child commands
Subcommands Commands Subcommands Commands
// List of flags to parse // List of flags to parse
@ -125,7 +126,8 @@ func (c Command) Run(ctx *Context) (err error) {
if err != nil { if err != nil {
if c.OnUsageError != nil { if c.OnUsageError != nil {
err := c.OnUsageError(ctx, err) err := c.OnUsageError(ctx, err, false)
HandleExitCoder(err)
return err return err
} else { } else {
fmt.Fprintln(ctx.App.Writer, "Incorrect Usage.") fmt.Fprintln(ctx.App.Writer, "Incorrect Usage.")
@ -142,6 +144,7 @@ func (c Command) Run(ctx *Context) (err error) {
ShowCommandHelp(ctx, c.Name) ShowCommandHelp(ctx, c.Name)
return 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) {
@ -156,6 +159,7 @@ func (c Command) Run(ctx *Context) (err error) {
defer func() { defer func() {
afterErr := c.After(context) afterErr := c.After(context)
if afterErr != nil { if afterErr != nil {
HandleExitCoder(err)
if err != nil { if err != nil {
err = NewMultiError(err, afterErr) err = NewMultiError(err, afterErr)
} else { } else {
@ -166,18 +170,23 @@ func (c Command) Run(ctx *Context) (err error) {
} }
if c.Before != nil { if c.Before != nil {
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)
HandleExitCoder(err)
return err return err
} }
} }
context.Command = c context.Command = c
c.Action(context) err = HandleAction(c.Action, context)
return nil
if err != nil {
HandleExitCoder(err)
}
return err
} }
func (c Command) Names() []string { func (c Command) Names() []string {

View File

@ -34,7 +34,7 @@ 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) {}, Action: func(_ *Context) error { return nil },
} }
command.SkipFlagParsing = c.skipFlagParsing command.SkipFlagParsing = c.skipFlagParsing
@ -50,9 +50,13 @@ func TestCommand_Run_DoesNotOverwriteErrorFromBefore(t *testing.T) {
app := NewApp() app := NewApp()
app.Commands = []Command{ app.Commands = []Command{
Command{ Command{
Name: "bar", Name: "bar",
Before: func(c *Context) error { return fmt.Errorf("before error") }, Before: func(c *Context) error {
After: func(c *Context) error { return fmt.Errorf("after error") }, return fmt.Errorf("before error")
},
After: func(c *Context) error {
return fmt.Errorf("after error")
},
}, },
} }
@ -73,11 +77,11 @@ func TestCommand_OnUsageError_WithWrongFlagValue(t *testing.T) {
app := NewApp() app := NewApp()
app.Commands = []Command{ app.Commands = []Command{
Command{ Command{
Name: "bar", Name: "bar",
Flags: []Flag{ Flags: []Flag{
IntFlag{Name: "flag"}, IntFlag{Name: "flag"},
}, },
OnUsageError: func(c *Context, err error) error { OnUsageError: func(c *Context, err error, _ bool) error {
if !strings.HasPrefix(err.Error(), "invalid value \"wrong\"") { if !strings.HasPrefix(err.Error(), "invalid value \"wrong\"") {
t.Errorf("Expect an invalid value error, but got \"%v\"", err) t.Errorf("Expect an invalid value error, but got \"%v\"", err)
} }

View File

@ -154,9 +154,10 @@ func TestContext_GlobalFlag(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) { app.Action = func(c *Context) error {
globalFlag = c.GlobalString("global") globalFlag = c.GlobalString("global")
globalFlagSet = c.GlobalIsSet("global") globalFlagSet = c.GlobalIsSet("global")
return nil
} }
app.Run([]string{"command", "-g", "foo"}) app.Run([]string{"command", "-g", "foo"})
expect(t, globalFlag, "foo") expect(t, globalFlag, "foo")
@ -182,13 +183,14 @@ func TestContext_GlobalFlagsInSubcommands(t *testing.T) {
Subcommands: []Command{ Subcommands: []Command{
{ {
Name: "bar", Name: "bar",
Action: func(c *Context) { 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 nil
}, },
}, },
}, },

75
errors.go Normal file
View File

@ -0,0 +1,75 @@
package cli
import (
"fmt"
"os"
"strings"
)
type MultiError struct {
Errors []error
}
func NewMultiError(err ...error) MultiError {
return MultiError{Errors: err}
}
func (m MultiError) Error() string {
errs := make([]string, len(m.Errors))
for i, err := range m.Errors {
errs[i] = err.Error()
}
return strings.Join(errs, "\n")
}
// ExitCoder is the interface checked by `App` and `Command` for a custom exit
// code
type ExitCoder interface {
ExitCode() int
}
// ExitError fulfills both the builtin `error` interface and `ExitCoder`
type ExitError struct {
exitCode int
message string
}
// NewExitError makes a new *ExitError
func NewExitError(message string, exitCode int) *ExitError {
return &ExitError{
exitCode: exitCode,
message: message,
}
}
// Error returns the string message, fulfilling the interface required by
// `error`
func (ee *ExitError) Error() string {
return ee.message
}
// ExitCode returns the exit code, fulfilling the interface required by
// `ExitCoder`
func (ee *ExitError) ExitCode() int {
return ee.exitCode
}
// HandleExitCoder checks if the error fulfills the ExitCoder interface, and if
// so prints the error to stderr (if it is non-empty) and calls os.Exit with the
// given exit code. If the given error is a MultiError, then this func is
// called on all members of the Errors slice.
func HandleExitCoder(err error) {
if exitErr, ok := err.(ExitCoder); ok {
if err.Error() != "" {
fmt.Fprintln(os.Stderr, err)
}
os.Exit(exitErr.ExitCode())
}
if multiErr, ok := err.(MultiError); ok {
for _, merr := range multiErr.Errors {
HandleExitCoder(merr)
}
}
}

View File

@ -325,13 +325,14 @@ func TestParseMultiString(t *testing.T) {
Flags: []Flag{ Flags: []Flag{
StringFlag{Name: "serve, s"}, StringFlag{Name: "serve, s"},
}, },
Action: func(ctx *Context) { 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 nil
}, },
}).Run([]string{"run", "-s", "10"}) }).Run([]string{"run", "-s", "10"})
} }
@ -345,10 +346,11 @@ func TestParseDestinationString(t *testing.T) {
Destination: &dest, Destination: &dest,
}, },
}, },
Action: func(ctx *Context) { Action: func(ctx *Context) error {
if dest != "10" { if dest != "10" {
t.Errorf("expected destination String 10") t.Errorf("expected destination String 10")
} }
return nil
}, },
} }
a.Run([]string{"run", "--dest", "10"}) a.Run([]string{"run", "--dest", "10"})
@ -361,13 +363,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) { 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 nil
}, },
}).Run([]string{"run"}) }).Run([]string{"run"})
} }
@ -379,13 +382,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) { 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 nil
}, },
}).Run([]string{"run"}) }).Run([]string{"run"})
} }
@ -395,13 +399,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) { 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 nil
}, },
}).Run([]string{"run", "-s", "10", "-s", "20"}) }).Run([]string{"run", "-s", "10", "-s", "20"})
} }
@ -414,13 +419,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) { 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 nil
}, },
}).Run([]string{"run"}) }).Run([]string{"run"})
} }
@ -433,13 +439,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) { 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 nil
}, },
}).Run([]string{"run"}) }).Run([]string{"run"})
} }
@ -449,13 +456,14 @@ func TestParseMultiInt(t *testing.T) {
Flags: []Flag{ Flags: []Flag{
IntFlag{Name: "serve, s"}, IntFlag{Name: "serve, s"},
}, },
Action: func(ctx *Context) { 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 nil
}, },
} }
a.Run([]string{"run", "-s", "10"}) a.Run([]string{"run", "-s", "10"})
@ -470,10 +478,11 @@ func TestParseDestinationInt(t *testing.T) {
Destination: &dest, Destination: &dest,
}, },
}, },
Action: func(ctx *Context) { Action: func(ctx *Context) error {
if dest != 10 { if dest != 10 {
t.Errorf("expected destination Int 10") t.Errorf("expected destination Int 10")
} }
return nil
}, },
} }
a.Run([]string{"run", "--dest", "10"}) a.Run([]string{"run", "--dest", "10"})
@ -486,13 +495,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) { 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 nil
}, },
} }
a.Run([]string{"run"}) a.Run([]string{"run"})
@ -505,13 +515,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) { 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 nil
}, },
} }
a.Run([]string{"run"}) a.Run([]string{"run"})
@ -522,13 +533,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) { 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 nil
}, },
}).Run([]string{"run", "-s", "10", "-s", "20"}) }).Run([]string{"run", "-s", "10", "-s", "20"})
} }
@ -541,13 +553,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) { 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 nil
}, },
}).Run([]string{"run"}) }).Run([]string{"run"})
} }
@ -560,13 +573,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) { 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 nil
}, },
}).Run([]string{"run"}) }).Run([]string{"run"})
} }
@ -576,13 +590,14 @@ func TestParseMultiFloat64(t *testing.T) {
Flags: []Flag{ Flags: []Flag{
Float64Flag{Name: "serve, s"}, Float64Flag{Name: "serve, s"},
}, },
Action: func(ctx *Context) { 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 nil
}, },
} }
a.Run([]string{"run", "-s", "10.2"}) a.Run([]string{"run", "-s", "10.2"})
@ -597,10 +612,11 @@ func TestParseDestinationFloat64(t *testing.T) {
Destination: &dest, Destination: &dest,
}, },
}, },
Action: func(ctx *Context) { 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 nil
}, },
} }
a.Run([]string{"run", "--dest", "10.2"}) a.Run([]string{"run", "--dest", "10.2"})
@ -613,13 +629,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) { 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 nil
}, },
} }
a.Run([]string{"run"}) a.Run([]string{"run"})
@ -632,13 +649,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) { 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 nil
}, },
} }
a.Run([]string{"run"}) a.Run([]string{"run"})
@ -649,13 +667,14 @@ func TestParseMultiBool(t *testing.T) {
Flags: []Flag{ Flags: []Flag{
BoolFlag{Name: "serve, s"}, BoolFlag{Name: "serve, s"},
}, },
Action: func(ctx *Context) { 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 nil
}, },
} }
a.Run([]string{"run", "--serve"}) a.Run([]string{"run", "--serve"})
@ -670,10 +689,11 @@ func TestParseDestinationBool(t *testing.T) {
Destination: &dest, Destination: &dest,
}, },
}, },
Action: func(ctx *Context) { Action: func(ctx *Context) error {
if dest != true { if dest != true {
t.Errorf("expected destination Bool true") t.Errorf("expected destination Bool true")
} }
return nil
}, },
} }
a.Run([]string{"run", "--dest"}) a.Run([]string{"run", "--dest"})
@ -686,13 +706,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) { 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 nil
}, },
} }
a.Run([]string{"run"}) a.Run([]string{"run"})
@ -705,13 +726,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) { 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 nil
}, },
} }
a.Run([]string{"run"}) a.Run([]string{"run"})
@ -722,13 +744,14 @@ func TestParseMultiBoolT(t *testing.T) {
Flags: []Flag{ Flags: []Flag{
BoolTFlag{Name: "serve, s"}, BoolTFlag{Name: "serve, s"},
}, },
Action: func(ctx *Context) { 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 nil
}, },
} }
a.Run([]string{"run", "--serve"}) a.Run([]string{"run", "--serve"})
@ -743,10 +766,11 @@ func TestParseDestinationBoolT(t *testing.T) {
Destination: &dest, Destination: &dest,
}, },
}, },
Action: func(ctx *Context) { Action: func(ctx *Context) error {
if dest != true { if dest != true {
t.Errorf("expected destination BoolT true") t.Errorf("expected destination BoolT true")
} }
return nil
}, },
} }
a.Run([]string{"run", "--dest"}) a.Run([]string{"run", "--dest"})
@ -759,13 +783,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) { 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 nil
}, },
} }
a.Run([]string{"run"}) a.Run([]string{"run"})
@ -778,13 +803,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) { 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 nil
}, },
} }
a.Run([]string{"run"}) a.Run([]string{"run"})
@ -813,13 +839,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) { 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 nil
}, },
} }
a.Run([]string{"run", "-s", "10,20"}) a.Run([]string{"run", "-s", "10,20"})
@ -832,13 +859,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) { 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 nil
}, },
} }
a.Run([]string{"run"}) a.Run([]string{"run"})
@ -851,10 +879,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) { 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 nil
}, },
} }
a.Run([]string{"run"}) a.Run([]string{"run"})

24
funcs.go Normal file
View File

@ -0,0 +1,24 @@
package cli
// An action to execute when the bash-completion flag is set
type BashCompleteFunc func(*Context)
// 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
type BeforeFunc func(*Context) error
// An action to execute after any subcommands are run, but after the subcommand has finished
// It is run even if Action() panics
type AfterFunc func(*Context) error
// The action to execute when no subcommands are specified
type ActionFunc func(*Context) error
// Execute this function if the proper command cannot be found
type CommandNotFoundFunc func(*Context, string)
// Execute this function if an usage error occurs. This is useful for displaying
// customized usage error messages. This function is able to replace the
// original error messages. If this function is not set, the "Incorrect usage"
// is displayed and the execution is interrupted.
type OnUsageErrorFunc func(context *Context, err error, isSubcommand bool) error

View File

@ -78,13 +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) { 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 nil
}, },
} }
@ -93,13 +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) { 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 nil
}, },
} }

View File

@ -66,10 +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) { 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 nil
}, },
} }
output := new(bytes.Buffer) output := new(bytes.Buffer)
@ -95,10 +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) { 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 nil
}, },
} }
output := new(bytes.Buffer) output := new(bytes.Buffer)