From a17c8cf1d8cce58eb467b8310ba684091d10a14c Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Mon, 25 Apr 2016 18:29:05 -0400 Subject: [PATCH] Rename func type suffixes `Fn`->`Func` and add `OnUsageErrorFunc` --- altsrc/flag.go | 4 ++-- app.go | 16 +++++++--------- command.go | 16 +++++++--------- command_test.go | 2 +- funcs.go | 16 +++++++++++----- 5 files changed, 28 insertions(+), 26 deletions(-) diff --git a/altsrc/flag.go b/altsrc/flag.go index 36ffa57..9aee544 100644 --- a/altsrc/flag.go +++ b/altsrc/flag.go @@ -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 // 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 -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) { inputSource, err := createInputSource() 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 // 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 -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) { inputSource, err := createInputSource(context) if err != nil { diff --git a/app.go b/app.go index 267387c..107bb2c 100644 --- a/app.go +++ b/app.go @@ -47,21 +47,19 @@ type App struct { // Populate on app startup, only gettable throught method Categories() categories CommandCategories // 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 // 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 // It is run even if Action() panics - After AfterFn + After AfterFunc // The action to execute when no subcommands are specified - Action ActionFn + Action ActionFunc // Execute this function if the proper command cannot be found - CommandNotFound CommandNotFoundFn - // 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. - OnUsageError func(context *Context, err error, isSubcommand bool) error + CommandNotFound CommandNotFoundFunc + // Execute this function if an usage error occurs + OnUsageError OnUsageErrorFunc // Compilation date Compiled time.Time // List of all authors who contributed diff --git a/command.go b/command.go index b9db123..7615c15 100644 --- a/command.go +++ b/command.go @@ -26,19 +26,17 @@ type Command struct { // The category the command is part of Category string // 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 // 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 // It is run even if Action() panics - After AfterFn + After AfterFunc // The function to call when this command is invoked - Action ActionFn - // 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. - OnUsageError func(context *Context, err error) error + Action ActionFunc + // Execute this function if a usage error occurs. + OnUsageError OnUsageErrorFunc // List of child commands Subcommands Commands // List of flags to parse @@ -125,7 +123,7 @@ func (c Command) Run(ctx *Context) (ec int, err error) { if err != nil { if c.OnUsageError != nil { - err := c.OnUsageError(ctx, err) + err := c.OnUsageError(ctx, err, false) if err != nil { return DefaultErrorExitCode, err } diff --git a/command_test.go b/command_test.go index 96b20de..80dc5cd 100644 --- a/command_test.go +++ b/command_test.go @@ -81,7 +81,7 @@ func TestCommand_OnUsageError_WithWrongFlagValue(t *testing.T) { Flags: []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\"") { t.Errorf("Expect an invalid value error, but got \"%v\"", err) } diff --git a/funcs.go b/funcs.go index 48909c1..f375fd9 100644 --- a/funcs.go +++ b/funcs.go @@ -1,18 +1,24 @@ package cli // 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 // 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 // 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 -type ActionFn func(*Context) int +type ActionFunc func(*Context) int // 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