Skip to content

Commit 4351e24

Browse files
authored
Merge pull request #10 from tucksaun/feat/description-func
Adds a new hook to tweak command description at runtime
2 parents 03d8cd7 + 2a5f1c8 commit 4351e24

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

command.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ type Command struct {
4444
Usage string
4545
// A longer explanation of how the command works
4646
Description string
47+
// or a function responsible to render the description
48+
DescriptionFunc DescriptionFunc
4749
// The category the command is part of
4850
Category string
4951
// An action to execute before any sub-subcommands are run, but after the context is ready

funcs.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ type ActionFunc func(*Context) error
3333
// CommandNotFoundFunc is executed if the proper command cannot be found
3434
type CommandNotFoundFunc func(*Context, string) error
3535

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+
3640
// FlagStringFunc is used by the help generation to display a flag, which is
3741
// expected to be a single line.
3842
type FlagStringFunc func(Flag) string

help.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ func ShowAppHelp(c *Context) error {
149149
// ShowCommandHelp prints help for the given command
150150
func ShowCommandHelp(ctx *Context, command string) error {
151151
if c := ctx.App.Command(command); c != nil {
152+
if c.DescriptionFunc != nil {
153+
c.Description = c.DescriptionFunc(c, ctx.App)
154+
}
155+
152156
HelpPrinter(ctx.App.Writer, CommandHelpTemplate, c)
153157
return nil
154158
}

help_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,31 @@ func TestShowCommandHelp_CommandAliases(t *testing.T) {
237237
}
238238
}
239239

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+
240265
func TestShowAppHelp_HiddenCommand(t *testing.T) {
241266
app := &Application{
242267
Commands: []*Command{

0 commit comments

Comments
 (0)