From 95eb9edaa3995de357ea07d64579dc4b68b105ea Mon Sep 17 00:00:00 2001 From: Simon Barendse Date: Thu, 4 Jul 2019 15:25:14 +0200 Subject: [PATCH] Add a long command help text to the usage template context This can for example be used to display a longer help text in the command-specific help than in the apps root help. --- cmd.go | 9 +++++++++ model.go | 2 ++ usage_test.go | 14 ++++++++++++++ 3 files changed, 25 insertions(+) diff --git a/cmd.go b/cmd.go index a19b6b0..7acf8c5 100644 --- a/cmd.go +++ b/cmd.go @@ -221,6 +221,7 @@ type CmdClause struct { name string aliases []string help string + helpLong string isDefault bool validator CmdClauseValidator hidden bool @@ -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 +} diff --git a/model.go b/model.go index d6e65e1..6015904 100644 --- a/model.go +++ b/model.go @@ -137,6 +137,7 @@ type CmdModel struct { Name string Aliases []string Help string + HelpLong string FullCommand string Depth int Hidden bool @@ -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, diff --git a/usage_test.go b/usage_test.go index 4d59e27..968f66a 100644 --- a/usage_test.go +++ b/usage_test.go @@ -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) +}