Rename func type suffixes `Fn`->`Func` and add `OnUsageErrorFunc`

main
Dan Buch 9 years ago
parent 10c8309d84
commit a17c8cf1d8
No known key found for this signature in database
GPG Key ID: FAEF12936DD3E3EC

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

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

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

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

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

Loading…
Cancel
Save