remove showFlagError, we can use the help printer assertion to accomplish the same goal
This commit is contained in:
parent
386b379d19
commit
9438aba3b8
6
app.go
6
app.go
@ -23,8 +23,6 @@ var (
|
|||||||
errInvalidActionType = NewExitError("ERROR invalid Action type. "+
|
errInvalidActionType = NewExitError("ERROR invalid Action type. "+
|
||||||
fmt.Sprintf("Must be `func(*Context`)` or `func(*Context) error). %s", contactSysadmin)+
|
fmt.Sprintf("Must be `func(*Context`)` or `func(*Context) error). %s", contactSysadmin)+
|
||||||
fmt.Sprintf("See %s", appActionDeprecationURL), 2)
|
fmt.Sprintf("See %s", appActionDeprecationURL), 2)
|
||||||
|
|
||||||
showFlagError printerFunc = fmt.Fprintln
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// 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
|
||||||
@ -235,7 +233,7 @@ func (a *App) Run(arguments []string) (err error) {
|
|||||||
|
|
||||||
cerr := checkRequiredFlags(a.Flags, set)
|
cerr := checkRequiredFlags(a.Flags, set)
|
||||||
if cerr != nil {
|
if cerr != nil {
|
||||||
showFlagError(a.Writer, cerr)
|
fmt.Fprintln(a.Writer, cerr)
|
||||||
ShowAppHelp(context)
|
ShowAppHelp(context)
|
||||||
return cerr
|
return cerr
|
||||||
}
|
}
|
||||||
@ -366,7 +364,7 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
|
|||||||
|
|
||||||
cerr := checkRequiredFlags(a.Flags, set)
|
cerr := checkRequiredFlags(a.Flags, set)
|
||||||
if cerr != nil {
|
if cerr != nil {
|
||||||
showFlagError(a.Writer, cerr)
|
fmt.Fprintln(a.Writer, cerr)
|
||||||
ShowSubcommandHelp(context)
|
ShowSubcommandHelp(context)
|
||||||
return cerr
|
return cerr
|
||||||
}
|
}
|
||||||
|
14
app_test.go
14
app_test.go
@ -913,17 +913,6 @@ func TestRequiredFlagAppRunBehavior(t *testing.T) {
|
|||||||
}
|
}
|
||||||
for _, test := range tdata {
|
for _, test := range tdata {
|
||||||
t.Run(test.testCase, func(t *testing.T) {
|
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
|
// setup - undo HelpPrinter mock when finished
|
||||||
oldPrinter := HelpPrinter
|
oldPrinter := HelpPrinter
|
||||||
defer func() {
|
defer func() {
|
||||||
@ -948,9 +937,6 @@ func TestRequiredFlagAppRunBehavior(t *testing.T) {
|
|||||||
if test.expectedAnError && err == nil {
|
if test.expectedAnError && err == nil {
|
||||||
t.Errorf("expected an error, but there was none")
|
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 {
|
if !test.expectedAnError && err != nil {
|
||||||
t.Errorf("did not expected an error, but there was one: %s", err)
|
t.Errorf("did not expected an error, but there was one: %s", err)
|
||||||
}
|
}
|
||||||
|
@ -137,7 +137,7 @@ func (c Command) Run(ctx *Context) (err error) {
|
|||||||
|
|
||||||
cerr := checkRequiredFlags(c.Flags, set)
|
cerr := checkRequiredFlags(c.Flags, set)
|
||||||
if cerr != nil {
|
if cerr != nil {
|
||||||
showFlagError(context.App.Writer, cerr)
|
fmt.Fprintln(context.App.Writer, cerr)
|
||||||
ShowCommandHelp(context, c.Name)
|
ShowCommandHelp(context, c.Name)
|
||||||
return cerr
|
return cerr
|
||||||
}
|
}
|
||||||
@ -273,7 +273,7 @@ func reorderArgs(args []string) []string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func translateShortOptions(set *flag.FlagSet, flagArgs Args) []string {
|
func translateShortOptions(set *flag.FlagSet, flagArgs Args) []string {
|
||||||
allCharsFlags := func (s string) bool {
|
allCharsFlags := func(s string) bool {
|
||||||
for i := range s {
|
for i := range s {
|
||||||
f := set.Lookup(string(s[i]))
|
f := set.Lookup(string(s[i]))
|
||||||
if f == nil {
|
if f == nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user