Skip to content

Commit 375e5df

Browse files
authored
Merge pull request #1489 from dearchap/fix_help_name_consistency
Fix: Help name consistency among app/commands and subcommands
2 parents 8dba5c3 + 8339b59 commit 375e5df

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

app.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ func compileTime() time.Time {
133133
func NewApp() *App {
134134
return &App{
135135
Name: filepath.Base(os.Args[0]),
136-
HelpName: filepath.Base(os.Args[0]),
137136
Usage: "A new cli application",
138137
UsageText: "",
139138
BashComplete: DefaultAppComplete,

help_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,58 @@ func TestShowCommandHelp_CommandAliases(t *testing.T) {
428428
}
429429
}
430430

431+
func TestHelpNameConsistency(t *testing.T) {
432+
// Setup some very basic templates based on actual AppHelp, CommandHelp
433+
// and SubcommandHelp templates to display the help name
434+
// The inconsistency shows up when users use NewApp() as opposed to
435+
// using App{...} directly
436+
SubcommandHelpTemplate = `{{.HelpName}}`
437+
app := NewApp()
438+
app.Name = "bar"
439+
app.CustomAppHelpTemplate = `{{.HelpName}}`
440+
app.Commands = []*Command{
441+
{
442+
Name: "command1",
443+
CustomHelpTemplate: `{{.HelpName}}`,
444+
Subcommands: []*Command{
445+
{
446+
Name: "subcommand1",
447+
CustomHelpTemplate: `{{.HelpName}}`,
448+
},
449+
},
450+
},
451+
}
452+
453+
tests := []struct {
454+
name string
455+
args []string
456+
}{
457+
{
458+
name: "App help",
459+
args: []string{"foo"},
460+
},
461+
{
462+
name: "Command help",
463+
args: []string{"foo", "command1"},
464+
},
465+
{
466+
name: "Subcommand help",
467+
args: []string{"foo", "command1", "subcommand1"},
468+
},
469+
}
470+
471+
for _, tt := range tests {
472+
output := &bytes.Buffer{}
473+
app.Writer = output
474+
if err := app.Run(tt.args); err != nil {
475+
t.Error(err)
476+
}
477+
if !strings.Contains(output.String(), "bar") {
478+
t.Errorf("expected output to contain bar; got: %q", output.String())
479+
}
480+
}
481+
}
482+
431483
func TestShowSubcommandHelp_CommandAliases(t *testing.T) {
432484
app := &App{
433485
Commands: []*Command{

0 commit comments

Comments
 (0)