Repository: urfave/cli
Pull request #1489 (2022-09-11 14:24:26)

Fix: Help name consistency among app/commands and subcommands
<!--
  This template provides some ideas of things to include in your PR description.
  To start, try providing a short summary of your changes in the Title above.
  If a section of the PR template does not apply to this PR, then delete that section.
 -->

## What type of PR is this?

_(REQUIRED)_

<!--
  Delete any of the following that do not apply:
 -->

- bug

## What this PR does / why we need it:

_(REQUIRED)_

<!--
  What goal is this change working towards?
  Provide a bullet pointed summary of how each file was changed.
  Briefly explain any decisions you made with respect to the changes.
  Include anything here that you didn't include in *Release Notes*
  above, such as changes to CI or changes to internal methods.
-->

## Which issue(s) this PR fixes:

_(REQUIRED)_

<!--
If this PR fixes one of more issues, list them here.
One line each, like so:

Fixes #123
Fixes #39
-->

## Special notes for your reviewer:

_(fill-in or delete this section)_

<!--
   Is there any particular feedback you would / wouldn't like?
   Which parts of the code should reviewers focus on?
-->

## Testing

_(fill-in or delete this section)_
<!--
  Describe how you tested this change.
-->

Added new test case

 go test -run=TestHelpNameConsistency

## Release Notes

_(REQUIRED)_
<!--
  If this PR makes user facing changes, please describe them here. This
  description will be copied into the release notes/changelog, whenever the
  next version is released. Keep this section short, and focus on high level
  changes.

  Put your text between the block. To omit notes, use NONE within the block.
-->

```release-note

```



Patch:
diff --git a/app.go b/app.go
index 1d2f6e8..2ffacd5 100644
--- a/app.go
+++ b/app.go
@@ -133,7 +133,6 @@ func compileTime() time.Time {
 func NewApp() *App {
 	return &App{
 		Name:         filepath.Base(os.Args[0]),
-		HelpName:     filepath.Base(os.Args[0]),
 		Usage:        "A new cli application",
 		UsageText:    "",
 		BashComplete: DefaultAppComplete,


Test patch:
diff --git a/help_test.go b/help_test.go
index 9422b4a..ade6f3d 100644
--- a/help_test.go
+++ b/help_test.go
@@ -428,6 +428,58 @@ func TestShowCommandHelp_CommandAliases(t *testing.T) {
 	}
 }
 
+func TestHelpNameConsistency(t *testing.T) {
+	// Setup some very basic templates based on actual AppHelp, CommandHelp
+	// and SubcommandHelp templates to display the help name
+	// The inconsistency shows up when users use NewApp() as opposed to
+	// using App{...} directly
+	SubcommandHelpTemplate = `{{.HelpName}}`
+	app := NewApp()
+	app.Name = "bar"
+	app.CustomAppHelpTemplate = `{{.HelpName}}`
+	app.Commands = []*Command{
+		{
+			Name:               "command1",
+			CustomHelpTemplate: `{{.HelpName}}`,
+			Subcommands: []*Command{
+				{
+					Name:               "subcommand1",
+					CustomHelpTemplate: `{{.HelpName}}`,
+				},
+			},
+		},
+	}
+
+	tests := []struct {
+		name string
+		args []string
+	}{
+		{
+			name: "App help",
+			args: []string{"foo"},
+		},
+		{
+			name: "Command help",
+			args: []string{"foo", "command1"},
+		},
+		{
+			name: "Subcommand help",
+			args: []string{"foo", "command1", "subcommand1"},
+		},
+	}
+
+	for _, tt := range tests {
+		output := &bytes.Buffer{}
+		app.Writer = output
+		if err := app.Run(tt.args); err != nil {
+			t.Error(err)
+		}
+		if !strings.Contains(output.String(), "bar") {
+			t.Errorf("expected output to contain bar; got: %q", output.String())
+		}
+	}
+}
+
 func TestShowSubcommandHelp_CommandAliases(t *testing.T) {
 	app := &App{
 		Commands: []*Command{