Add ArgDescriptionForUseLine to add custom suffix to use line

This commit is contained in:
Randy Stauner 2019-10-02 19:32:16 -07:00
parent 19cf35ea77
commit 8bd5a5381f
2 changed files with 18 additions and 0 deletions

View file

@ -68,6 +68,11 @@ type Command struct {
// but accepted if entered manually. // but accepted if entered manually.
ArgAliases []string 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 is custom functions used by the bash autocompletion generator.
BashCompletionFunction string BashCompletionFunction string
@ -1180,6 +1185,9 @@ func (c *Command) UseLine() string {
if c.HasAvailableFlags() && !strings.Contains(useline, "[flags]") { if c.HasAvailableFlags() && !strings.Contains(useline, "[flags]") {
useline += " [flags]" useline += " [flags]"
} }
if c.ArgDescriptionForUseLine != "" {
useline += " " + c.ArgDescriptionForUseLine
}
return useline return useline
} }

View file

@ -830,6 +830,16 @@ func TestFlagsInUsage(t *testing.T) {
checkStringContains(t, output, "[flags]") 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) { func TestHelpExecutedOnNonRunnableChild(t *testing.T) {
rootCmd := &Command{Use: "root", Run: emptyRun} rootCmd := &Command{Use: "root", Run: emptyRun}
childCmd := &Command{Use: "child", Long: "Long description"} childCmd := &Command{Use: "child", Long: "Long description"}