Skip to content

Commit 0ab5b6b

Browse files
jdefeparis
authored andcommitted
doc: hide hidden parent flags (#686)
* fixes #685
1 parent 7ee208b commit 0ab5b6b

File tree

6 files changed

+89
-6
lines changed

6 files changed

+89
-6
lines changed

doc/man_docs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,13 @@ func manPrintFlags(buf *bytes.Buffer, flags *pflag.FlagSet) {
176176

177177
func manPrintOptions(buf *bytes.Buffer, command *cobra.Command) {
178178
flags := command.NonInheritedFlags()
179-
if flags.HasFlags() {
179+
if flags.HasAvailableFlags() {
180180
buf.WriteString("# OPTIONS\n")
181181
manPrintFlags(buf, flags)
182182
buf.WriteString("\n")
183183
}
184184
flags = command.InheritedFlags()
185-
if flags.HasFlags() {
185+
if flags.HasAvailableFlags() {
186186
buf.WriteString("# OPTIONS INHERITED FROM PARENT COMMANDS\n")
187187
manPrintFlags(buf, flags)
188188
buf.WriteString("\n")

doc/man_docs_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,42 @@ func TestGenManDoc(t *testing.T) {
4747
checkStringContains(t, output, translate("Auto generated"))
4848
}
4949

50+
func TestGenManNoHiddenParents(t *testing.T) {
51+
header := &GenManHeader{
52+
Title: "Project",
53+
Section: "2",
54+
}
55+
56+
// We generate on a subcommand so we have both subcommands and parents
57+
for _, name := range []string{"rootflag", "strtwo"} {
58+
f := rootCmd.PersistentFlags().Lookup(name)
59+
f.Hidden = true
60+
defer func() { f.Hidden = false }()
61+
}
62+
buf := new(bytes.Buffer)
63+
if err := GenMan(echoCmd, header, buf); err != nil {
64+
t.Fatal(err)
65+
}
66+
output := buf.String()
67+
68+
// Make sure parent has - in CommandPath() in SEE ALSO:
69+
parentPath := echoCmd.Parent().CommandPath()
70+
dashParentPath := strings.Replace(parentPath, " ", "-", -1)
71+
expected := translate(dashParentPath)
72+
expected = expected + "(" + header.Section + ")"
73+
checkStringContains(t, output, expected)
74+
75+
checkStringContains(t, output, translate(echoCmd.Name()))
76+
checkStringContains(t, output, translate(echoCmd.Name()))
77+
checkStringContains(t, output, "boolone")
78+
checkStringOmits(t, output, "rootflag")
79+
checkStringContains(t, output, translate(rootCmd.Name()))
80+
checkStringContains(t, output, translate(echoSubCmd.Name()))
81+
checkStringOmits(t, output, translate(deprecatedCmd.Name()))
82+
checkStringContains(t, output, translate("Auto generated"))
83+
checkStringOmits(t, output, "OPTIONS INHERITED FROM PARENT COMMANDS")
84+
}
85+
5086
func TestGenManNoGenTag(t *testing.T) {
5187
echoCmd.DisableAutoGenTag = true
5288
defer func() { echoCmd.DisableAutoGenTag = false }()

doc/md_docs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ import (
2929
func printOptions(buf *bytes.Buffer, cmd *cobra.Command, name string) error {
3030
flags := cmd.NonInheritedFlags()
3131
flags.SetOutput(buf)
32-
if flags.HasFlags() {
32+
if flags.HasAvailableFlags() {
3333
buf.WriteString("### Options\n\n```\n")
3434
flags.PrintDefaults()
3535
buf.WriteString("```\n\n")
3636
}
3737

3838
parentFlags := cmd.InheritedFlags()
3939
parentFlags.SetOutput(buf)
40-
if parentFlags.HasFlags() {
40+
if parentFlags.HasAvailableFlags() {
4141
buf.WriteString("### Options inherited from parent commands\n\n```\n")
4242
parentFlags.PrintDefaults()
4343
buf.WriteString("```\n\n")

doc/md_docs_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,30 @@ func TestGenMdDoc(t *testing.T) {
2525
checkStringContains(t, output, rootCmd.Short)
2626
checkStringContains(t, output, echoSubCmd.Short)
2727
checkStringOmits(t, output, deprecatedCmd.Short)
28+
checkStringContains(t, output, "Options inherited from parent commands")
29+
}
30+
31+
func TestGenMdNoHiddenParents(t *testing.T) {
32+
// We generate on subcommand so we have both subcommands and parents.
33+
for _, name := range []string{"rootflag", "strtwo"} {
34+
f := rootCmd.PersistentFlags().Lookup(name)
35+
f.Hidden = true
36+
defer func() { f.Hidden = false }()
37+
}
38+
buf := new(bytes.Buffer)
39+
if err := GenMarkdown(echoCmd, buf); err != nil {
40+
t.Fatal(err)
41+
}
42+
output := buf.String()
43+
44+
checkStringContains(t, output, echoCmd.Long)
45+
checkStringContains(t, output, echoCmd.Example)
46+
checkStringContains(t, output, "boolone")
47+
checkStringOmits(t, output, "rootflag")
48+
checkStringContains(t, output, rootCmd.Short)
49+
checkStringContains(t, output, echoSubCmd.Short)
50+
checkStringOmits(t, output, deprecatedCmd.Short)
51+
checkStringOmits(t, output, "Options inherited from parent commands")
2852
}
2953

3054
func TestGenMdNoTag(t *testing.T) {

doc/rest_docs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
func printOptionsReST(buf *bytes.Buffer, cmd *cobra.Command, name string) error {
3030
flags := cmd.NonInheritedFlags()
3131
flags.SetOutput(buf)
32-
if flags.HasFlags() {
32+
if flags.HasAvailableFlags() {
3333
buf.WriteString("Options\n")
3434
buf.WriteString("~~~~~~~\n\n::\n\n")
3535
flags.PrintDefaults()
@@ -38,7 +38,7 @@ func printOptionsReST(buf *bytes.Buffer, cmd *cobra.Command, name string) error
3838

3939
parentFlags := cmd.InheritedFlags()
4040
parentFlags.SetOutput(buf)
41-
if parentFlags.HasFlags() {
41+
if parentFlags.HasAvailableFlags() {
4242
buf.WriteString("Options inherited from parent commands\n")
4343
buf.WriteString("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n::\n\n")
4444
parentFlags.PrintDefaults()

doc/rest_docs_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,29 @@ func TestGenRSTDoc(t *testing.T) {
2727
checkStringOmits(t, output, deprecatedCmd.Short)
2828
}
2929

30+
func TestGenRSTNoHiddenParents(t *testing.T) {
31+
// We generate on a subcommand so we have both subcommands and parents
32+
for _, name := range []string{"rootflag", "strtwo"} {
33+
f := rootCmd.PersistentFlags().Lookup(name)
34+
f.Hidden = true
35+
defer func() { f.Hidden = false }()
36+
}
37+
buf := new(bytes.Buffer)
38+
if err := GenReST(echoCmd, buf); err != nil {
39+
t.Fatal(err)
40+
}
41+
output := buf.String()
42+
43+
checkStringContains(t, output, echoCmd.Long)
44+
checkStringContains(t, output, echoCmd.Example)
45+
checkStringContains(t, output, "boolone")
46+
checkStringOmits(t, output, "rootflag")
47+
checkStringContains(t, output, rootCmd.Short)
48+
checkStringContains(t, output, echoSubCmd.Short)
49+
checkStringOmits(t, output, deprecatedCmd.Short)
50+
checkStringOmits(t, output, "Options inherited from parent commands")
51+
}
52+
3053
func TestGenRSTNoTag(t *testing.T) {
3154
rootCmd.DisableAutoGenTag = true
3255
defer func() { rootCmd.DisableAutoGenTag = false }()

0 commit comments

Comments
 (0)