@@ -12,14 +12,7 @@ import (
1212
1313var (
1414 changeLogURL = "https://github.com/urfave/cli/blob/master/CHANGELOG.md"
15- appActionDeprecationURL = fmt .Sprintf ("%s#deprecated-cli-app-action-signature" , changeLogURL )
1615 runAndExitOnErrorDeprecationURL = fmt .Sprintf ("%s#deprecated-cli-app-runandexitonerror" , changeLogURL )
17-
18- contactSysadmin = "This is an error in the application. Please contact the distributor of this application if this is not you."
19-
20- errInvalidActionType = NewExitError ("ERROR invalid Action type. " +
21- fmt .Sprintf ("Must be `func(*Context`)` or `func(*Context) error). %s" , contactSysadmin )+
22- fmt .Sprintf ("See %s" , appActionDeprecationURL ), 2 )
2316)
2417
2518// App is the main structure of a cli application. It is recommended that
@@ -63,9 +56,27 @@ type App struct {
6356 After AfterFunc
6457
6558 // The action to execute when no subcommands are specified
66- // Expects a `cli.ActionFunc` but will accept the *deprecated* signature of `func(*cli.Context) {}`
67- // *Note*: support for the deprecated `Action` signature will be removed in a future version
68- Action interface {}
59+ Action ActionFunc
60+
61+ // DefaultBefore executes before the app, commands, and subcommands that do
62+ // not specify a BeforeFunc of their own
63+ DefaultBefore BeforeFunc
64+ // DefaultAction is the action executed by the app, commands, and subcommands
65+ // that do not specify an action of their own
66+ DefaultAction ActionFunc
67+ // DefaultAfter executes after the app, commands, and subcommands that do not
68+ // specify an AfterFunc of their own
69+ DefaultAfter AfterFunc
70+ // DefaultOnUsageError is executed on a usage error by the app, commands, and
71+ // subcommands that do not specify an OnUsageError of their own
72+ DefaultOnUsageError OnUsageErrorFunc
73+ // GlobalHideHelp hides the help flag for the app, all commands, and all subcommands
74+ GlobalHideHelp bool
75+ // GlobalHideHelpCommand hides the help command for the app, all commands, and all subcommands
76+ GlobalHideHelpCommand bool
77+ // GlobalFlags are flags that can be used by the app, any command, or any
78+ // subcommand, unless command.NoGlobalFlags is true
79+ GlobalFlags []Flag
6980
7081 // Execute this function if the proper command cannot be found
7182 CommandNotFound CommandNotFoundFunc
@@ -149,11 +160,16 @@ func (a *App) Setup() {
149160 }
150161 a .Commands = newCmds
151162
163+ // make the app-wide GlobalFlags usable by the app itself
164+ for _ , fl := range a .GlobalFlags {
165+ a .appendFlag (fl )
166+ }
167+
152168 if a .Command (helpCommand .Name ) == nil {
153- if ! a .HideHelpCommand {
169+ if ! a .hideHelpCommand () {
154170 a .Commands = append (a .Commands , helpCommand )
155171 }
156- if ! a .HideHelp && (HelpFlag != BoolFlag {}) {
172+ if ! a .hideHelp () && (HelpFlag != BoolFlag {}) {
157173 a .appendFlag (HelpFlag )
158174 }
159175 }
@@ -211,16 +227,16 @@ func (a *App) Run(arguments []string) (err error) {
211227 }
212228
213229 if err != nil {
214- if a . OnUsageError != nil {
215- err := a . OnUsageError (context , err , false )
230+ if onUsageError := a . resolveOnUsageError (); onUsageError != nil {
231+ err := onUsageError (context , err , false )
216232 HandleExitCoder (err )
217233 return err
218234 }
219235 fmt .Fprintf (a .Writer , "%s %s\n \n " , "Incorrect Usage." , err .Error ())
220236 return err
221237 }
222238
223- if ! a .HideHelp && checkHelp (context ) {
239+ if ! a .hideHelp () && checkHelp (context ) {
224240 ShowAppHelp (context )
225241 return nil
226242 }
@@ -230,9 +246,9 @@ func (a *App) Run(arguments []string) (err error) {
230246 return nil
231247 }
232248
233- if a . After != nil {
249+ if after := a . resolveAfter (); after != nil {
234250 defer func () {
235- if afterErr := a . After (context ); afterErr != nil {
251+ if afterErr := after (context ); afterErr != nil {
236252 if err != nil {
237253 err = NewMultiError (err , afterErr )
238254 } else {
@@ -242,8 +258,8 @@ func (a *App) Run(arguments []string) (err error) {
242258 }()
243259 }
244260
245- if a . Before != nil {
246- beforeErr := a . Before (context )
261+ if before := a . resolveBefore (); before != nil {
262+ beforeErr := before (context )
247263 if beforeErr != nil {
248264 fmt .Fprintf (a .Writer , "%v\n \n " , beforeErr )
249265 HandleExitCoder (beforeErr )
@@ -261,12 +277,8 @@ func (a *App) Run(arguments []string) (err error) {
261277 }
262278 }
263279
264- if a .Action == nil {
265- a .Action = helpCommand .Action
266- }
267-
268280 // Run default Action
269- err = HandleAction ( a . Action , context )
281+ err = a . resolveAction ()( context )
270282
271283 HandleExitCoder (err )
272284 return err
@@ -290,10 +302,10 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
290302 // append help to commands
291303 if len (a .Commands ) > 0 {
292304 if a .Command (helpCommand .Name ) == nil {
293- if ! a .HideHelpCommand {
305+ if ! a .hideHelpCommand () {
294306 a .Commands = append (a .Commands , helpCommand )
295307 }
296- if ! a .HideHelp && (HelpFlag != BoolFlag {}) {
308+ if ! a .hideHelp () && (HelpFlag != BoolFlag {}) {
297309 a .appendFlag (HelpFlag )
298310 }
299311 }
@@ -330,8 +342,8 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
330342 }
331343
332344 if err != nil {
333- if a . OnUsageError != nil {
334- err = a . OnUsageError (context , err , true )
345+ if onUsageError := a . resolveOnUsageError (); onUsageError != nil {
346+ err = onUsageError (context , err , true )
335347 HandleExitCoder (err )
336348 return err
337349 }
@@ -349,9 +361,9 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
349361 }
350362 }
351363
352- if a . After != nil {
364+ if after := a . resolveAfter (); after != nil {
353365 defer func () {
354- afterErr := a . After (context )
366+ afterErr := after (context )
355367 if afterErr != nil {
356368 HandleExitCoder (err )
357369 if err != nil {
@@ -363,8 +375,8 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
363375 }()
364376 }
365377
366- if a . Before != nil {
367- beforeErr := a . Before (context )
378+ if before := a . resolveBefore (); before != nil {
379+ beforeErr := before (context )
368380 if beforeErr != nil {
369381 HandleExitCoder (beforeErr )
370382 err = beforeErr
@@ -382,7 +394,7 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
382394 }
383395
384396 // Run default Action
385- err = HandleAction ( a . Action , context )
397+ err = a . resolveAction ()( context )
386398
387399 HandleExitCoder (err )
388400 return err
@@ -464,6 +476,50 @@ func (a *App) appendFlag(flag Flag) {
464476 }
465477}
466478
479+ // hideHelp reports whether the built-in help flag should be hidden for this
480+ // app, honoring both the app-specific HideHelp and the app-wide GlobalHideHelp.
481+ func (a * App ) hideHelp () bool {
482+ return a .GlobalHideHelp || a .HideHelp
483+ }
484+
485+ // hideHelpCommand reports whether the built-in help command should be hidden
486+ // for this app, honoring both the app-specific HideHelpCommand and the
487+ // app-wide GlobalHideHelpCommand.
488+ func (a * App ) hideHelpCommand () bool {
489+ return a .GlobalHideHelpCommand || a .HideHelpCommand
490+ }
491+
492+ func (a * App ) resolveBefore () BeforeFunc {
493+ if a .Before != nil {
494+ return a .Before
495+ }
496+ return a .DefaultBefore
497+ }
498+
499+ func (a * App ) resolveAfter () AfterFunc {
500+ if a .After != nil {
501+ return a .After
502+ }
503+ return a .DefaultAfter
504+ }
505+
506+ func (a * App ) resolveAction () ActionFunc {
507+ if a .Action != nil {
508+ return a .Action
509+ }
510+ if a .DefaultAction != nil {
511+ return a .DefaultAction
512+ }
513+ return helpCommand .Action
514+ }
515+
516+ func (a * App ) resolveOnUsageError () OnUsageErrorFunc {
517+ if a .OnUsageError != nil {
518+ return a .OnUsageError
519+ }
520+ return a .DefaultOnUsageError
521+ }
522+
467523// Author represents someone who has contributed to a cli project.
468524type Author struct {
469525 Name string // The Authors name
@@ -479,19 +535,3 @@ func (a Author) String() string {
479535
480536 return fmt .Sprintf ("%v%v" , a .Name , e )
481537}
482-
483- // HandleAction attempts to figure out which Action signature was used. If
484- // it's an ActionFunc or a func with the legacy signature for Action, the func
485- // is run!
486- func HandleAction (action interface {}, context * Context ) (err error ) {
487- if a , ok := action .(ActionFunc ); ok {
488- return a (context )
489- } else if a , ok := action .(func (* Context ) error ); ok {
490- return a (context )
491- } else if a , ok := action .(func (* Context )); ok { // deprecated function signature
492- a (context )
493- return nil
494- } else {
495- return errInvalidActionType
496- }
497- }
0 commit comments