Skip to content

Commit 41334d4

Browse files
authored
Merge pull request #2196 from Maks1mS/custom-usage-help
feat: add ability to override usage text of default help command
2 parents b2257f8 + cb91e73 commit 41334d4

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

godoc-current.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ var AnyArguments = []Argument{
5757
}
5858
AnyArguments to differentiate between no arguments(nil) vs aleast one
5959

60+
var ArgsUsageCommandHelp = "[command]"
61+
ArgsUsageCommandHelp is a short description of the arguments of the help
62+
command
63+
6064
var CommandHelpTemplate = `NAME:
6165
{{template "helpNameTemplate" .}}
6266

@@ -172,6 +176,10 @@ OPTIONS:{{template "visibleFlagTemplate" .}}{{end}}
172176
cli.go uses text/template to render templates. You can render custom help
173177
text by setting this variable.
174178

179+
var UsageCommandHelp = "Shows a list of commands or help for one command"
180+
UsageCommandHelp is the text to override the USAGE section of the help
181+
command
182+
175183
var VersionPrinter = DefaultPrintVersion
176184
VersionPrinter prints the version for the root Command.
177185

help.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,18 @@ var ShowCommandHelp = DefaultShowCommandHelp
5656
// ShowSubcommandHelp prints help for the given subcommand
5757
var ShowSubcommandHelp = DefaultShowSubcommandHelp
5858

59+
// UsageCommandHelp is the text to override the USAGE section of the help command
60+
var UsageCommandHelp = "Shows a list of commands or help for one command"
61+
62+
// ArgsUsageCommandHelp is a short description of the arguments of the help command
63+
var ArgsUsageCommandHelp = "[command]"
64+
5965
func buildHelpCommand(withAction bool) *Command {
6066
cmd := &Command{
6167
Name: helpName,
6268
Aliases: []string{helpAlias},
63-
Usage: "Shows a list of commands or help for one command",
64-
ArgsUsage: "[command]",
69+
Usage: UsageCommandHelp,
70+
ArgsUsage: ArgsUsageCommandHelp,
6571
HideHelp: true,
6672
}
6773

help_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2009,3 +2009,26 @@ func TestPrintHelpCustomTemplateError(t *testing.T) {
20092009
*tmpl = oldtmpl
20102010
}
20112011
}
2012+
2013+
func TestCustomUsageCommandHelp(t *testing.T) {
2014+
old := UsageCommandHelp
2015+
defer func() { UsageCommandHelp = old }()
2016+
2017+
UsageCommandHelp = "Custom usage help command"
2018+
2019+
out := &bytes.Buffer{}
2020+
cmd := &Command{
2021+
Name: "app",
2022+
Commands: []*Command{
2023+
{
2024+
Name: "cmd",
2025+
Commands: []*Command{{Name: "subcmd"}},
2026+
},
2027+
},
2028+
Writer: out,
2029+
ErrWriter: out,
2030+
}
2031+
2032+
_ = cmd.Run(buildTestContext(t), []string{"app", "help"})
2033+
assert.Contains(t, out.String(), UsageCommandHelp)
2034+
}

testdata/godoc-v3.x.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ var AnyArguments = []Argument{
5757
}
5858
AnyArguments to differentiate between no arguments(nil) vs aleast one
5959

60+
var ArgsUsageCommandHelp = "[command]"
61+
ArgsUsageCommandHelp is a short description of the arguments of the help
62+
command
63+
6064
var CommandHelpTemplate = `NAME:
6165
{{template "helpNameTemplate" .}}
6266

@@ -172,6 +176,10 @@ OPTIONS:{{template "visibleFlagTemplate" .}}{{end}}
172176
cli.go uses text/template to render templates. You can render custom help
173177
text by setting this variable.
174178

179+
var UsageCommandHelp = "Shows a list of commands or help for one command"
180+
UsageCommandHelp is the text to override the USAGE section of the help
181+
command
182+
175183
var VersionPrinter = DefaultPrintVersion
176184
VersionPrinter prints the version for the root Command.
177185

0 commit comments

Comments
 (0)