Skip to content
Myr

Cretea a new GenManTree function that takes an options struct.

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

authored by Daniel Nephin · committed by Daniel Nephin · parent 97206b3 · Browse files · 1 file+25 added−6 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: Daniel Nephin <dnephin@gmail.com>

Diff stats

1 file+25 added−6 removed

  • other:+25 added−6 removed

1 file+25 added−6 removed

modified:doc/man_docs.go+25 added-6 removed
32 unmodified lines
33// correctly if your command names have - in them. If you have `cmd` with two34// subcmds, `sub` and `sub-third`. And `sub` has a subcommand called `third`35// it is undefined which help output will be in the file `cmd-sub-third.1`.36func GenManTree(cmd *cobra.Command, header *GenManHeader, dir string) error {37 return GenManTreeFromOpts(cmd, GenManTreeOptions{38 Header: header,39 Path: dir,40 CommandSeparator: "_",41 })42}4344// GenManTreeFromOpts generates a man page for the command and all descendants.45// The pages are written to the opts.Path directory.46func GenManTreeFromOpts(cmd *cobra.Command, opts GenManTreeOptions) error {47 header := opts.Header48 if header == nil {49 header = &GenManHeader{}50 }51 for _, c := range cmd.Commands() {52 if !c.IsAvailableCommand() || c.IsHelpCommand() {53 continue54 }44 if err := GenManTree(c, header, dir); err != nil {55 if err := GenManTreeFromOpts(c, opts); err != nil {56 return err57 }58 }59 section := "1"60 if header.Section != "" {61 section = header.Section62 }6364 separator := "_"65 if opts.CommandSeparator != "" {66 separator = opts.CommandSeparator67 }53 basename := strings.Replace(cmd.CommandPath(), " ", "_", -1) + "." + section54 filename := filepath.Join(dir, basename)68 basename := strings.Replace(cmd.CommandPath(), " ", separator, -1)69 filename := filepath.Join(opts.Path, basename + "." + section)70 f, err := os.Create(filename)71 if err != nil {72 return err73 }
2 unmodified lines
76 headerCopy := *header77 return GenMan(cmd, &headerCopy, f)78}7980type GenManTreeOptions struct {81 Header *GenManHeader82 Path string83 CommandSeparator string84}8586// GenManHeader is a lot like the .TH header at the start of man pages. These87// include the title, section, date, source, and manual. We will use the88// current time if Date if unset and will use "Auto generated by spf13/cobra"89// if the Source is unset.
97 unmodified lines
187 }188}189190func genMan(cmd *cobra.Command, header *GenManHeader) []byte {170 // something like `rootcmd subcmd1 subcmd2`171 commandName := cmd.CommandPath()191 // something like `rootcmd-subcmd1-subcmd2`173 dashCommandName := strings.Replace(commandName, " ", "-", -1)192 dashCommandName := strings.Replace(cmd.CommandPath(), " ", "-", -1)193194 buf := new(bytes.Buffer)195196 manPreamble(buf, header, cmd, dashCommandName)
35 unmodified lines