Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix issue by adding custom replaceAll function and replacing all newlines by newlines+spaces in command usage description
  • Loading branch information
kchugalinskiy committed Aug 18, 2021
1 parent 58d113d commit 38c5f1f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
9 changes: 5 additions & 4 deletions help.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,11 @@ func ShowCommandCompletions(ctx *Context, command string) {
// allow using arbitrary functions in template rendering.
func printHelpCustom(out io.Writer, templ string, data interface{}, customFuncs map[string]interface{}) {
funcMap := template.FuncMap{
"join": strings.Join,
"indent": indent,
"nindent": nindent,
"trim": strings.TrimSpace,
"join": strings.Join,
"indent": indent,
"nindent": nindent,
"trim": strings.TrimSpace,
"replaceAll": strings.ReplaceAll,
}
for key, value := range customFuncs {
funcMap[key] = value
Expand Down
39 changes: 39 additions & 0 deletions help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,29 @@ func TestShowCommandHelp_HelpPrinter(t *testing.T) {
wantTemplate: SubcommandHelpTemplate,
wantOutput: "yo",
},
{
name: "no-command-full",
template: "",
printer: HelpPrinter,
command: "",
wantTemplate: SubcommandHelpTemplate,
wantOutput: `NAME:
my-app - A new cli application
USAGE:
my-app command [command options] [arguments...]
COMMANDS:
my-command lorem ipsum
dolor sit
amet
help, h Shows a list of commands or help for one command
OPTIONS:
--help, -h show help (default: false)
`,
},
{
name: "standard-command",
template: "",
Expand All @@ -260,6 +283,21 @@ func TestShowCommandHelp_HelpPrinter(t *testing.T) {
wantTemplate: CommandHelpTemplate,
wantOutput: "yo",
},
{
name: "standard-command-full",
template: "",
printer: HelpPrinter,
command: "my-command",
wantTemplate: CommandHelpTemplate,
wantOutput: `NAME:
my-app my-command - lorem ipsum
dolor sit
amet
USAGE:
my-app my-command [arguments...]
`,
},
{
name: "custom-template-command",
template: "{{doublecho .Name}}",
Expand Down Expand Up @@ -294,6 +332,7 @@ func TestShowCommandHelp_HelpPrinter(t *testing.T) {
Commands: []*Command{
{
Name: "my-command",
Usage: "lorem ipsum\ndolor sit\namet",
CustomHelpTemplate: tt.template,
},
},
Expand Down
10 changes: 5 additions & 5 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ AUTHOR{{with $length := len .Authors}}{{if ne 1 $length}}S{{end}}{{end}}:
COMMANDS:{{range .VisibleCategories}}{{if .Name}}
{{.Name}}:{{range .VisibleCommands}}
{{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}}{{else}}{{range .VisibleCommands}}
{{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}}{{end}}{{end}}{{end}}{{if .VisibleFlags}}
{{join .Names ", "}}{{"\t"}}{{replaceAll .Usage "\n" "\n "}}{{end}}{{else}}{{range .VisibleCommands}}
{{join .Names ", "}}{{"\t"}}{{replaceAll .Usage "\n" "\n "}}{{end}}{{end}}{{end}}{{end}}{{if .VisibleFlags}}
GLOBAL OPTIONS:
{{range $index, $option := .VisibleFlags}}{{if $index}}
Expand All @@ -36,7 +36,7 @@ COPYRIGHT:
// cli.go uses text/template to render templates. You can
// render custom help text by setting this variable.
var CommandHelpTemplate = `NAME:
{{.HelpName}} - {{.Usage}}
{{.HelpName}} - {{replaceAll .Usage "\n" "\n "}}
USAGE:
{{if .UsageText}}{{.UsageText | nindent 3 | trim}}{{else}}{{.HelpName}}{{if .VisibleFlags}} [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{end}}{{if .Category}}
Expand Down Expand Up @@ -66,8 +66,8 @@ DESCRIPTION:
COMMANDS:{{range .VisibleCategories}}{{if .Name}}
{{.Name}}:{{range .VisibleCommands}}
{{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}}{{else}}{{range .VisibleCommands}}
{{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}}{{end}}{{end}}{{if .VisibleFlags}}
{{join .Names ", "}}{{"\t"}}{{replaceAll .Usage "\n" "\n "}}{{end}}{{else}}{{range .VisibleCommands}}
{{join .Names ", "}}{{"\t"}}{{replaceAll .Usage "\n" "\n "}}{{end}}{{end}}{{end}}{{if .VisibleFlags}}
OPTIONS:
{{range .VisibleFlags}}{{.}}
Expand Down

0 comments on commit 38c5f1f

Please sign in to comment.