Skip to content

Commit

Permalink
Add a long command help text to the usage template context
Browse files Browse the repository at this point in the history
This can for example be used to display a longer help text in the
command-specific help than in the apps root help.
  • Loading branch information
SimonBarendse authored and alecthomas committed Jul 5, 2019
1 parent 5ca6d1a commit 95eb9ed
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
9 changes: 9 additions & 0 deletions cmd.go
Expand Up @@ -221,6 +221,7 @@ type CmdClause struct {
name string
aliases []string
help string
helpLong string
isDefault bool
validator CmdClauseValidator
hidden bool
Expand Down Expand Up @@ -302,3 +303,11 @@ func (c *CmdClause) Hidden() *CmdClause {
c.hidden = true
return c
}

// HelpLong adds a long help text, which can be used in usage templates.
// For example, to use a longer help text in the command-specific help
// than in the apps root help.
func (c *CmdClause) HelpLong(help string) *CmdClause {
c.helpLong = help
return c
}
2 changes: 2 additions & 0 deletions model.go
Expand Up @@ -137,6 +137,7 @@ type CmdModel struct {
Name string
Aliases []string
Help string
HelpLong string
FullCommand string
Depth int
Hidden bool
Expand Down Expand Up @@ -230,6 +231,7 @@ func (c *CmdClause) Model() *CmdModel {
Name: c.name,
Aliases: c.aliases,
Help: c.help,
HelpLong: c.helpLong,
Depth: depth,
Hidden: c.hidden,
Default: c.isDefault,
Expand Down
14 changes: 14 additions & 0 deletions usage_test.go
Expand Up @@ -77,3 +77,17 @@ func TestUsageFuncs(t *testing.T) {
usage := buf.String()
assert.Equal(t, "3", usage)
}

func TestCmdClause_HelpLong(t *testing.T) {
var buf bytes.Buffer
tpl := `{{define "FormatUsage"}}{{.HelpLong}}{{end}}\
{{template "FormatUsage" .Context.SelectedCommand}}`

a := New("test", "Test").Writer(&buf).Terminate(nil)
a.UsageTemplate(tpl)
a.Command("command", "short help text").HelpLong("long help text")

a.Parse([]string{"command", "--help"})
usage := buf.String()
assert.Equal(t, "long help text", usage)
}

0 comments on commit 95eb9ed

Please sign in to comment.