Skip to content
Myr

doc: GenMarkdown skip Synopsis on empty long cmd (#1207)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

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.

authored by Marc Lopez Rubio · committed by GitHub · parent 9ed1d71 · Browse files · 3 files+25 added−10 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Signed-off-by: Marc Lopez <marc5.12@outlook.com>

Diff stats

3 files+25 added−10 removed

  • other:+25 added−10 removed

3 files+25 added−10 removed

modified:doc/cmd_test.go+6 added-1 removed
25 unmodified lines
26 printCmd.Flags().IntP("intthree", "i", 345, "help message for flag intthree")27 printCmd.Flags().BoolP("boolthree", "b", true, "help message for flag boolthree")2829 echoCmd.AddCommand(timesCmd, echoSubCmd, deprecatedCmd)30 rootCmd.AddCommand(printCmd, echoCmd)30 rootCmd.AddCommand(printCmd, echoCmd, dummyCmd)31}3233var rootCmd = &cobra.Command{34 Use: "root",
37 unmodified lines
72 Short: "Print anything to the screen",73 Long: `an absolutely utterly useless command for testing.`,74}7576var dummyCmd = &cobra.Command{77 Use: "dummy [action]",78 Short: "Performs a dummy action",79}8081func checkStringContains(t *testing.T, got, expected string) {82 if !strings.Contains(got, expected) {83 t.Errorf("Expected to contain: \n %v\nGot:\n %v\n", expected, got)84 }
7 unmodified lines
modified:doc/md_docs_test.go+14 added
26 unmodified lines
27 checkStringOmits(t, output, deprecatedCmd.Short)28 checkStringContains(t, output, "Options inherited from parent commands")29}3031func TestGenMdDocWithNoLongOrSynopsis(t *testing.T) {32 // We generate on subcommand so we have both subcommands and parents.33 buf := new(bytes.Buffer)34 if err := GenMarkdown(dummyCmd, buf); err != nil {35 t.Fatal(err)36 }37 output := buf.String()3839 checkStringContains(t, output, dummyCmd.Example)40 checkStringContains(t, output, dummyCmd.Short)41 checkStringContains(t, output, "Options inherited from parent commands")42 checkStringOmits(t, output, "### Synopsis")43}4445func TestGenMdNoHiddenParents(t *testing.T) {46 // We generate on subcommand so we have both subcommands and parents.47 for _, name := range []string{"rootflag", "strtwo"} {48 f := rootCmd.PersistentFlags().Lookup(name)
64 unmodified lines
modified:doc/md_docs.go+5 added-9 removed
56 unmodified lines
5758 buf := new(bytes.Buffer)59 name := cmd.CommandPath()6061 short := cmd.Short62 long := cmd.Long63 if len(long) == 0 {64 long = short61 buf.WriteString("## " + name + "\n\n")62 buf.WriteString(cmd.Short + "\n\n")63 if len(cmd.Long) > 0 {64 buf.WriteString("### Synopsis\n\n")65 buf.WriteString(cmd.Long + "\n\n")66 }6767 buf.WriteString("## " + name + "\n\n")68 buf.WriteString(short + "\n\n")69 buf.WriteString("### Synopsis\n\n")70 buf.WriteString(long + "\n\n")7168 if cmd.Runnable() {69 buf.WriteString(fmt.Sprintf("```\n%s\n```\n\n", cmd.UseLine()))70 }71
84 unmodified lines