From 8bd5a5381f656924b3bd2804e3e13eb368f5123c Mon Sep 17 00:00:00 2001 From: Randy Stauner Date: Wed, 2 Oct 2019 19:32:16 -0700 Subject: [PATCH] Add ArgDescriptionForUseLine to add custom suffix to use line --- command.go | 8 ++++++++ command_test.go | 10 ++++++++++ 2 files changed, 18 insertions(+) diff --git a/command.go b/command.go index 42e500de..b02175c1 100644 --- a/command.go +++ b/command.go @@ -68,6 +68,11 @@ type Command struct { // but accepted if entered manually. ArgAliases []string + // ArgDescriptionForUseLine will be appended to the usage line of a command + // line of a command when printing help or generating docs + // to allow you to describe the meaning of the arguments the command takes. + ArgDescriptionForUseLine string + // BashCompletionFunction is custom functions used by the bash autocompletion generator. BashCompletionFunction string @@ -1180,6 +1185,9 @@ func (c *Command) UseLine() string { if c.HasAvailableFlags() && !strings.Contains(useline, "[flags]") { useline += " [flags]" } + if c.ArgDescriptionForUseLine != "" { + useline += " " + c.ArgDescriptionForUseLine + } return useline } diff --git a/command_test.go b/command_test.go index b26bd4ab..f439392a 100644 --- a/command_test.go +++ b/command_test.go @@ -830,6 +830,16 @@ func TestFlagsInUsage(t *testing.T) { checkStringContains(t, output, "[flags]") } +func TestArgsInUsage(t *testing.T) { + rootCmd := &Command{Use: "root", Args: NoArgs, ArgDescriptionForUseLine: "name [arg...]", Run: func(*Command, []string) {}} + output, err := executeCommand(rootCmd, "--help") + if err != nil { + t.Errorf("Unexpected error: %v", err) + } + + checkStringContains(t, output, "[flags] name [arg...]") +} + func TestHelpExecutedOnNonRunnableChild(t *testing.T) { rootCmd := &Command{Use: "root", Run: emptyRun} childCmd := &Command{Use: "child", Long: "Long description"}