Skip to content

Commit 17d7ab0

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

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

docs.go

Lines changed: 6 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

@@ -367,6 +370,9 @@ func (tt tabularTemplate) PrepareCommands(commands []*cli.Command, appPath, pare
367370
var result = make([]cliTabularCommandTemplate, 0, len(commands))
368371

369372
for _, cmd := range commands {
373+
if level > 0 && cmd.Name == "help" {
374+
continue
375+
}
370376
var command = cliTabularCommandTemplate{
371377
AppPath: appPath,
372378
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"
@@ -182,6 +183,49 @@ func TestToTabularMarkdown(t *testing.T) {
182183
})
183184
}
184185

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

0 commit comments

Comments
 (0)