mirror of
https://github.com/spf13/cobra
synced 2024-11-24 22:57:12 +00:00
Manpage supports commands with slashes
This commit is contained in:
parent
500d19e1c2
commit
385a6fe2ea
2 changed files with 40 additions and 4 deletions
|
@ -66,7 +66,13 @@ func GenManTreeFromOpts(cmd *cobra.Command, opts GenManTreeOptions) error {
|
||||||
if opts.CommandSeparator != "" {
|
if opts.CommandSeparator != "" {
|
||||||
separator = 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)
|
filename := filepath.Join(opts.Path, basename+"."+section)
|
||||||
f, err := os.Create(filename)
|
f, err := os.Create(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -202,8 +208,14 @@ func genMan(cmd *cobra.Command, header *GenManHeader) []byte {
|
||||||
cmd.InitDefaultHelpCmd()
|
cmd.InitDefaultHelpCmd()
|
||||||
cmd.InitDefaultHelpFlag()
|
cmd.InitDefaultHelpFlag()
|
||||||
|
|
||||||
|
// note that genMan doesn't accept custom separator defined in GenManTreeOptions
|
||||||
|
replacer := strings.NewReplacer(
|
||||||
|
" ", "-",
|
||||||
|
"/", "-",
|
||||||
|
)
|
||||||
|
|
||||||
// something like `rootcmd-subcmd1-subcmd2`
|
// something like `rootcmd-subcmd1-subcmd2`
|
||||||
dashCommandName := strings.ReplaceAll(cmd.CommandPath(), " ", "-")
|
dashCommandName := replacer.Replace(cmd.CommandPath())
|
||||||
|
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
|
|
||||||
|
@ -218,7 +230,8 @@ func genMan(cmd *cobra.Command, header *GenManHeader) []byte {
|
||||||
seealsos := make([]string, 0)
|
seealsos := make([]string, 0)
|
||||||
if cmd.HasParent() {
|
if cmd.HasParent() {
|
||||||
parentPath := cmd.Parent().CommandPath()
|
parentPath := cmd.Parent().CommandPath()
|
||||||
dashParentPath := strings.ReplaceAll(parentPath, " ", "-")
|
|
||||||
|
dashParentPath := replacer.Replace(parentPath)
|
||||||
seealso := fmt.Sprintf("**%s(%s)**", dashParentPath, header.Section)
|
seealso := fmt.Sprintf("**%s(%s)**", dashParentPath, header.Section)
|
||||||
seealsos = append(seealsos, seealso)
|
seealsos = append(seealsos, seealso)
|
||||||
cmd.VisitParents(func(c *cobra.Command) {
|
cmd.VisitParents(func(c *cobra.Command) {
|
||||||
|
@ -233,7 +246,7 @@ func genMan(cmd *cobra.Command, header *GenManHeader) []byte {
|
||||||
if !c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand() {
|
if !c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand() {
|
||||||
continue
|
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)
|
seealsos = append(seealsos, seealso)
|
||||||
}
|
}
|
||||||
buf.WriteString(strings.Join(seealsos, ", ") + "\n")
|
buf.WriteString(strings.Join(seealsos, ", ") + "\n")
|
||||||
|
|
|
@ -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) {
|
func TestManPrintFlagsHidesShortDeperecated(t *testing.T) {
|
||||||
c := &cobra.Command{}
|
c := &cobra.Command{}
|
||||||
c.Flags().StringP("foo", "f", "default", "Foo flag")
|
c.Flags().StringP("foo", "f", "default", "Foo flag")
|
||||||
|
|
Loading…
Reference in a new issue