Manpage supports commands with slashes

This commit is contained in:
Jim Schubert 2022-04-23 15:27:28 -04:00
parent 500d19e1c2
commit 385a6fe2ea
2 changed files with 40 additions and 4 deletions

View File

@ -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")

View File

@ -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")