Skip to content

Commit 0661a34

Browse files
committed
skip nested help subcommands
When every command has a nested "help" subcommand it is harder to read the documentation.
1 parent 0333461 commit 0661a34

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

docs.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ func prepareCommands(commands []*cli.Command, level int) []string {
196196
if command.Hidden {
197197
continue
198198
}
199+
if level > 0 && command.Name == "help" {
200+
continue
201+
}
199202

200203
usageText := prepareUsageText(command)
201204

@@ -369,6 +372,10 @@ func (tt tabularTemplate) PrepareCommands(commands []*cli.Command, appPath, pare
369372
result := make([]cliTabularCommandTemplate, 0, len(commands))
370373

371374
for _, cmd := range commands {
375+
if level > 0 && cmd.Name == "help" {
376+
continue
377+
}
378+
372379
command := cliTabularCommandTemplate{
373380
AppPath: appPath,
374381
Name: strings.TrimSpace(strings.Join([]string{parentCommandName, cmd.Name}, " ")),

docs_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io/fs"
88
"net/mail"
99
"os"
10+
"strings"
1011
"testing"
1112

1213
"github.com/stretchr/testify/require"
@@ -185,6 +186,49 @@ func TestToTabularMarkdown(t *testing.T) {
185186
})
186187
}
187188

189+
func TestPrepareCommandsSkipsNestedHelp(t *testing.T) {
190+
tmpl := tabularTemplate{}
191+
commands := []*cli.Command{
192+
{Name: "help"},
193+
{
194+
Name: "config",
195+
Commands: []*cli.Command{
196+
{Name: "help"},
197+
{Name: "sub"},
198+
},
199+
},
200+
}
201+
202+
result := tmpl.PrepareCommands(commands, "app", "", 0)
203+
require.Len(t, result, 2)
204+
require.Equal(t, "help", result[0].Name)
205+
require.Len(t, result[1].SubCommands, 1)
206+
require.Equal(t, "config sub", result[1].SubCommands[0].Name)
207+
require.Equal(t, uint(0), result[0].Level)
208+
require.Equal(t, uint(0), result[1].Level)
209+
require.Equal(t, uint(1), result[1].SubCommands[0].Level)
210+
}
211+
212+
func TestPrepareCommandsMarkdownSkipsNestedHelp(t *testing.T) {
213+
commands := []*cli.Command{
214+
{Name: "help"},
215+
{
216+
Name: "config",
217+
Commands: []*cli.Command{
218+
{Name: "help"},
219+
{Name: "sub"},
220+
},
221+
},
222+
}
223+
224+
sections := prepareCommands(commands, 0)
225+
joined := strings.Join(sections, "\n")
226+
require.Contains(t, joined, "## help\n")
227+
require.Contains(t, joined, "## config\n")
228+
require.Contains(t, joined, "### sub\n")
229+
require.NotContains(t, joined, "### help")
230+
}
231+
188232
func TestToTabularMarkdownFailed(t *testing.T) {
189233
tpl := MarkdownTabularDocTemplate
190234
t.Cleanup(func() { MarkdownTabularDocTemplate = tpl })

0 commit comments

Comments
 (0)