Skip to content

Commit 3de689e

Browse files
committed
feat: add ability to override usage text of default help command
1 parent 39c9ea0 commit 3de689e

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
@@ -2010,3 +2010,26 @@ func TestPrintHelpCustomTemplateError(t *testing.T) {
20102010
*tmpl = oldtmpl
20112011
}
20122012
}
2013+
2014+
func TestCustomUsageCommandHelp(t *testing.T) {
2015+
old := UsageCommandHelp
2016+
defer func() { UsageCommandHelp = old }()
2017+
2018+
UsageCommandHelp = "Custom usage help command"
2019+
2020+
out := &bytes.Buffer{}
2021+
cmd := &Command{
2022+
Name: "app",
2023+
Commands: []*Command{
2024+
{
2025+
Name: "cmd",
2026+
Commands: []*Command{{Name: "subcmd"}},
2027+
},
2028+
},
2029+
Writer: out,
2030+
ErrWriter: out,
2031+
}
2032+
2033+
_ = cmd.Run(buildTestContext(t), []string{"app", "help"})
2034+
assert.Contains(t, out.String(), UsageCommandHelp)
2035+
}

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)