Skip to content
Myr

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

6 files+139 added−47 removed

modified:cobra_test.go+13 added-5 removed
263 unmodified lines
264 fmt.Fprintf(out, "Unexpected response.\nExpecting to contain: \n %q\nGot:\n %q\n", expected, found)265 t.Errorf(out.String())266}267268func checkStringContains(t *testing.T, found, expected string) {269 if !strings.Contains(found, expected) {270 logErr(t, found, expected)271 }272}273274func 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}277278func checkStringOmits(t *testing.T, found, expected string) {279 if strings.Contains(found, expected) {280 logErr(t, found, expected)281 }282}283284func 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}287288func checkOutputContains(t *testing.T, c *Command, check string) {289 buf := new(bytes.Buffer)
720 unmodified lines
added:examples_test.go+34 added
1package cobra_test23import (4 "bytes"5 "fmt"67 "github.com/spf13/cobra"8)910func 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}2122func 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 lines
23 cmdRootWithRun.PersistentFlags().StringVarP(&flags2a, "rootflag", "r", "two", strtwoParentHelp)2425 out := new(bytes.Buffer)2627 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()3435 // 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)4142 // 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)4546 // 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)4950 // 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)5354 // 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)5758 // 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)6162 // 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)6566 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 lines
24 mangen "github.com/cpuguy83/go-md2man/md2man"25 "github.com/spf13/pflag"26)2728// GenManTree will call cmd.GenManTree(header, dir)28func GenManTree(cmd *Command, projectName, dir string) {29 cmd.GenManTree(projectName, dir)29func GenManTree(cmd *Command, header *GenManHeader, dir string) {30 cmd.GenManTree(header, dir)31}3233// 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`.32func (cmd *Command) GenManTree(projectName, dir string) {38func (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)4941 cmd.GenMan(projectName, out)50 cmd.GenMan(header, out)5152 filename := cmd.CommandPath()53 filename = dir + strings.Replace(filename, " ", "-", -1) + ".1"54 outFile, err := os.Create(filename)
8 unmodified lines
63 os.Exit(1)64 }65}6658func 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.71type GenManHeader struct {72 Title string73 Section string74 Date *time.Time75 date string76 Source string77 Manual string78}7962func (cmd *Command) GenMan(projectName string, out *bytes.Buffer) {80// GenMan will call cmd.GenMan(header, out)81func GenMan(cmd *Command, header *GenManHeader, out *bytes.Buffer) {82 cmd.GenMan(header, out)83}8485// 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.87func (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}9596func 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}11269func manPreamble(out *bytes.Buffer, projectName, name, short, long string) {70 fmt.Fprintf(out, `%% %s(1)113func 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 lines
165 fmt.Fprintf(out, "\n")166 }167}168123func genMarkdown(cmd *Command, projectName string) []byte {169func 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 lines
180 if len(long) == 0 {181 long = short182 }183137 manPreamble(buf, projectName, commandName, short, long)184 manPreamble(buf, header, commandName, short, long)185 manPrintOptions(buf, cmd)186187 if len(cmd.Example) > 0 {188 fmt.Fprintf(buf, "# EXAMPLE\n")
2 unmodified lines
191192 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 }199200 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 }210162 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
modified:md_docs.go+1 added-1 removed
106 unmodified lines
107 }108 fmt.Fprintf(out, "\n")109 }110111 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}113114func GenMarkdownTree(cmd *Command, dir string) {115 cmd.GenMarkdownTree(dir)
42 unmodified lines