Mangen: allow callers to specify header information
Before adoption: this commit came in with the history the project adopted, so no landing records it.
We previously had this weak argument called projectName which let you set a single part of a man page header. Instead do the best we can if the caller doesn't pass us anything, but let the caller specify anything they want.
authored by Eric Paris · committed by Eric Paris · parent cfecf13 · Browse files · 5 files·+110 added−21 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+110 added−21 removed
- other:+110 added−21 removed
Provenance
- Commit
edcf765- Tree
72b3671- Author
Eric Paris <eparis@redhat.com>- Committer
Eric Paris <eparis@redhat.com>- Parent
cfecf13
5 files changed+110 added−21 removed
added:examples_test.go+34 added
1+package cobra_test2+3+import (4+ "bytes"5+ "fmt"6+7+ "github.com/spf13/cobra"8+)9+10+func ExampleCommand_GenManTree() {11+ cmd := &cobra.Command{12+ Use: "test",13+ Short: "my test program",14+ }15+ header := &cobra.GenManHeader{16+ Title: "MINE",17+ Section: "3",18+ }19+ cmd.GenManTree(header, "/tmp")20+}21+22+func ExampleCommand_GenMan() {23+ cmd := &cobra.Command{24+ Use: "test",25+ Short: "my test program",26+ }27+ header := &cobra.GenManHeader{28+ Title: "MINE",29+ Section: "3",30+ }31+ out := new(bytes.Buffer)32+ cmd.GenMan(header, out)33+ fmt.Print(out.String())34+}modified:man_docs_test.go+6 added-2 removed
22 unmodified lines23 cmdRootWithRun.PersistentFlags().StringVarP(&flags2a, "rootflag", "r", "two", strtwoParentHelp)24 25 out := new(bytes.Buffer)26 27+ header := &GenManHeader{28+ Title: "Project",29+ Section: "2",30+ }31 // We generate on a subcommand so we have both subcommands and parents28- cmdEcho.GenMan("PROJECT", out)32+ cmdEcho.GenMan(header, out)33 found := out.String()34 35 // Make sure parent has - in CommandPath() in SEE ALSO:36 parentPath := cmdEcho.Parent().CommandPath()37 dashParentPath := strings.Replace(parentPath, " ", "-", -1)38 expected := translate(dashParentPath)35- expected = expected + "(1)"39+ expected = expected + "(" + header.Section + ")"40 checkStringContains(t, found, expected)41 42 // Our description43 expected = translate(cmdEcho.Name())25 unmodified linesmodified:man_docs.go+64 added-17 removed
23 unmodified lines24 mangen "github.com/cpuguy83/go-md2man/md2man"25 "github.com/spf13/pflag"26 )27 28+// GenManTree will call cmd.GenManTree(header, dir)28-func GenManTree(cmd *Command, projectName, dir string) {29- cmd.GenManTree(projectName, dir)29+func GenManTree(cmd *Command, header *GenManHeader, dir string) {30+ cmd.GenManTree(header, dir)31 }32 33+// GenManTree will generate a man page for this command and all decendants34+// in the directory given. The header may be nil. This function may not work35+// correctly if your command names have - in them. If you have `cmd` with two36+// subcmds, `sub` and `sub-third`. And `sub` has a subcommand called `third`37+// it is undefined which help output will be in the file `cmd-sub-third.1`.32-func (cmd *Command) GenManTree(projectName, dir string) {38+func (cmd *Command) GenManTree(header *GenManHeader, dir string) {39+ if header == nil {40+ header = &GenManHeader{}41+ }42 for _, c := range cmd.Commands() {43 if len(c.Deprecated) != 0 || c == cmd.helpCommand {44 continue45 }37- GenManTree(c, projectName, dir)46+ GenManTree(c, header, dir)47 }48 out := new(bytes.Buffer)49 41- cmd.GenMan(projectName, out)50+ cmd.GenMan(header, out)51 52 filename := cmd.CommandPath()53 filename = dir + strings.Replace(filename, " ", "-", -1) + ".1"54 outFile, err := os.Create(filename)8 unmodified lines63 os.Exit(1)64 }65 }66 58-func GenMan(cmd *Command, projectName string, out *bytes.Buffer) {59- cmd.GenMan(projectName, out)67+// GenManHeader is a lot like the .TH header at the start of man pages. These68+// include the title, section, date, source, and manual. We will use the69+// current time if Date if unset and will use "Auto generated by spf13/cobra"70+// if the Source is unset.71+type GenManHeader struct {72+ Title string73+ Section string74+ Date *time.Time75+ date string76+ Source string77+ Manual string78 }79 62-func (cmd *Command) GenMan(projectName string, out *bytes.Buffer) {80+// GenMan will call cmd.GenMan(header, out)81+func GenMan(cmd *Command, header *GenManHeader, out *bytes.Buffer) {82+ cmd.GenMan(header, out)83+}84 85+// GenMan will generate a man page for the given command in the out buffer.86+// The header argument may be nil, however obviously out may not.87+func (cmd *Command) GenMan(header *GenManHeader, out *bytes.Buffer) {88+ if header == nil {89+ header = &GenManHeader{}90+ }64- buf := genMarkdown(cmd, projectName)91+ buf := genMarkdown(cmd, header)92 final := mangen.Render(buf)93 out.Write(final)94 }95 96+func fillHeader(header *GenManHeader, name string) {97+ if header.Title == "" {98+ header.Title = name99+ }100+ if header.Section == "" {101+ header.Section = "1"102+ }103+ if header.Date == nil {104+ now := time.Now()105+ header.Date = &now106+ }107+ header.date = (*header.Date).Format("Jan 2006")108+ if header.Source == "" {109+ header.Source = "Auto generated by spf13/cobra"110+ }111+}112+69-func manPreamble(out *bytes.Buffer, projectName, name, short, long string) {70- fmt.Fprintf(out, `%% %s(1)113+func manPreamble(out *bytes.Buffer, header *GenManHeader, name, short, long string) {114+ fmt.Fprintf(out, `%% %s(%s)%s115+%% %s116+%% %s117 # NAME72-`, projectName)118+`, header.Title, header.Section, header.date, header.Source, header.Manual)119 fmt.Fprintf(out, "%s \\- %s\n\n", name, short)120 fmt.Fprintf(out, "# SYNOPSIS\n")121 fmt.Fprintf(out, "**%s** [OPTIONS]\n\n", name)122 fmt.Fprintf(out, "# DESCRIPTION\n")42 unmodified lines165 fmt.Fprintf(out, "\n")166 }167 }168 123-func genMarkdown(cmd *Command, projectName string) []byte {169+func genMarkdown(cmd *Command, header *GenManHeader) []byte {170+ fillHeader(header, cmd.Name())171 // something like `rootcmd subcmd1 subcmd2`172 commandName := cmd.CommandPath()173 // something like `rootcmd-subcmd1-subcmd2`174 dashCommandName := strings.Replace(commandName, " ", "-", -1)5 unmodified lines180 if len(long) == 0 {181 long = short182 }183 137- manPreamble(buf, projectName, commandName, short, long)184+ manPreamble(buf, header, commandName, short, long)185 manPrintOptions(buf, cmd)186 187 if len(cmd.Example) > 0 {188 fmt.Fprintf(buf, "# EXAMPLE\n")4 unmodified lines193 fmt.Fprintf(buf, "# SEE ALSO\n")194 if cmd.HasParent() {195 parentPath := cmd.Parent().CommandPath()196 dashParentPath := strings.Replace(parentPath, " ", "-", -1)150- fmt.Fprintf(buf, "**%s(1)**, ", dashParentPath)197+ fmt.Fprintf(buf, "**%s(%s)**, ", dashParentPath, header.Section)198 }199 200 children := cmd.Commands()201 sort.Sort(byName(children))202 for _, c := range children {203 if len(c.Deprecated) != 0 || c == cmd.helpCommand {204 continue205 }159- fmt.Fprintf(buf, "**%s-%s(1)**, ", dashCommandName, c.Name())206+ fmt.Fprintf(buf, "**%s-%s(%s)**, ", dashCommandName, c.Name(), header.Section)207 }208 fmt.Fprintf(buf, "\n")209 }210 164- fmt.Fprintf(buf, "# HISTORY\n%s Auto generated by spf13/cobra\n", time.Now().UTC())211+ fmt.Fprintf(buf, "# HISTORY\n%s Auto generated by spf13/cobra\n", header.Date.Format("2-Jan-2006"))212 return buf.Bytes()213 }modified:man_docs.md+5 added-1 removed
12 unmodified lines131415161718192017212223242512 unmodified lines cmd := &cobra.Command{ Use: "test", Short: "my test program", } header := &cobra.GenManHeader{ Title: "MINE", Section: "3", } cmd.GenManTree("/tmp") cmd.GenManTree(header, "/tmp")}```
That will get you a man page `/tmp/test.1`modified:md_docs.go+1 added-1 removed
106 unmodified lines107 }108 fmt.Fprintf(out, "\n")109 }110 111- fmt.Fprintf(out, "###### Auto generated by spf13/cobra at %s\n", time.Now().UTC())111+ fmt.Fprintf(out, "###### Auto generated by spf13/cobra on %s\n", time.Now().Format("2-Jan-2006"))112 }113 114 func GenMarkdownTree(cmd *Command, dir string) {115 cmd.GenMarkdownTree(dir)42 unmodified lines