From 71379de70f8832fdb00bf9d16f4945b3ada79f7e Mon Sep 17 00:00:00 2001 From: Derek Smith Date: Wed, 2 Jun 2021 12:47:27 -0500 Subject: [PATCH] fix(UsageText): consistent indent for help UsageText output Signed-off-by: Derek Smith --- help_test.go | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++ template.go | 6 +-- 2 files changed, 117 insertions(+), 3 deletions(-) diff --git a/help_test.go b/help_test.go index 407c269173..8dd262d56c 100644 --- a/help_test.go +++ b/help_test.go @@ -511,6 +511,36 @@ func TestShowSubcommandHelp_CommandUsageText(t *testing.T) { } } +func TestShowSubcommandHelp_MultiLine_CommandUsageText(t *testing.T) { + app := &App{ + Commands: []*Command{ + { + Name: "frobbly", + UsageText: `This is a +multi +line +UsageText`, + }, + }, + } + + output := &bytes.Buffer{} + app.Writer = output + + _ = app.Run([]string{"foo", "frobbly", "--help"}) + + expected := `USAGE: + This is a + multi + line + UsageText +` + + if !strings.Contains(output.String(), expected) { + t.Errorf("expected output to include usage text; got: %q", output.String()) + } +} + func TestShowSubcommandHelp_SubcommandUsageText(t *testing.T) { app := &App{ Commands: []*Command{ @@ -535,6 +565,40 @@ func TestShowSubcommandHelp_SubcommandUsageText(t *testing.T) { } } +func TestShowSubcommandHelp_MultiLine_SubcommandUsageText(t *testing.T) { + app := &App{ + Commands: []*Command{ + { + Name: "frobbly", + Subcommands: []*Command{ + { + Name: "bobbly", + UsageText: `This is a +multi +line +UsageText`, + }, + }, + }, + }, + } + + output := &bytes.Buffer{} + app.Writer = output + _ = app.Run([]string{"foo", "frobbly", "bobbly", "--help"}) + + expected := `USAGE: + This is a + multi + line + UsageText +` + + if !strings.Contains(output.String(), expected) { + t.Errorf("expected output to include usage text; got: %q", output.String()) + } +} + func TestShowAppHelp_HiddenCommand(t *testing.T) { app := &App{ Commands: []*Command{ @@ -780,6 +844,56 @@ VERSION: } } +func TestShowAppHelp_UsageText(t *testing.T) { + app := &App{ + UsageText: "This is a sinlge line of UsageText", + Commands: []*Command{ + { + Name: "frobbly", + }, + }, + } + + output := &bytes.Buffer{} + app.Writer = output + + _ = app.Run([]string{"foo"}) + + if !strings.Contains(output.String(), "This is a sinlge line of UsageText") { + t.Errorf("expected output to include usage text; got: %q", output.String()) + } +} + +func TestShowAppHelp_MultiLine_UsageText(t *testing.T) { + app := &App{ + UsageText: `This is a +multi +line +App UsageText`, + Commands: []*Command{ + { + Name: "frobbly", + }, + }, + } + + output := &bytes.Buffer{} + app.Writer = output + + _ = app.Run([]string{"foo"}) + + expected := `USAGE: + This is a + multi + line + App UsageText +` + + if !strings.Contains(output.String(), expected) { + t.Errorf("expected output to include usage text; got: %q", output.String()) + } +} + func TestHideHelpCommand(t *testing.T) { app := &App{ HideHelpCommand: true, diff --git a/template.go b/template.go index 317cc8817d..69e040e7fb 100644 --- a/template.go +++ b/template.go @@ -7,7 +7,7 @@ var AppHelpTemplate = `NAME: {{.Name}}{{if .Usage}} - {{.Usage}}{{end}} USAGE: - {{if .UsageText}}{{.UsageText}}{{else}}{{.HelpName}} {{if .VisibleFlags}}[global options]{{end}}{{if .Commands}} command [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{end}}{{if .Version}}{{if not .HideVersion}} + {{if .UsageText}}{{.UsageText | nindent 3 | trim}}{{else}}{{.HelpName}} {{if .VisibleFlags}}[global options]{{end}}{{if .Commands}} command [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{end}}{{if .Version}}{{if not .HideVersion}} VERSION: {{.Version}}{{end}}{{end}}{{if .Description}} @@ -39,7 +39,7 @@ var CommandHelpTemplate = `NAME: {{.HelpName}} - {{.Usage}} USAGE: - {{if .UsageText}}{{.UsageText}}{{else}}{{.HelpName}}{{if .VisibleFlags}} [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{end}}{{if .Category}} + {{if .UsageText}}{{.UsageText | nindent 3 | trim}}{{else}}{{.HelpName}}{{if .VisibleFlags}} [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{end}}{{if .Category}} CATEGORY: {{.Category}}{{end}}{{if .Description}} @@ -59,7 +59,7 @@ var SubcommandHelpTemplate = `NAME: {{.HelpName}} - {{.Usage}} USAGE: - {{if .UsageText}}{{.UsageText}}{{else}}{{.HelpName}} command{{if .VisibleFlags}} [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{end}}{{if .Description}} + {{if .UsageText}}{{.UsageText | nindent 3 | trim}}{{else}}{{.HelpName}} command{{if .VisibleFlags}} [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{end}}{{if .Description}} DESCRIPTION: {{.Description | nindent 3 | trim}}{{end}}