Skip to content
Myr

doc: Refactor man docs

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

Performance impact (very strange): benchmark old ns/op new ns/op delta BenchmarkGenManToFile-4 30037 29001 -3.45% benchmark old allocs new allocs delta BenchmarkGenManToFile-4 77 89 +15.58% benchmark old bytes new bytes delta BenchmarkGenManToFile-4 9075 9651 +6.35%

authored by Albert Nigmatzianov · committed by Albert Nigmatzianov · parent ea3c5be · Browse files · 2 files+46 added−29 removed
This commit

State

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

Diff stats

2 files+46 added−29 removed

  • other:+46 added−29 removed

2 files+46 added−29 removed

modified:doc/man_docs_test.go+17 added
195 unmodified lines
196 }197198 return fmt.Errorf("AssertNextLineEquals: hit EOF before finding %#v", expectedLine)199}200201func BenchmarkGenManToFile(b *testing.B) {202 c := initializeWithRootCmd()203 file, err := ioutil.TempFile("", "")204 if err != nil {205 b.Fatal(err)206 }207 defer os.Remove(file.Name())208 defer file.Close()209210 b.ResetTimer()211 for i := 0; i < b.N; i++ {212 if err := GenMan(c, nil, file); err != nil {213 b.Fatal(err)214 }215 }216}
modified:doc/man_docs.go+29 added-29 removed
21 unmodified lines
22 "sort"23 "strings"24 "time"2526 mangen "github.com/cpuguy83/go-md2man/md2man"26 "github.com/cpuguy83/go-md2man/md2man"27 "github.com/spf13/cobra"28 "github.com/spf13/pflag"29)30
73 unmodified lines
104 }105 fillHeader(header, cmd.CommandPath())106107 b := genMan(cmd, header)108 _, err := w.Write(mangen.Render(b))108 _, err := w.Write(md2man.Render(b))109 return err110}111112func fillHeader(header *GenManHeader, name string) {
12 unmodified lines
125 header.Source = "Auto generated by spf13/cobra"126 }127}128129func manPreamble(out io.Writer, header *GenManHeader, cmd *cobra.Command, dashedName string) {129func manPreamble(buf *bytes.Buffer, header *GenManHeader, cmd *cobra.Command, dashedName string) {130 description := cmd.Long131 if len(description) == 0 {132 description = cmd.Short133 }134135 fmt.Fprintf(out, `%% %s(%s)%s135 buf.WriteString(fmt.Sprintf(`%% %s(%s)%s136%% %s137%% %s138# NAME139`, header.Title, header.Section, header.date, header.Source, header.Manual)140 fmt.Fprintf(out, "%s \\- %s\n\n", dashedName, cmd.Short)141 fmt.Fprintf(out, "# SYNOPSIS\n")142 fmt.Fprintf(out, "**%s**\n\n", cmd.UseLine())143 fmt.Fprintf(out, "# DESCRIPTION\n")144 fmt.Fprintf(out, "%s\n\n", description)139`, header.Title, header.Section, header.date, header.Source, header.Manual))140 buf.WriteString(fmt.Sprintf("%s \\- %s\n\n", dashedName, cmd.Short))141 buf.WriteString("# SYNOPSIS\n")142 buf.WriteString(fmt.Sprintf("**%s**\n\n", cmd.UseLine()))143 buf.WriteString("# DESCRIPTION\n")144 buf.WriteString(description + "\n\n")145}146147func manPrintFlags(out io.Writer, flags *pflag.FlagSet) {147func manPrintFlags(buf *bytes.Buffer, flags *pflag.FlagSet) {148 flags.VisitAll(func(flag *pflag.Flag) {149 if len(flag.Deprecated) > 0 || flag.Hidden {150 return151 }
3 unmodified lines
155 } else {156 format = fmt.Sprintf("**--%s**", flag.Name)157 }158 if len(flag.NoOptDefVal) > 0 {159 format = format + "["159 format += "["160 }161 if flag.Value.Type() == "string" {162 // put quotes on the value163 format = format + "=%q"163 format += "=%q"164 } else {165 format = format + "=%s"165 format += "=%s"166 }167 if len(flag.NoOptDefVal) > 0 {168 format = format + "]"168 format += "]"169 }170 format = format + "\n\t%s\n\n"171 fmt.Fprintf(out, format, flag.DefValue, flag.Usage)170 format += "\n\t%s\n\n"171 buf.WriteString(fmt.Sprintf(format, flag.DefValue, flag.Usage))172 })173}174175func manPrintOptions(out io.Writer, command *cobra.Command) {175func manPrintOptions(buf *bytes.Buffer, command *cobra.Command) {176 flags := command.NonInheritedFlags()177 if flags.HasFlags() {178 fmt.Fprintf(out, "# OPTIONS\n")179 manPrintFlags(out, flags)180 fmt.Fprintf(out, "\n")178 buf.WriteString("# OPTIONS\n")179 manPrintFlags(buf, flags)180 buf.WriteString("\n")181 }182 flags = command.InheritedFlags()183 if flags.HasFlags() {184 fmt.Fprintf(out, "# OPTIONS INHERITED FROM PARENT COMMANDS\n")185 manPrintFlags(out, flags)186 fmt.Fprintf(out, "\n")184 buf.WriteString("# OPTIONS INHERITED FROM PARENT COMMANDS\n")185 manPrintFlags(buf, flags)186 buf.WriteString("\n")187 }188}189190func genMan(cmd *cobra.Command, header *GenManHeader) []byte {
4 unmodified lines
195196 manPreamble(buf, header, cmd, dashCommandName)197 manPrintOptions(buf, cmd)198 if len(cmd.Example) > 0 {199 fmt.Fprintf(buf, "# EXAMPLE\n")200 fmt.Fprintf(buf, "```\n%s\n```\n", cmd.Example)199 buf.WriteString("# EXAMPLE\n")200 buf.WriteString(fmt.Sprintf("```\n%s\n```\n", cmd.Example))201 }202 if hasSeeAlso(cmd) {203 fmt.Fprintf(buf, "# SEE ALSO\n")203 buf.WriteString("# SEE ALSO\n")204 seealsos := make([]string, 0)205 if cmd.HasParent() {206 parentPath := cmd.Parent().CommandPath()207 dashParentPath := strings.Replace(parentPath, " ", "-", -1)
13 unmodified lines
221 }222 seealso := fmt.Sprintf("**%s-%s(%s)**", dashCommandName, c.Name(), header.Section)223 seealsos = append(seealsos, seealso)224 }225 fmt.Fprintf(buf, "%s\n", strings.Join(seealsos, ", "))225 buf.WriteString(strings.Join(seealsos, ", ") + "\n")226 }227 if !cmd.DisableAutoGenTag {228 fmt.Fprintf(buf, "# HISTORY\n%s Auto generated by spf13/cobra\n", header.Date.Format("2-Jan-2006"))228 buf.WriteString(fmt.Sprintf("# HISTORY\n%s Auto generated by spf13/cobra\n", header.Date.Format("2-Jan-2006")))229 }230 return buf.Bytes()231}