Rename func type suffixes Fn->Func and add OnUsageErrorFunc

This commit is contained in:
Dan Buch 2016-04-25 18:29:05 -04:00
parent 10c8309d84
commit a17c8cf1d8
No known key found for this signature in database
GPG Key ID: FAEF12936DD3E3EC
5 changed files with 28 additions and 26 deletions

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)) cli.BeforeFn { func InitInputSource(flags []cli.Flag, createInputSource func() (InputSourceContext, error)) cli.BeforeFunc {
return func(context *cli.Context) (int, error) { return func(context *cli.Context) (int, 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)) cli.BeforeFn { 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) (int, error) {
inputSource, err := createInputSource(context) inputSource, err := createInputSource(context)
if err != nil { if err != nil {

16
app.go
View File

@ -47,21 +47,19 @@ 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 BashCompleteFn 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 BeforeFn 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 AfterFn After AfterFunc
// The action to execute when no subcommands are specified // The action to execute when no subcommands are specified
Action ActionFn Action ActionFunc
// Execute this function if the proper command cannot be found // Execute this function if the proper command cannot be found
CommandNotFound CommandNotFoundFn 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

View File

@ -26,19 +26,17 @@ 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 BashCompleteFn 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 BeforeFn 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 AfterFn After AfterFunc
// The function to call when this command is invoked // The function to call when this command is invoked
Action ActionFn Action ActionFunc
// Execute this function, if an usage error occurs. This is useful for displaying customized usage error messages. // Execute this function if a 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) error
// List of child commands // List of child commands
Subcommands Commands Subcommands Commands
// List of flags to parse // List of flags to parse
@ -125,7 +123,7 @@ 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) err := c.OnUsageError(ctx, err, false)
if err != nil { if err != nil {
return DefaultErrorExitCode, err return DefaultErrorExitCode, err
} }

View File

@ -81,7 +81,7 @@ func TestCommand_OnUsageError_WithWrongFlagValue(t *testing.T) {
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

@ -1,18 +1,24 @@
package cli package cli
// An action to execute when the bash-completion flag is set // An action to execute when the bash-completion flag is set
type BashCompleteFn func(*Context) 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 BeforeFn func(*Context) (int, error) type BeforeFunc func(*Context) (int, 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 AfterFn func(*Context) (int, error) type AfterFunc func(*Context) (int, error)
// The action to execute when no subcommands are specified // The action to execute when no subcommands are specified
type ActionFn func(*Context) int type ActionFunc func(*Context) int
// Execute this function if the proper command cannot be found // Execute this function if the proper command cannot be found
type CommandNotFoundFn func(*Context, string) 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