| 1 |
package flags |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"io" |
| 6 |
"strings" |
| 7 |
"time" |
| 8 |
) |
| 9 |
|
| 10 |
func (x *Parser) formatForMan(wr io.Writer, s string) { |
| 11 |
for { |
| 12 |
idx := strings.IndexRune(s, '`') |
| 13 |
|
| 14 |
if idx < 0 { |
| 15 |
fmt.Fprintf(wr, "%s", s) |
| 16 |
break |
| 17 |
} |
| 18 |
|
| 19 |
fmt.Fprintf(wr, "%s", s[:idx]) |
| 20 |
|
| 21 |
s = s[idx+1:] |
| 22 |
idx = strings.IndexRune(s, '\'') |
| 23 |
|
| 24 |
if idx < 0 { |
| 25 |
fmt.Fprintf(wr, "%s", s) |
| 26 |
break |
| 27 |
} |
| 28 |
|
| 29 |
fmt.Fprintf(wr, "\\fB%s\\fP", s[:idx]) |
| 30 |
s = s[idx+1:] |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
func (x *Parser) writeManPageOptions(wr io.Writer, groups ...*Group) { |
| 35 |
for _, group := range groups { |
| 36 |
for _, opt := range group.Options { |
| 37 |
fmt.Fprintln(wr, ".TP") |
| 38 |
fmt.Fprintf(wr, "\\fB") |
| 39 |
|
| 40 |
if opt.ShortName != 0 { |
| 41 |
fmt.Fprintf(wr, "-%c", opt.ShortName) |
| 42 |
} |
| 43 |
|
| 44 |
if len(opt.LongName) != 0 { |
| 45 |
if opt.ShortName != 0 { |
| 46 |
fmt.Fprintf(wr, ", ") |
| 47 |
} |
| 48 |
|
| 49 |
fmt.Fprintf(wr, "--%s", opt.LongName) |
| 50 |
} |
| 51 |
|
| 52 |
fmt.Fprintln(wr, "\\fP") |
| 53 |
x.formatForMan(wr, opt.Description) |
| 54 |
fmt.Fprintln(wr, "") |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
func (x *Parser) writeManPageCommand(wr io.Writer, command string, grp *Group) { |
| 60 |
fmt.Fprintf(wr, ".SS %s\n", command) |
| 61 |
fmt.Fprintln(wr, grp.Name) |
| 62 |
|
| 63 |
if len(grp.LongDescription) > 0 { |
| 64 |
fmt.Fprintln(wr, "") |
| 65 |
|
| 66 |
cmdstart := fmt.Sprintf("The %s command", command) |
| 67 |
|
| 68 |
if strings.HasPrefix(grp.LongDescription, cmdstart) { |
| 69 |
fmt.Fprintf(wr, "The \\fI%s\\fP command", command) |
| 70 |
|
| 71 |
x.formatForMan(wr, grp.LongDescription[len(cmdstart):]) |
| 72 |
fmt.Fprintln(wr, "") |
| 73 |
} else { |
| 74 |
x.formatForMan(wr, grp.LongDescription) |
| 75 |
fmt.Fprintln(wr, "") |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
x.writeManPageOptions(wr, grp) |
| 80 |
|
| 81 |
for k, v := range grp.Commander.Commands { |
| 82 |
x.writeManPageCommand(wr, command+" "+k, v) |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
func (x *Parser) WriteManPage(wr io.Writer, description string) { |
| 89 |
t := time.Now() |
| 90 |
|
| 91 |
fmt.Fprintf(wr, ".TH %s 1 \"%s\"\n", x.ApplicationName, t.Format("2 January 2006")) |
| 92 |
fmt.Fprintln(wr, ".SH NAME") |
| 93 |
fmt.Fprintf(wr, "%s \\- %s\n", x.ApplicationName, x.Description) |
| 94 |
fmt.Fprintln(wr, ".SH SYNOPSIS") |
| 95 |
fmt.Fprintf(wr, "\\fB%s\\fP %s\n", x.ApplicationName, x.Usage) |
| 96 |
fmt.Fprintln(wr, ".SH DESCRIPTION") |
| 97 |
|
| 98 |
x.formatForMan(wr, description) |
| 99 |
fmt.Fprintln(wr, "") |
| 100 |
|
| 101 |
fmt.Fprintln(wr, ".SH OPTIONS") |
| 102 |
|
| 103 |
x.writeManPageOptions(wr, x.Groups...) |
| 104 |
|
| 105 |
if len(x.Commander.Commands) > 0 { |
| 106 |
fmt.Fprintln(wr, ".SH COMMANDS") |
| 107 |
|
| 108 |
for k, v := range x.Commander.Commands { |
| 109 |
x.writeManPageCommand(wr, k, v) |
| 110 |
} |
| 111 |
} |
| 112 |
} |