Skip to content

Commit 3976dfc

Browse files
authored
Merge pull request #2180 from urfave/consistent-public-defaults-and-more
Ensure public vars reference public types
2 parents ba5d54b + c32894b commit 3976dfc

File tree

10 files changed

+316
-184
lines changed

10 files changed

+316
-184
lines changed

command.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ type Command struct {
8585
Writer io.Writer `json:"-"`
8686
// ErrWriter writes error output
8787
ErrWriter io.Writer `json:"-"`
88-
// ExitErrHandler processes any error encountered while running an App before
89-
// it is returned to the caller. If no function is provided, HandleExitCoder
90-
// is used as the default behavior.
88+
// ExitErrHandler processes any error encountered while running a Command before it is
89+
// returned to the caller. If no function is provided, HandleExitCoder is used as the
90+
// default behavior.
9191
ExitErrHandler ExitErrHandlerFunc `json:"-"`
9292
// Other custom info
9393
Metadata map[string]interface{} `json:"metadata"`

command_run.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,9 @@ func (cmd *Command) run(ctx context.Context, osArgs []string) (_ context.Context
175175
}
176176
if !cmd.hideHelp() {
177177
if cmd.parent == nil {
178-
tracef("running ShowAppHelp")
179-
if err := ShowAppHelp(cmd); err != nil {
180-
tracef("SILENTLY IGNORING ERROR running ShowAppHelp %[1]v (cmd=%[2]q)", err, cmd.Name)
178+
tracef("running ShowRootCommandHelp")
179+
if err := ShowRootCommandHelp(cmd); err != nil {
180+
tracef("SILENTLY IGNORING ERROR running ShowRootCommandHelp %[1]v (cmd=%[2]q)", err, cmd.Name)
181181
}
182182
} else {
183183
tracef("running ShowCommandHelp with %[1]q", cmd.Name)

docs/v3/examples/full-api-example.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,9 @@ func main() {
185185
return nil
186186
},
187187
Action: func(ctx context.Context, cmd *cli.Command) error {
188-
cli.DefaultAppComplete(ctx, cmd)
188+
cli.DefaultRootCommandComplete(ctx, cmd)
189189
cli.HandleExitCoder(errors.New("not an exit coder, though"))
190-
cli.ShowAppHelp(cmd)
190+
cli.ShowRootCommandHelp(cmd)
191191
cli.ShowCommandHelp(ctx, cmd, "also-nope")
192192
cli.ShowSubcommandHelp(cmd)
193193
cli.ShowVersion(cmd)

docs/v3/examples/help/generated-help-text.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ or subcommand, and break execution.
1111

1212
#### Customization
1313

14-
All of the help text generation may be customized, and at multiple levels. The
15-
templates are exposed as variables `AppHelpTemplate`, `CommandHelpTemplate`, and
16-
`SubcommandHelpTemplate` which may be reassigned or augmented, and full override
17-
is possible by assigning a compatible func to the `cli.HelpPrinter` variable,
18-
e.g.:
14+
All of the help text generation may be customized, and at multiple levels. The templates
15+
are exposed as variables `RootCommandHelpTemplate`, `CommandHelpTemplate`, and
16+
`SubcommandHelpTemplate` which may be reassigned or augmented, and full override is
17+
possible by assigning a compatible func to the `cli.HelpPrinter` variable, e.g.:
1918

2019
<!-- {
2120
"output": "Ha HA. I pwnd the help!!1"

errors.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ type ErrorFormatter interface {
9292
Format(s fmt.State, verb rune)
9393
}
9494

95-
// ExitCoder is the interface checked by `App` and `Command` for a custom exit
96-
// code
95+
// ExitCoder is the interface checked by `Command` for a custom exit code.
9796
type ExitCoder interface {
9897
error
9998
ExitCode() int
@@ -107,11 +106,11 @@ type exitError struct {
107106
// Exit wraps a message and exit code into an error, which by default is
108107
// handled with a call to os.Exit during default error handling.
109108
//
110-
// This is the simplest way to trigger a non-zero exit code for an App without
109+
// This is the simplest way to trigger a non-zero exit code for a Command without
111110
// having to call os.Exit manually. During testing, this behavior can be avoided
112-
// by overriding the ExitErrHandler function on an App or the package-global
111+
// by overriding the ExitErrHandler function on a Command or the package-global
113112
// OsExiter function.
114-
func Exit(message interface{}, exitCode int) ExitCoder {
113+
func Exit(message any, exitCode int) ExitCoder {
115114
var err error
116115

117116
switch e := message.(type) {
@@ -144,7 +143,7 @@ func (ee *exitError) ExitCode() int {
144143
// for the ExitCoder interface, and OsExiter will be called with the last exit
145144
// code found, or exit code 1 if no ExitCoder is found.
146145
//
147-
// This function is the default error-handling behavior for an App.
146+
// This function is the default error-handling behavior for a Command.
148147
func HandleExitCoder(err error) {
149148
if err == nil {
150149
return

fish.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"text/template"
99
)
1010

11-
// ToFishCompletion creates a fish completion string for the `*App`
11+
// ToFishCompletion creates a fish completion string for the `*Command`
1212
// The function errors if either parsing or writing of the string fails.
1313
func (cmd *Command) ToFishCompletion() (string, error) {
1414
var w bytes.Buffer

godoc-current.txt

Lines changed: 85 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ GLOBAL OPTIONS:{{template "visiblePersistentFlagTemplate" .}}{{end}}
7979
uses text/template to render templates. You can render custom help text by
8080
setting this variable.
8181

82+
var DefaultAppComplete = DefaultRootCommandComplete
83+
DefaultAppComplete is a backward-compatible name for
84+
DefaultRootCommandComplete.
85+
8286
var DefaultInverseBoolPrefix = "no-"
8387
var ErrWriter io.Writer = os.Stderr
8488
ErrWriter is used to write errors to the user. This can be anything
@@ -131,13 +135,19 @@ COPYRIGHT:
131135
cli.go uses text/template to render templates. You can render custom help
132136
text by setting this variable.
133137

134-
var ShowAppHelp = showAppHelp
135-
ShowAppHelp is an action that displays the help
138+
var ShowAppHelp = ShowRootCommandHelp
139+
ShowAppHelp is a backward-compatible name for ShowRootCommandHelp.
140+
141+
var ShowAppHelpAndExit = ShowRootCommandHelpAndExit
142+
ShowAppHelpAndExit is a backward-compatible name for ShowRootCommandHelp.
136143

137-
var ShowCommandHelp = showCommandHelp
144+
var ShowCommandHelp = DefaultShowCommandHelp
138145
ShowCommandHelp prints help for the given command
139146

140-
var ShowSubcommandHelp = showSubcommandHelp
147+
var ShowRootCommandHelp = DefaultShowRootCommandHelp
148+
ShowRootCommandHelp is an action that displays help for the root command.
149+
150+
var ShowSubcommandHelp = DefaultShowSubcommandHelp
141151
ShowSubcommandHelp prints help for the given subcommand
142152

143153
var SubcommandHelpTemplate = `NAME:
@@ -162,37 +172,40 @@ OPTIONS:{{template "visibleFlagTemplate" .}}{{end}}
162172
cli.go uses text/template to render templates. You can render custom help
163173
text by setting this variable.
164174

165-
var VersionPrinter = printVersion
166-
VersionPrinter prints the version for the App
175+
var VersionPrinter = DefaultPrintVersion
176+
VersionPrinter prints the version for the root Command.
167177

168-
var HelpPrinter helpPrinter = printHelp
169-
HelpPrinter is a function that writes the help output. If not set
170-
explicitly, this calls HelpPrinterCustom using only the default template
171-
functions.
172178

173-
If custom logic for printing help is required, this function can be
174-
overridden. If the ExtraInfo field is defined on an App, this function
175-
should not be modified, as HelpPrinterCustom will be used directly in order
176-
to capture the extra information.
179+
FUNCTIONS
177180

178-
var HelpPrinterCustom helpPrinterCustom = printHelpCustom
179-
HelpPrinterCustom is a function that writes the help output. It is used as
180-
the default implementation of HelpPrinter, and may be called directly if the
181-
ExtraInfo field is set on an App.
181+
func DefaultCompleteWithFlags(ctx context.Context, cmd *Command)
182+
func DefaultPrintHelp(out io.Writer, templ string, data any)
183+
DefaultPrintHelp is the default implementation of HelpPrinter.
182184

183-
In the default implementation, if the customFuncs argument contains a
184-
"wrapAt" key, which is a function which takes no arguments and returns an
185-
int, this int value will be used to produce a "wrap" function used by the
186-
default template to wrap long lines.
185+
func DefaultPrintHelpCustom(out io.Writer, templ string, data any, customFuncs map[string]any)
186+
DefaultPrintHelpCustom is the default implementation of HelpPrinterCustom.
187187

188+
The customFuncs map will be combined with a default template.FuncMap to
189+
allow using arbitrary functions in template rendering.
188190

189-
FUNCTIONS
191+
func DefaultPrintVersion(cmd *Command)
192+
DefaultPrintVersion is the default implementation of VersionPrinter.
190193

191-
func DefaultAppComplete(ctx context.Context, cmd *Command)
192-
DefaultAppComplete prints the list of subcommands as the default app
193-
completion method
194+
func DefaultRootCommandComplete(ctx context.Context, cmd *Command)
195+
DefaultRootCommandComplete prints the list of subcommands as the default
196+
completion method.
197+
198+
func DefaultShowCommandHelp(ctx context.Context, cmd *Command, commandName string) error
199+
DefaultShowCommandHelp is the default implementation of ShowCommandHelp.
200+
201+
func DefaultShowRootCommandHelp(cmd *Command) error
202+
DefaultShowRootCommandHelp is the default implementation of
203+
ShowRootCommandHelp.
204+
205+
func DefaultShowSubcommandHelp(cmd *Command) error
206+
DefaultShowSubcommandHelp is the default implementation of
207+
ShowSubcommandHelp.
194208

195-
func DefaultCompleteWithFlags(ctx context.Context, cmd *Command)
196209
func FlagNames(name string, aliases []string) []string
197210
func HandleExitCoder(err error)
198211
HandleExitCoder handles errors implementing ExitCoder by printing their
@@ -202,21 +215,22 @@ func HandleExitCoder(err error)
202215
for the ExitCoder interface, and OsExiter will be called with the last exit
203216
code found, or exit code 1 if no ExitCoder is found.
204217

205-
This function is the default error-handling behavior for an App.
206-
207-
func ShowAppHelpAndExit(cmd *Command, exitCode int)
208-
ShowAppHelpAndExit - Prints the list of subcommands for the app and exits
209-
with exit code.
218+
This function is the default error-handling behavior for a Command.
210219

211220
func ShowCommandHelpAndExit(ctx context.Context, cmd *Command, command string, code int)
212-
ShowCommandHelpAndExit - exits with code after showing help
221+
ShowCommandHelpAndExit exits with code after showing help via
222+
ShowCommandHelp.
223+
224+
func ShowRootCommandHelpAndExit(cmd *Command, exitCode int)
225+
ShowRootCommandHelpAndExit prints the list of subcommands and exits with
226+
exit code.
213227

214228
func ShowSubcommandHelpAndExit(cmd *Command, exitCode int)
215-
ShowSubcommandHelpAndExit - Prints help for the given subcommand and exits
216-
with exit code.
229+
ShowSubcommandHelpAndExit prints help for the given subcommand via
230+
ShowSubcommandHelp and exits with exit code.
217231

218232
func ShowVersion(cmd *Command)
219-
ShowVersion prints the version number of the App
233+
ShowVersion prints the version number of the root Command.
220234

221235

222236
TYPES
@@ -472,9 +486,9 @@ type Command struct {
472486
Writer io.Writer `json:"-"`
473487
// ErrWriter writes error output
474488
ErrWriter io.Writer `json:"-"`
475-
// ExitErrHandler processes any error encountered while running an App before
476-
// it is returned to the caller. If no function is provided, HandleExitCoder
477-
// is used as the default behavior.
489+
// ExitErrHandler processes any error encountered while running a Command before it is
490+
// returned to the caller. If no function is provided, HandleExitCoder is used as the
491+
// default behavior.
478492
ExitErrHandler ExitErrHandlerFunc `json:"-"`
479493
// Other custom info
480494
Metadata map[string]interface{} `json:"metadata"`
@@ -687,7 +701,7 @@ func (c *Command) TimestampArg(name string) time.Time
687701
func (c *Command) TimestampArgs(name string) []time.Time
688702

689703
func (cmd *Command) ToFishCompletion() (string, error)
690-
ToFishCompletion creates a fish completion string for the `*App` The
704+
ToFishCompletion creates a fish completion string for the `*Command` The
691705
function errors if either parsing or writing of the string fails.
692706

693707
func (cmd *Command) Uint(name string) uint
@@ -847,16 +861,15 @@ type ExitCoder interface {
847861
error
848862
ExitCode() int
849863
}
850-
ExitCoder is the interface checked by `App` and `Command` for a custom exit
851-
code
864+
ExitCoder is the interface checked by `Command` for a custom exit code.
852865

853-
func Exit(message interface{}, exitCode int) ExitCoder
866+
func Exit(message any, exitCode int) ExitCoder
854867
Exit wraps a message and exit code into an error, which by default is
855868
handled with a call to os.Exit during default error handling.
856869

857-
This is the simplest way to trigger a non-zero exit code for an App
858-
without having to call os.Exit manually. During testing, this behavior
859-
can be avoided by overriding the ExitErrHandler function on an App or the
870+
This is the simplest way to trigger a non-zero exit code for a Command
871+
without having to call os.Exit manually. During testing, this behavior can
872+
be avoided by overriding the ExitErrHandler function on a Command or the
860873
package-global OsExiter function.
861874

862875
type ExitErrHandlerFunc func(context.Context, *Command, error)
@@ -1093,6 +1106,32 @@ type FloatSliceFlag = FlagBase[[]float64, NoConfig, FloatSlice]
10931106

10941107
type GenericFlag = FlagBase[Value, NoConfig, genericValue]
10951108

1109+
type HelpPrinterCustomFunc func(w io.Writer, templ string, data any, customFunc map[string]any)
1110+
Prints help for the Command with custom template function.
1111+
1112+
var HelpPrinterCustom HelpPrinterCustomFunc = DefaultPrintHelpCustom
1113+
HelpPrinterCustom is a function that writes the help output. It is used as
1114+
the default implementation of HelpPrinter, and may be called directly if the
1115+
ExtraInfo field is set on a Command.
1116+
1117+
In the default implementation, if the customFuncs argument contains a
1118+
"wrapAt" key, which is a function which takes no arguments and returns an
1119+
int, this int value will be used to produce a "wrap" function used by the
1120+
default template to wrap long lines.
1121+
1122+
type HelpPrinterFunc func(w io.Writer, templ string, data any)
1123+
HelpPrinterFunc prints help for the Command.
1124+
1125+
var HelpPrinter HelpPrinterFunc = DefaultPrintHelp
1126+
HelpPrinter is a function that writes the help output. If not set
1127+
explicitly, this calls HelpPrinterCustom using only the default template
1128+
functions.
1129+
1130+
If custom logic for printing help is required, this function can be
1131+
overridden. If the ExtraInfo field is defined on a Command, this function
1132+
should not be modified, as HelpPrinterCustom will be used directly in order
1133+
to capture the extra information.
1134+
10961135
type Int16Arg = ArgumentBase[int16, IntegerConfig, intValue[int16]]
10971136

10981137
type Int16Args = ArgumentsBase[int16, IntegerConfig, intValue[int16]]

0 commit comments

Comments
 (0)