1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
|
package mango
import (
"errors"
"fmt"
"sort"
"strings"
"time"
)
// ManPage represents a man page generator.
type ManPage struct {
Root Command
sections []Section
section uint
description string
longDescription string
}
// Command represents a command.
type Command struct {
Name string
Short string
Usage string
Example string
Flags map[string]Flag
Commands map[string]*Command
}
// Flag represents a flag.
type Flag struct {
Name string
Short string
Usage string
PFlag bool
}
// Section represents a section.
type Section struct {
Name string
Text string
}
// NewManPage returns a new ManPage generator instance.
func NewManPage(section uint, title string, description string) *ManPage {
root := NewCommand(title, "", "")
return &ManPage{
Root: *root,
section: section,
description: description,
}
}
// NewCommand returns a new Command.
func NewCommand(name string, short string, usage string) *Command {
return &Command{
Name: name,
Short: short,
Usage: usage,
Flags: make(map[string]Flag),
Commands: make(map[string]*Command),
}
}
// WithLongDescription sets the long description.
func (m *ManPage) WithLongDescription(desc string) *ManPage {
m.longDescription = desc
return m
}
// WithSection adds a section to the man page.
func (m *ManPage) WithSection(section string, text string) *ManPage {
m.sections = append(m.sections, Section{
Name: section,
Text: text,
})
return m
}
// AddFlag adds a flag to the command.
func (m *Command) AddFlag(f Flag) error {
if _, found := m.Flags[f.Name]; found {
return errors.New("duplicate flag: " + f.Name)
}
m.Flags[f.Name] = f
return nil
}
// AddCommand adds a sub-command.
func (m *Command) AddCommand(c *Command) error {
if _, found := m.Commands[c.Name]; found {
return errors.New("duplicate command: " + c.Name)
}
m.Commands[c.Name] = c
return nil
}
func (m ManPage) buildCommand(w Builder, c Command) {
if len(c.Flags) > 0 {
if c.Name == m.Root.Name {
w.Section("Options")
w.TaggedParagraph(-1)
} else {
w.TaggedParagraph(-1)
w.TextBold("OPTIONS")
w.Indent(4)
}
keys := make([]string, 0, len(c.Flags))
for k := range c.Flags {
keys = append(keys, k)
}
sort.Strings(keys)
for i, k := range keys {
opt := c.Flags[k]
if i > 0 {
w.TaggedParagraph(-1)
}
prefix := "-"
if opt.PFlag {
prefix = "--"
}
if opt.Short != "" {
w.TextBold(fmt.Sprintf("-%[2]s, %[1]s%[3]s", prefix, opt.Short, opt.Name))
} else {
w.TextBold(prefix + opt.Name)
}
w.EndSection()
w.Text(strings.ReplaceAll(opt.Usage, "\n", " "))
}
if c.Name == m.Root.Name {
} else {
w.IndentEnd()
}
}
if len(c.Commands) > 0 {
if c.Name == m.Root.Name {
w.Section("Commands")
w.TaggedParagraph(-1)
} else {
w.TaggedParagraph(-1)
w.TextBold("COMMANDS")
w.Indent(4)
}
keys := make([]string, 0, len(c.Commands))
for k := range c.Commands {
keys = append(keys, k)
}
sort.Strings(keys)
for i, k := range keys {
opt := c.Commands[k]
if i > 0 {
w.TaggedParagraph(-1)
}
w.TextBold(opt.Name)
if opt.Usage != "" {
w.Text(strings.TrimPrefix(opt.Usage, opt.Name))
}
w.Indent(4)
w.Text(strings.ReplaceAll(opt.Short, "\n", " "))
w.IndentEnd()
m.buildCommand(w, *opt)
}
if c.Name == m.Root.Name {
} else {
w.IndentEnd()
}
}
if c.Example != "" {
if c.Name == m.Root.Name {
w.Section("Examples")
w.TaggedParagraph(-1)
} else {
w.TaggedParagraph(-1)
w.TextBold("EXAMPLES")
w.Indent(4)
}
w.Text(c.Example)
if c.Name == m.Root.Name {
w.EndSection()
} else {
w.IndentEnd()
}
}
}
// Build generates the man page.
func (m ManPage) Build(w Builder) string {
/*
Common order:
- name
- synopsis
- description
- options
- exit status
- usage
- notes
- authors
- copyright
- see also
*/
w.Heading(m.section, m.Root.Name, m.description, time.Now())
w.Section("Name")
w.Text(m.Root.Name + " - " + m.description)
w.Section("Synopsis")
w.TextBold(m.Root.Name)
w.Text(" [")
w.TextItalic("options...")
w.Text("] [")
w.TextItalic("argument...")
w.Text("]")
w.Section("Description")
w.Text(m.longDescription)
m.buildCommand(w, m.Root)
for _, v := range m.sections {
w.Section(v.Name)
w.Text(v.Text)
}
return w.String()
}
|