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

Commit 6615750

Browse files
author
Faris Huskovic
committed
Update help output format
1 parent b2c2ad4 commit 6615750

File tree

3 files changed

+10
-11
lines changed

3 files changed

+10
-11
lines changed

command_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type (
1515
// Mock for root/parent command.
1616
mockParentCmd struct{}
1717
// Mock subcommand with aliases and a nested sucommand of its own.
18-
mockSubCmd struct{
18+
mockSubCmd struct {
1919
buf *bytes.Buffer
2020
}
2121
// Mock subcommand with aliases and no nested subcommands.
@@ -103,8 +103,8 @@ Commands:
103103
fl := pflag.NewFlagSet(name, pflag.ExitOnError)
104104
// If the help output doesn't contain the subcommand and
105105
// isn't formatted the way we expect the test will fail.
106-
renderHelp(name, cmd, fl, buf)
107-
got := string(buf.Bytes())
106+
renderHelp(buf, name, cmd, fl)
107+
got := buf.String()
108108
assert.Equal(t, t.Name(), expected, got)
109109
})
110110
}
@@ -128,8 +128,8 @@ Description: A simple mock subcommand with aliases and no nested subcommands.
128128
`
129129

130130
for _, test := range []struct {
131-
cmd Command
132131
name, expected string
132+
cmd Command
133133
}{
134134
{
135135
name: "subcmd w/nested subcmd.",
@@ -148,8 +148,8 @@ Description: A simple mock subcommand with aliases and no nested subcommands.
148148
fl := pflag.NewFlagSet(name, pflag.ExitOnError)
149149
// If the help output is not written to the buffer
150150
// in the format we expect then the test will fail.
151-
renderHelp(name, test.cmd, fl, buf)
152-
got := string(buf.Bytes())
151+
renderHelp(buf, name, test.cmd, fl)
152+
got := buf.String()
153153
assert.Equal(t, t.Name(), test.expected, got)
154154
})
155155
}

help.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func renderFlagHelp(fullName string, fl *pflag.FlagSet, w io.Writer) {
4545
}
4646

4747
// renderHelp generates a command's help page.
48-
func renderHelp(fullName string, cmd Command, fl *pflag.FlagSet, w io.Writer) {
48+
func renderHelp(w io.Writer, fullName string, cmd Command, fl *pflag.FlagSet) {
4949
var b strings.Builder
5050
spec := cmd.Spec()
5151
fmt.Fprintf(&b, "Usage: %v %v\n\n", fullName, spec.Usage)
@@ -54,9 +54,8 @@ func renderHelp(fullName string, cmd Command, fl *pflag.FlagSet, w io.Writer) {
5454
if spec.HasAliases() {
5555
fmt.Fprintf(&b, "Aliases: %s\n\n", strings.Join(spec.Aliases, ", "))
5656
}
57-
// Render usage and description.
58-
usageAndDesc := fmt.Sprintf("%sDescription: %s\n", b.String(), spec.Desc)
59-
fmt.Fprint(w, usageAndDesc)
57+
// Print usage and description.
58+
fmt.Fprintf(w, "%sDescription: %s\n", b.String(), spec.Desc)
6059

6160
tw := tabwriter.NewWriter(w, 0, 4, 2, ' ', tabwriter.StripEscape)
6261
defer tw.Flush()

run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func Run(cmd Command, args []string, parent string) {
9494
// Reassign the usage now that we've parsed the args
9595
// so that we can render it manually.
9696
fl.Usage = func() {
97-
renderHelp(name, cmd, fl, os.Stderr)
97+
renderHelp(os.Stderr, name, cmd, fl)
9898
}
9999
if err != nil {
100100
fl.Usage()

0 commit comments

Comments
 (0)