Skip to content

Adds a new hook to tweak command description at runtime #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ type Command struct {
Usage string
// A longer explanation of how the command works
Description string
// or a function responsible to render the description
DescriptionFunc DescriptionFunc
// The category the command is part of
Category string
// An action to execute before any sub-subcommands are run, but after the context is ready
Expand Down
4 changes: 4 additions & 0 deletions funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ type ActionFunc func(*Context) error
// CommandNotFoundFunc is executed if the proper command cannot be found
type CommandNotFoundFunc func(*Context, string) error

// DescriptionFunc is used by the help generation to display a description when
// its computation is intensive or needs runtime information
type DescriptionFunc func(*Command, *Application) string

// FlagStringFunc is used by the help generation to display a flag, which is
// expected to be a single line.
type FlagStringFunc func(Flag) string
4 changes: 4 additions & 0 deletions help.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ func ShowAppHelp(c *Context) error {
// ShowCommandHelp prints help for the given command
func ShowCommandHelp(ctx *Context, command string) error {
if c := ctx.App.Command(command); c != nil {
if c.DescriptionFunc != nil {
c.Description = c.DescriptionFunc(c, ctx.App)
}

HelpPrinter(ctx.App.Writer, CommandHelpTemplate, c)
return nil
}
Expand Down
25 changes: 25 additions & 0 deletions help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,31 @@ func TestShowCommandHelp_CommandAliases(t *testing.T) {
}
}

func TestShowCommandHelp_DescriptionFunc(t *testing.T) {
app := &Application{
Commands: []*Command{
{
Name: "frobbly",
Description: "this is not my custom description",
DescriptionFunc: func(*Command, *Application) string {
return "this is my custom description"
},
Action: func(ctx *Context) error {
return nil
},
},
},
}

output := &bytes.Buffer{}
app.Writer = output
app.Run([]string{"foo", "help", "frobbly"})

if !strings.Contains(output.String(), "this is my custom description") {
t.Errorf("expected output to include result of DescriptionFunc; got: %q", output.String())
}
}

func TestShowAppHelp_HiddenCommand(t *testing.T) {
app := &Application{
Commands: []*Command{
Expand Down
Loading