From 9438aba3b89e7053070ef277121a14e5fb95947e Mon Sep 17 00:00:00 2001 From: Lynn Cyrin Date: Thu, 1 Aug 2019 19:54:57 -0700 Subject: [PATCH] remove showFlagError, we can use the help printer assertion to accomplish the same goal --- app.go | 6 ++---- app_test.go | 14 -------------- command.go | 4 ++-- 3 files changed, 4 insertions(+), 20 deletions(-) diff --git a/app.go b/app.go index c286327..6557044 100644 --- a/app.go +++ b/app.go @@ -23,8 +23,6 @@ var ( errInvalidActionType = NewExitError("ERROR invalid Action type. "+ fmt.Sprintf("Must be `func(*Context`)` or `func(*Context) error). %s", contactSysadmin)+ fmt.Sprintf("See %s", appActionDeprecationURL), 2) - - showFlagError printerFunc = fmt.Fprintln ) // App is the main structure of a cli application. It is recommended that @@ -235,7 +233,7 @@ func (a *App) Run(arguments []string) (err error) { cerr := checkRequiredFlags(a.Flags, set) if cerr != nil { - showFlagError(a.Writer, cerr) + fmt.Fprintln(a.Writer, cerr) ShowAppHelp(context) return cerr } @@ -366,7 +364,7 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) { cerr := checkRequiredFlags(a.Flags, set) if cerr != nil { - showFlagError(a.Writer, cerr) + fmt.Fprintln(a.Writer, cerr) ShowSubcommandHelp(context) return cerr } diff --git a/app_test.go b/app_test.go index cd4b1ef..36f8a72 100644 --- a/app_test.go +++ b/app_test.go @@ -913,17 +913,6 @@ func TestRequiredFlagAppRunBehavior(t *testing.T) { } for _, test := range tdata { t.Run(test.testCase, func(t *testing.T) { - // setup - undo showFlagError mock when finished - oldShowError := showFlagError - defer func() { - showFlagError = oldShowError - }() - // setup - mock showFlagError - var showFlagErrorWasCalled = false - showFlagError = func(writer io.Writer, err ...interface{}) (int, error) { - showFlagErrorWasCalled = true - return 0, nil - } // setup - undo HelpPrinter mock when finished oldPrinter := HelpPrinter defer func() { @@ -948,9 +937,6 @@ func TestRequiredFlagAppRunBehavior(t *testing.T) { if test.expectedAnError && err == nil { t.Errorf("expected an error, but there was none") } - if test.expectedAnError && showFlagErrorWasCalled == false { - t.Errorf("showFlagError expected to be called, but was not") - } if !test.expectedAnError && err != nil { t.Errorf("did not expected an error, but there was one: %s", err) } diff --git a/command.go b/command.go index a6b19b5..fdec3c9 100644 --- a/command.go +++ b/command.go @@ -137,7 +137,7 @@ func (c Command) Run(ctx *Context) (err error) { cerr := checkRequiredFlags(c.Flags, set) if cerr != nil { - showFlagError(context.App.Writer, cerr) + fmt.Fprintln(context.App.Writer, cerr) ShowCommandHelp(context, c.Name) return cerr } @@ -273,7 +273,7 @@ func reorderArgs(args []string) []string { } func translateShortOptions(set *flag.FlagSet, flagArgs Args) []string { - allCharsFlags := func (s string) bool { + allCharsFlags := func(s string) bool { for i := range s { f := set.Lookup(string(s[i])) if f == nil {