Skip to content
This repository was archived by the owner on Oct 17, 2021. It is now read-only.

Commit 2e2a923

Browse files
author
Faris Huskovic
committed
Update help output
1 parent fea394f commit 2e2a923

File tree

2 files changed

+73
-7
lines changed

2 files changed

+73
-7
lines changed

command_test.go

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,23 @@ const (
1414

1515
expectedParentCmdHelpOutput = `Usage: mockParentCmd Mock parent command usage.
1616
17-
Mock parent command description.
17+
Description: Mock parent command description.
1818
1919
Commands:
20-
s,sc,sub,mockSubCmd - A simple mock subcommand with aliases.
20+
s, sc, sub, mockSubCmd - A simple mock subcommand with aliases and its own subcommand.
2121
`
2222
)
2323

2424
var subCmd = &mockSubCmd{buf: new(bytes.Buffer)}
2525

2626
type (
27+
// Mock for root/parent command.
2728
mockParentCmd struct{}
28-
mockSubCmd struct{ buf *bytes.Buffer }
29+
// Mock subcommand with aliases and a nested
30+
// sucommand of its own.
31+
mockSubCmd struct{ buf *bytes.Buffer }
32+
// Mock subcommand with aliases and no nested subcommands.
33+
mockSubCmdNoNested struct{}
2934
)
3035

3136
func (c *mockParentCmd) Run(fl *pflag.FlagSet) {}
@@ -47,14 +52,29 @@ func (c *mockSubCmd) Run(fl *pflag.FlagSet) {
4752
}
4853
}
4954

55+
func (c *mockSubCmd) Subcommands() []Command {
56+
return []Command{new(mockSubCmd)}
57+
}
58+
5059
func (c *mockSubCmd) Write(b []byte) (int, error) { return c.buf.Write(b) }
5160

5261
func (c *mockSubCmd) Spec() CommandSpec {
5362
return CommandSpec{
5463
Name: "mockSubCmd",
5564
Usage: "Test a subcommand.",
5665
Aliases: []string{"s", "sc", "sub"},
57-
Desc: "A simple mock subcommand with aliases.",
66+
Desc: "A simple mock subcommand with aliases and its own subcommand.",
67+
}
68+
}
69+
70+
func (c *mockSubCmdNoNested) Run(fl *pflag.FlagSet) {}
71+
72+
func (c *mockSubCmdNoNested) Spec() CommandSpec {
73+
return CommandSpec{
74+
Name: "mockSubCmdNoNested",
75+
Usage: "Used for help output tests.",
76+
Aliases: []string{"s", "sc", "sub"},
77+
Desc: "A simple mock subcommand with aliases and no nested subcommands.",
5878
}
5979
}
6080

@@ -103,3 +123,49 @@ func TestSubcmdAliasesInParentCmdHelpOutput(t *testing.T) {
103123
expected := expectedParentCmdHelpOutput
104124
assert.Equal(t, "display_subcmd_aliases_in_parentcmd_help_output", expected, got)
105125
}
126+
127+
func TestSubCmdHelpOutput(t *testing.T) {
128+
withNested := `Usage: mockSubCmd Test a subcommand.
129+
130+
Aliases: s, sc, sub
131+
132+
Description: A simple mock subcommand with aliases and its own subcommand.
133+
134+
Commands:
135+
s, sc, sub, mockSubCmd - A simple mock subcommand with aliases and its own subcommand.
136+
`
137+
138+
noNested := `Usage: mockSubCmdNoNested Used for help output tests.
139+
140+
Aliases: s, sc, sub
141+
142+
Description: A simple mock subcommand with aliases and no nested subcommands.
143+
`
144+
145+
for _, test := range []struct {
146+
cmd Command
147+
name, expected string
148+
}{
149+
{
150+
name: "subcmd w/nested subcmd.",
151+
expected: withNested,
152+
cmd: new(mockSubCmd),
153+
},
154+
{
155+
name: "subcmd w/no nested subcmds.",
156+
expected: noNested,
157+
cmd: new(mockSubCmdNoNested),
158+
},
159+
} {
160+
t.Run(test.name, func(t *testing.T) {
161+
buf := new(bytes.Buffer)
162+
name := test.cmd.Spec().Name
163+
fl := pflag.NewFlagSet(name, pflag.ExitOnError)
164+
// If the help output is not written to the buffer
165+
// in the format we expect then the test will fail.
166+
renderHelp(name, test.cmd, fl, buf)
167+
got := string(buf.Bytes())
168+
assert.Equal(t, t.Name(), test.expected, got)
169+
})
170+
}
171+
}

help.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ func renderHelp(fullName string, cmd Command, fl *pflag.FlagSet, w io.Writer) {
5252

5353
// If the command has aliases, add them to the output as a comma-separated list.
5454
if spec.HasAliases() {
55-
fmt.Fprintf(&b, "Aliases: %v", spec.Aliases)
55+
fmt.Fprintf(&b, "Aliases: %s\n\n", strings.Join(spec.Aliases, ", "))
5656
}
5757
// Render usage and description.
58-
usageAndDesc := fmt.Sprintf("%s%s\n", b.String(), spec.Desc)
58+
usageAndDesc := fmt.Sprintf("%sDescription: %s\n", b.String(), spec.Desc)
5959
fmt.Fprint(w, usageAndDesc)
6060

6161
tw := tabwriter.NewWriter(w, 0, 4, 2, ' ', tabwriter.StripEscape)
@@ -80,7 +80,7 @@ func renderHelp(fullName string, cmd Command, fl *pflag.FlagSet, w io.Writer) {
8080
continue
8181
}
8282

83-
allNames := strings.Join(append(spec.Aliases, spec.Name), ",")
83+
allNames := strings.Join(append(spec.Aliases, spec.Name), ", ")
8484

8585
fmt.Fprintf(tw,
8686
tabEscape+"\t"+tabEscape+"%v\t- %v\n",

0 commit comments

Comments
 (0)