doc: hide hidden parent flags (#686)

* fixes #685
This commit is contained in:
James DeFelice 2018-04-24 12:15:12 -04:00 committed by Eric Paris
parent 7ee208b09f
commit 0ab5b6bcfc
6 changed files with 89 additions and 6 deletions

View file

@ -176,13 +176,13 @@ func manPrintFlags(buf *bytes.Buffer, flags *pflag.FlagSet) {
func manPrintOptions(buf *bytes.Buffer, command *cobra.Command) { func manPrintOptions(buf *bytes.Buffer, command *cobra.Command) {
flags := command.NonInheritedFlags() flags := command.NonInheritedFlags()
if flags.HasFlags() { if flags.HasAvailableFlags() {
buf.WriteString("# OPTIONS\n") buf.WriteString("# OPTIONS\n")
manPrintFlags(buf, flags) manPrintFlags(buf, flags)
buf.WriteString("\n") buf.WriteString("\n")
} }
flags = command.InheritedFlags() flags = command.InheritedFlags()
if flags.HasFlags() { if flags.HasAvailableFlags() {
buf.WriteString("# OPTIONS INHERITED FROM PARENT COMMANDS\n") buf.WriteString("# OPTIONS INHERITED FROM PARENT COMMANDS\n")
manPrintFlags(buf, flags) manPrintFlags(buf, flags)
buf.WriteString("\n") buf.WriteString("\n")

View file

@ -47,6 +47,42 @@ func TestGenManDoc(t *testing.T) {
checkStringContains(t, output, translate("Auto generated")) checkStringContains(t, output, translate("Auto generated"))
} }
func TestGenManNoHiddenParents(t *testing.T) {
header := &GenManHeader{
Title: "Project",
Section: "2",
}
// We generate on a subcommand so we have both subcommands and parents
for _, name := range []string{"rootflag", "strtwo"} {
f := rootCmd.PersistentFlags().Lookup(name)
f.Hidden = true
defer func() { f.Hidden = false }()
}
buf := new(bytes.Buffer)
if err := GenMan(echoCmd, header, buf); err != nil {
t.Fatal(err)
}
output := buf.String()
// Make sure parent has - in CommandPath() in SEE ALSO:
parentPath := echoCmd.Parent().CommandPath()
dashParentPath := strings.Replace(parentPath, " ", "-", -1)
expected := translate(dashParentPath)
expected = expected + "(" + header.Section + ")"
checkStringContains(t, output, expected)
checkStringContains(t, output, translate(echoCmd.Name()))
checkStringContains(t, output, translate(echoCmd.Name()))
checkStringContains(t, output, "boolone")
checkStringOmits(t, output, "rootflag")
checkStringContains(t, output, translate(rootCmd.Name()))
checkStringContains(t, output, translate(echoSubCmd.Name()))
checkStringOmits(t, output, translate(deprecatedCmd.Name()))
checkStringContains(t, output, translate("Auto generated"))
checkStringOmits(t, output, "OPTIONS INHERITED FROM PARENT COMMANDS")
}
func TestGenManNoGenTag(t *testing.T) { func TestGenManNoGenTag(t *testing.T) {
echoCmd.DisableAutoGenTag = true echoCmd.DisableAutoGenTag = true
defer func() { echoCmd.DisableAutoGenTag = false }() defer func() { echoCmd.DisableAutoGenTag = false }()

View file

@ -29,7 +29,7 @@ import (
func printOptions(buf *bytes.Buffer, cmd *cobra.Command, name string) error { func printOptions(buf *bytes.Buffer, cmd *cobra.Command, name string) error {
flags := cmd.NonInheritedFlags() flags := cmd.NonInheritedFlags()
flags.SetOutput(buf) flags.SetOutput(buf)
if flags.HasFlags() { if flags.HasAvailableFlags() {
buf.WriteString("### Options\n\n```\n") buf.WriteString("### Options\n\n```\n")
flags.PrintDefaults() flags.PrintDefaults()
buf.WriteString("```\n\n") buf.WriteString("```\n\n")
@ -37,7 +37,7 @@ func printOptions(buf *bytes.Buffer, cmd *cobra.Command, name string) error {
parentFlags := cmd.InheritedFlags() parentFlags := cmd.InheritedFlags()
parentFlags.SetOutput(buf) parentFlags.SetOutput(buf)
if parentFlags.HasFlags() { if parentFlags.HasAvailableFlags() {
buf.WriteString("### Options inherited from parent commands\n\n```\n") buf.WriteString("### Options inherited from parent commands\n\n```\n")
parentFlags.PrintDefaults() parentFlags.PrintDefaults()
buf.WriteString("```\n\n") buf.WriteString("```\n\n")

View file

@ -25,6 +25,30 @@ func TestGenMdDoc(t *testing.T) {
checkStringContains(t, output, rootCmd.Short) checkStringContains(t, output, rootCmd.Short)
checkStringContains(t, output, echoSubCmd.Short) checkStringContains(t, output, echoSubCmd.Short)
checkStringOmits(t, output, deprecatedCmd.Short) checkStringOmits(t, output, deprecatedCmd.Short)
checkStringContains(t, output, "Options inherited from parent commands")
}
func TestGenMdNoHiddenParents(t *testing.T) {
// We generate on subcommand so we have both subcommands and parents.
for _, name := range []string{"rootflag", "strtwo"} {
f := rootCmd.PersistentFlags().Lookup(name)
f.Hidden = true
defer func() { f.Hidden = false }()
}
buf := new(bytes.Buffer)
if err := GenMarkdown(echoCmd, buf); err != nil {
t.Fatal(err)
}
output := buf.String()
checkStringContains(t, output, echoCmd.Long)
checkStringContains(t, output, echoCmd.Example)
checkStringContains(t, output, "boolone")
checkStringOmits(t, output, "rootflag")
checkStringContains(t, output, rootCmd.Short)
checkStringContains(t, output, echoSubCmd.Short)
checkStringOmits(t, output, deprecatedCmd.Short)
checkStringOmits(t, output, "Options inherited from parent commands")
} }
func TestGenMdNoTag(t *testing.T) { func TestGenMdNoTag(t *testing.T) {

View file

@ -29,7 +29,7 @@ import (
func printOptionsReST(buf *bytes.Buffer, cmd *cobra.Command, name string) error { func printOptionsReST(buf *bytes.Buffer, cmd *cobra.Command, name string) error {
flags := cmd.NonInheritedFlags() flags := cmd.NonInheritedFlags()
flags.SetOutput(buf) flags.SetOutput(buf)
if flags.HasFlags() { if flags.HasAvailableFlags() {
buf.WriteString("Options\n") buf.WriteString("Options\n")
buf.WriteString("~~~~~~~\n\n::\n\n") buf.WriteString("~~~~~~~\n\n::\n\n")
flags.PrintDefaults() flags.PrintDefaults()
@ -38,7 +38,7 @@ func printOptionsReST(buf *bytes.Buffer, cmd *cobra.Command, name string) error
parentFlags := cmd.InheritedFlags() parentFlags := cmd.InheritedFlags()
parentFlags.SetOutput(buf) parentFlags.SetOutput(buf)
if parentFlags.HasFlags() { if parentFlags.HasAvailableFlags() {
buf.WriteString("Options inherited from parent commands\n") buf.WriteString("Options inherited from parent commands\n")
buf.WriteString("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n::\n\n") buf.WriteString("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n::\n\n")
parentFlags.PrintDefaults() parentFlags.PrintDefaults()

View file

@ -27,6 +27,29 @@ func TestGenRSTDoc(t *testing.T) {
checkStringOmits(t, output, deprecatedCmd.Short) checkStringOmits(t, output, deprecatedCmd.Short)
} }
func TestGenRSTNoHiddenParents(t *testing.T) {
// We generate on a subcommand so we have both subcommands and parents
for _, name := range []string{"rootflag", "strtwo"} {
f := rootCmd.PersistentFlags().Lookup(name)
f.Hidden = true
defer func() { f.Hidden = false }()
}
buf := new(bytes.Buffer)
if err := GenReST(echoCmd, buf); err != nil {
t.Fatal(err)
}
output := buf.String()
checkStringContains(t, output, echoCmd.Long)
checkStringContains(t, output, echoCmd.Example)
checkStringContains(t, output, "boolone")
checkStringOmits(t, output, "rootflag")
checkStringContains(t, output, rootCmd.Short)
checkStringContains(t, output, echoSubCmd.Short)
checkStringOmits(t, output, deprecatedCmd.Short)
checkStringOmits(t, output, "Options inherited from parent commands")
}
func TestGenRSTNoTag(t *testing.T) { func TestGenRSTNoTag(t *testing.T) {
rootCmd.DisableAutoGenTag = true rootCmd.DisableAutoGenTag = true
defer func() { rootCmd.DisableAutoGenTag = false }() defer func() { rootCmd.DisableAutoGenTag = false }()