Skip to content

Commit d065061

Browse files
authored
feat: Add app-level fields (#11)
1 parent bc9585b commit d065061

8 files changed

Lines changed: 264 additions & 182 deletions

File tree

‎.github/workflows/go.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
- name: Set up Go
1717
uses: actions/setup-go@v3
1818
with:
19-
go-version: 1.18
19+
go-version: 1.22
2020

2121
- name: Build
2222
run: go build -v ./...

‎.gitignore‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
*.coverprofile
22
node_modules/
3-
*.test
3+
*.test
4+
.idea/
5+
.vscode/

‎app.go‎

Lines changed: 89 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,7 @@ import (
1212

1313
var (
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.
468524
type 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-
}

‎app_test.go‎

Lines changed: 1 addition & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,90 +1579,6 @@ func TestCustomHelpVersionFlags(t *testing.T) {
15791579
}
15801580
}
15811581

1582-
func TestHandleAction_WithNonFuncAction(t *testing.T) {
1583-
app := NewApp()
1584-
app.Action = 42
1585-
fs, err := flagSet(app.Name, app.Flags)
1586-
if err != nil {
1587-
t.Errorf("error creating FlagSet: %s", err)
1588-
}
1589-
err = HandleAction(app.Action, NewContext(app, fs, nil))
1590-
1591-
if err == nil {
1592-
t.Fatalf("expected to receive error from Run, got none")
1593-
}
1594-
1595-
exitErr, ok := err.(*ExitError)
1596-
1597-
if !ok {
1598-
t.Fatalf("expected to receive a *ExitError")
1599-
}
1600-
1601-
if !strings.HasPrefix(exitErr.Error(), "ERROR invalid Action type.") {
1602-
t.Fatalf("expected an unknown Action error, but got: %v", exitErr.Error())
1603-
}
1604-
1605-
if exitErr.ExitCode() != 2 {
1606-
t.Fatalf("expected error exit code to be 2, but got: %v", exitErr.ExitCode())
1607-
}
1608-
}
1609-
1610-
func TestHandleAction_WithInvalidFuncSignature(t *testing.T) {
1611-
app := NewApp()
1612-
app.Action = func() string { return "" }
1613-
fs, err := flagSet(app.Name, app.Flags)
1614-
if err != nil {
1615-
t.Errorf("error creating FlagSet: %s", err)
1616-
}
1617-
err = HandleAction(app.Action, NewContext(app, fs, nil))
1618-
1619-
if err == nil {
1620-
t.Fatalf("expected to receive error from Run, got none")
1621-
}
1622-
1623-
exitErr, ok := err.(*ExitError)
1624-
1625-
if !ok {
1626-
t.Fatalf("expected to receive a *ExitError")
1627-
}
1628-
1629-
if !strings.HasPrefix(exitErr.Error(), "ERROR invalid Action type") {
1630-
t.Fatalf("expected an unknown Action error, but got: %v", exitErr.Error())
1631-
}
1632-
1633-
if exitErr.ExitCode() != 2 {
1634-
t.Fatalf("expected error exit code to be 2, but got: %v", exitErr.ExitCode())
1635-
}
1636-
}
1637-
1638-
func TestHandleAction_WithInvalidFuncReturnSignature(t *testing.T) {
1639-
app := NewApp()
1640-
app.Action = func(_ *Context) (int, error) { return 0, nil }
1641-
fs, err := flagSet(app.Name, app.Flags)
1642-
if err != nil {
1643-
t.Errorf("error creating FlagSet: %s", err)
1644-
}
1645-
err = HandleAction(app.Action, NewContext(app, fs, nil))
1646-
1647-
if err == nil {
1648-
t.Fatalf("expected to receive error from Run, got none")
1649-
}
1650-
1651-
exitErr, ok := err.(*ExitError)
1652-
1653-
if !ok {
1654-
t.Fatalf("expected to receive a *ExitError")
1655-
}
1656-
1657-
if !strings.HasPrefix(exitErr.Error(), "ERROR invalid Action type") {
1658-
t.Fatalf("expected an invalid Action signature error, but got: %v", exitErr.Error())
1659-
}
1660-
1661-
if exitErr.ExitCode() != 2 {
1662-
t.Fatalf("expected error exit code to be 2, but got: %v", exitErr.ExitCode())
1663-
}
1664-
}
1665-
16661582
func TestHandleAction_WithUnknownPanic(t *testing.T) {
16671583
defer func() { refute(t, recover(), nil) }()
16681584

@@ -1677,7 +1593,7 @@ func TestHandleAction_WithUnknownPanic(t *testing.T) {
16771593
if err != nil {
16781594
t.Errorf("error creating FlagSet: %s", err)
16791595
}
1680-
HandleAction(app.Action, NewContext(app, fs, nil))
1596+
app.Action(NewContext(app, fs, nil))
16811597
}
16821598

16831599
func TestShellCompletionForIncompleteFlags(t *testing.T) {
@@ -1723,21 +1639,3 @@ func TestShellCompletionForIncompleteFlags(t *testing.T) {
17231639
t.Errorf("app should not return an error: %s", err)
17241640
}
17251641
}
1726-
1727-
func TestHandleActionActuallyWorksWithActions(t *testing.T) {
1728-
var f ActionFunc
1729-
called := false
1730-
f = func(c *Context) error {
1731-
called = true
1732-
return nil
1733-
}
1734-
1735-
err := HandleAction(f, nil)
1736-
if err != nil {
1737-
t.Errorf("Should not have errored: %v", err)
1738-
}
1739-
1740-
if !called {
1741-
t.Errorf("Function was not called")
1742-
}
1743-
}

0 commit comments

Comments
 (0)