Skip to content

Commit

Permalink
doc: GenMarkdown skip Synopsis on empty long cmd (#1207)
Browse files Browse the repository at this point in the history
This patch modifies the GenMarkdownCustom to skip writing a Synopsis
header with the `cmd.Short` duplicated both before and after the header.

Instead, it only writes the `### Synopsis` header and the paragraph when
a `cmd.Long` has some kind of content in it.

Adds `TestGenMdDocWithNoLongOrSynopsis` as the test case.

Signed-off-by: Marc Lopez <marc5.12@outlook.com>
  • Loading branch information
marclop committed Aug 26, 2020
1 parent 9ed1d71 commit 02a0d2f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
7 changes: 6 additions & 1 deletion doc/cmd_test.go
Expand Up @@ -27,7 +27,7 @@ func init() {
printCmd.Flags().BoolP("boolthree", "b", true, "help message for flag boolthree")

echoCmd.AddCommand(timesCmd, echoSubCmd, deprecatedCmd)
rootCmd.AddCommand(printCmd, echoCmd)
rootCmd.AddCommand(printCmd, echoCmd, dummyCmd)
}

var rootCmd = &cobra.Command{
Expand Down Expand Up @@ -73,6 +73,11 @@ var printCmd = &cobra.Command{
Long: `an absolutely utterly useless command for testing.`,
}

var dummyCmd = &cobra.Command{
Use: "dummy [action]",
Short: "Performs a dummy action",
}

func checkStringContains(t *testing.T, got, expected string) {
if !strings.Contains(got, expected) {
t.Errorf("Expected to contain: \n %v\nGot:\n %v\n", expected, got)
Expand Down
14 changes: 5 additions & 9 deletions doc/md_docs.go
Expand Up @@ -58,16 +58,12 @@ func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string)
buf := new(bytes.Buffer)
name := cmd.CommandPath()

short := cmd.Short
long := cmd.Long
if len(long) == 0 {
long = short
}

buf.WriteString("## " + name + "\n\n")
buf.WriteString(short + "\n\n")
buf.WriteString("### Synopsis\n\n")
buf.WriteString(long + "\n\n")
buf.WriteString(cmd.Short + "\n\n")
if len(cmd.Long) > 0 {
buf.WriteString("### Synopsis\n\n")
buf.WriteString(cmd.Long + "\n\n")
}

if cmd.Runnable() {
buf.WriteString(fmt.Sprintf("```\n%s\n```\n\n", cmd.UseLine()))
Expand Down
14 changes: 14 additions & 0 deletions doc/md_docs_test.go
Expand Up @@ -28,6 +28,20 @@ func TestGenMdDoc(t *testing.T) {
checkStringContains(t, output, "Options inherited from parent commands")
}

func TestGenMdDocWithNoLongOrSynopsis(t *testing.T) {
// We generate on subcommand so we have both subcommands and parents.
buf := new(bytes.Buffer)
if err := GenMarkdown(dummyCmd, buf); err != nil {
t.Fatal(err)
}
output := buf.String()

checkStringContains(t, output, dummyCmd.Example)
checkStringContains(t, output, dummyCmd.Short)
checkStringContains(t, output, "Options inherited from parent commands")
checkStringOmits(t, output, "### Synopsis")
}

func TestGenMdNoHiddenParents(t *testing.T) {
// We generate on subcommand so we have both subcommands and parents.
for _, name := range []string{"rootflag", "strtwo"} {
Expand Down

0 comments on commit 02a0d2f

Please sign in to comment.