File tree Expand file tree Collapse file tree 4 files changed +35
-0
lines changed Expand file tree Collapse file tree 4 files changed +35
-0
lines changed Original file line number Diff line number Diff line change @@ -44,6 +44,8 @@ type Command struct {
44
44
Usage string
45
45
// A longer explanation of how the command works
46
46
Description string
47
+ // or a function responsible to render the description
48
+ DescriptionFunc DescriptionFunc
47
49
// The category the command is part of
48
50
Category string
49
51
// An action to execute before any sub-subcommands are run, but after the context is ready
Original file line number Diff line number Diff line change @@ -33,6 +33,10 @@ type ActionFunc func(*Context) error
33
33
// CommandNotFoundFunc is executed if the proper command cannot be found
34
34
type CommandNotFoundFunc func (* Context , string ) error
35
35
36
+ // DescriptionFunc is used by the help generation to display a description when
37
+ // its computation is intensive or needs runtime information
38
+ type DescriptionFunc func (* Command , * Application ) string
39
+
36
40
// FlagStringFunc is used by the help generation to display a flag, which is
37
41
// expected to be a single line.
38
42
type FlagStringFunc func (Flag ) string
Original file line number Diff line number Diff line change @@ -149,6 +149,10 @@ func ShowAppHelp(c *Context) error {
149
149
// ShowCommandHelp prints help for the given command
150
150
func ShowCommandHelp (ctx * Context , command string ) error {
151
151
if c := ctx .App .Command (command ); c != nil {
152
+ if c .DescriptionFunc != nil {
153
+ c .Description = c .DescriptionFunc (c , ctx .App )
154
+ }
155
+
152
156
HelpPrinter (ctx .App .Writer , CommandHelpTemplate , c )
153
157
return nil
154
158
}
Original file line number Diff line number Diff line change @@ -237,6 +237,31 @@ func TestShowCommandHelp_CommandAliases(t *testing.T) {
237
237
}
238
238
}
239
239
240
+ func TestShowCommandHelp_DescriptionFunc (t * testing.T ) {
241
+ app := & Application {
242
+ Commands : []* Command {
243
+ {
244
+ Name : "frobbly" ,
245
+ Description : "this is not my custom description" ,
246
+ DescriptionFunc : func (* Command , * Application ) string {
247
+ return "this is my custom description"
248
+ },
249
+ Action : func (ctx * Context ) error {
250
+ return nil
251
+ },
252
+ },
253
+ },
254
+ }
255
+
256
+ output := & bytes.Buffer {}
257
+ app .Writer = output
258
+ app .Run ([]string {"foo" , "help" , "frobbly" })
259
+
260
+ if ! strings .Contains (output .String (), "this is my custom description" ) {
261
+ t .Errorf ("expected output to include result of DescriptionFunc; got: %q" , output .String ())
262
+ }
263
+ }
264
+
240
265
func TestShowAppHelp_HiddenCommand (t * testing.T ) {
241
266
app := & Application {
242
267
Commands : []* Command {
You can’t perform that action at this time.
0 commit comments