Merge pull request #145 from eparis/man-cleanups
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Enhance Man Page Generation
authored by Eric Paris · committed by Eric Paris · parents b625a4d, edcf765 · Browse files · 6 files·+139 added−47 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+139 added−47 removed
- other:+139 added−47 removed
Provenance
- Commit
75d96d7- Tree
bf59974- Author
Eric Paris <eparis@redhat.com>- Committer
Eric Paris <eparis@redhat.com>- Parent 1
b625a4d- Parent 2
edcf765
6 files changed+139 added−47 removed
modified:cobra_test.go+13 added-5 removed
263 unmodified lines264 fmt.Fprintf(out, "Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)265 t.Errorf(out.String())266 }267 268+func checkStringContains(t *testing.T, found, expected string) {269+ if !strings.Contains(found, expected) {270+ logErr(t, found, expected)271+ }272+}273+274 func checkResultContains(t *testing.T, x resulter, check string) {269- if !strings.Contains(x.Output, check) {270- logErr(t, x.Output, check)275+ checkStringContains(t, x.Output, check)276+}277+278+func checkStringOmits(t *testing.T, found, expected string) {279+ if strings.Contains(found, expected) {280+ logErr(t, found, expected)281 }282 }283 284 func checkResultOmits(t *testing.T, x resulter, check string) {275- if strings.Contains(x.Output, check) {276- logErr(t, x.Output, check)285+ checkStringOmits(t, x.Output, check)277- }286 }287 288 func checkOutputContains(t *testing.T, c *Command, check string) {289 buf := new(bytes.Buffer)720 unmodified linesadded: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+20 added-23 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)39+ expected = expected + "(" + header.Section + ")"40+ checkStringContains(t, found, expected)41+42 // Our description32- expected := translate(cmdEcho.Name())33- if !strings.Contains(found, expected) {34- t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)35- }43+ expected = translate(cmdEcho.Name())44+ checkStringContains(t, found, expected)45 46 // Better have our example47 expected = translate(cmdEcho.Name())39- if !strings.Contains(found, expected) {40- t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)41- }48+ checkStringContains(t, found, expected)49 50 // A local flag51 expected = "boolone"45- if !strings.Contains(found, expected) {46- t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)47- }52+ checkStringContains(t, found, expected)53 54 // persistent flag on parent55 expected = "rootflag"51- if !strings.Contains(found, expected) {52- t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)53- }56+ checkStringContains(t, found, expected)57 58 // We better output info about our parent59 expected = translate(cmdRootWithRun.Name())57- if !strings.Contains(found, expected) {58- t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)59- }60+ checkStringContains(t, found, expected)61 62 // And about subcommands63 expected = translate(cmdEchoSub.Name())63- if !strings.Contains(found, expected) {64- t.Errorf("Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)65- }64+ checkStringContains(t, found, expected)65 66 unexpected := translate(cmdDeprecated.Name())68- if strings.Contains(found, unexpected) {69- t.Errorf("Unexpected response.\nFound: %v\nBut should not have!!\n", unexpected)70- }67+ checkStringOmits(t, found, unexpected)68 }modified:man_docs.go+66 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")2 unmodified lines191 192 if cmd.hasSeeAlso() {193 fmt.Fprintf(buf, "# SEE ALSO\n")194 if cmd.HasParent() {148- fmt.Fprintf(buf, "**%s(1)**, ", cmd.Parent().CommandPath())195+ parentPath := cmd.Parent().CommandPath()196+ dashParentPath := strings.Replace(parentPath, " ", "-", -1)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 }157- 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 162- 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