From 385a6fe2ea11dabc07897e9f4acc5b0e795389aa Mon Sep 17 00:00:00 2001 From: Jim Schubert Date: Sat, 23 Apr 2022 15:27:28 -0400 Subject: [PATCH] Manpage supports commands with slashes --- doc/man_docs.go | 21 +++++++++++++++++---- doc/man_docs_test.go | 23 +++++++++++++++++++++++ 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/doc/man_docs.go b/doc/man_docs.go index 8dbce35..9a08b21 100644 --- a/doc/man_docs.go +++ b/doc/man_docs.go @@ -66,7 +66,13 @@ func GenManTreeFromOpts(cmd *cobra.Command, opts GenManTreeOptions) error { if opts.CommandSeparator != "" { separator = opts.CommandSeparator } - basename := strings.ReplaceAll(cmd.CommandPath(), " ", separator) + + replacer := strings.NewReplacer( + " ", separator, + "/", separator, + ) + + basename := replacer.Replace(cmd.CommandPath()) filename := filepath.Join(opts.Path, basename+"."+section) f, err := os.Create(filename) if err != nil { @@ -202,8 +208,14 @@ func genMan(cmd *cobra.Command, header *GenManHeader) []byte { cmd.InitDefaultHelpCmd() cmd.InitDefaultHelpFlag() + // note that genMan doesn't accept custom separator defined in GenManTreeOptions + replacer := strings.NewReplacer( + " ", "-", + "/", "-", + ) + // something like `rootcmd-subcmd1-subcmd2` - dashCommandName := strings.ReplaceAll(cmd.CommandPath(), " ", "-") + dashCommandName := replacer.Replace(cmd.CommandPath()) buf := new(bytes.Buffer) @@ -218,7 +230,8 @@ func genMan(cmd *cobra.Command, header *GenManHeader) []byte { seealsos := make([]string, 0) if cmd.HasParent() { parentPath := cmd.Parent().CommandPath() - dashParentPath := strings.ReplaceAll(parentPath, " ", "-") + + dashParentPath := replacer.Replace(parentPath) seealso := fmt.Sprintf("**%s(%s)**", dashParentPath, header.Section) seealsos = append(seealsos, seealso) cmd.VisitParents(func(c *cobra.Command) { @@ -233,7 +246,7 @@ func genMan(cmd *cobra.Command, header *GenManHeader) []byte { if !c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand() { continue } - seealso := fmt.Sprintf("**%s-%s(%s)**", dashCommandName, c.Name(), header.Section) + seealso := fmt.Sprintf("**%s-%s(%s)**", dashCommandName, replacer.Replace(c.Name()), header.Section) seealsos = append(seealsos, seealso) } buf.WriteString(strings.Join(seealsos, ", ") + "\n") diff --git a/doc/man_docs_test.go b/doc/man_docs_test.go index f7994f8..b5c1e86 100644 --- a/doc/man_docs_test.go +++ b/doc/man_docs_test.go @@ -136,6 +136,29 @@ func TestGenManSeeAlso(t *testing.T) { } } +func TestGenManSeeAlsoSlashCommands(t *testing.T) { + rootCmd := &cobra.Command{Use: "root", Run: emptyRun} + childCmd := &cobra.Command{Use: "run/first", Run: emptyRun} + rootCmd.AddCommand(childCmd) + + buf := new(bytes.Buffer) + header := &GenManHeader{} + if err := GenMan(rootCmd, header, buf); err != nil { + t.Fatal(err) + } + scanner := bufio.NewScanner(buf) + + if err := assertLineFound(scanner, ".SH SEE ALSO"); err != nil { + t.Fatalf("Couldn't find SEE ALSO section header: %v", err) + } + if err := assertNextLineEquals(scanner, ".PP"); err != nil { + t.Fatalf("First line after SEE ALSO wasn't break-indent: %v", err) + } + if err := assertNextLineEquals(scanner, `\fBroot-run-first(1)\fP`); err != nil { + t.Fatalf("Second line after SEE ALSO wasn't correct: %v", err) + } +} + func TestManPrintFlagsHidesShortDeperecated(t *testing.T) { c := &cobra.Command{} c.Flags().StringP("foo", "f", "default", "Foo flag")