File: man.go

package info (click to toggle)
golang-github-svent-go-flags 1-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 224 kB
  • ctags: 186
  • sloc: sh: 13; makefile: 7
file content (112 lines) | stat: -rw-r--r-- 2,379 bytes parent folder | download | duplicates (2)
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
package flags

import (
	"fmt"
	"io"
	"strings"
	"time"
)

func (x *Parser) formatForMan(wr io.Writer, s string) {
	for {
		idx := strings.IndexRune(s, '`')

		if idx < 0 {
			fmt.Fprintf(wr, "%s", s)
			break
		}

		fmt.Fprintf(wr, "%s", s[:idx])

		s = s[idx+1:]
		idx = strings.IndexRune(s, '\'')

		if idx < 0 {
			fmt.Fprintf(wr, "%s", s)
			break
		}

		fmt.Fprintf(wr, "\\fB%s\\fP", s[:idx])
		s = s[idx+1:]
	}
}

func (x *Parser) writeManPageOptions(wr io.Writer, groups ...*Group) {
	for _, group := range groups {
		for _, opt := range group.Options {
			fmt.Fprintln(wr, ".TP")
			fmt.Fprintf(wr, "\\fB")

			if opt.ShortName != 0 {
				fmt.Fprintf(wr, "-%c", opt.ShortName)
			}

			if len(opt.LongName) != 0 {
				if opt.ShortName != 0 {
					fmt.Fprintf(wr, ", ")
				}

				fmt.Fprintf(wr, "--%s", opt.LongName)
			}

			fmt.Fprintln(wr, "\\fP")
			x.formatForMan(wr, opt.Description)
			fmt.Fprintln(wr, "")
		}
	}
}

func (x *Parser) writeManPageCommand(wr io.Writer, command string, grp *Group) {
	fmt.Fprintf(wr, ".SS %s\n", command)
	fmt.Fprintln(wr, grp.Name)

	if len(grp.LongDescription) > 0 {
		fmt.Fprintln(wr, "")

		cmdstart := fmt.Sprintf("The %s command", command)

		if strings.HasPrefix(grp.LongDescription, cmdstart) {
			fmt.Fprintf(wr, "The \\fI%s\\fP command", command)

			x.formatForMan(wr, grp.LongDescription[len(cmdstart):])
			fmt.Fprintln(wr, "")
		} else {
			x.formatForMan(wr, grp.LongDescription)
			fmt.Fprintln(wr, "")
		}
	}

	x.writeManPageOptions(wr, grp)

	for k, v := range grp.Commander.Commands {
		x.writeManPageCommand(wr, command+" "+k, v)
	}
}

// WriteManPage writes a basic man page in groff format to the specified
// writer.
func (x *Parser) WriteManPage(wr io.Writer, description string) {
	t := time.Now()

	fmt.Fprintf(wr, ".TH %s 1 \"%s\"\n", x.ApplicationName, t.Format("2 January 2006"))
	fmt.Fprintln(wr, ".SH NAME")
	fmt.Fprintf(wr, "%s \\- %s\n", x.ApplicationName, x.Description)
	fmt.Fprintln(wr, ".SH SYNOPSIS")
	fmt.Fprintf(wr, "\\fB%s\\fP %s\n", x.ApplicationName, x.Usage)
	fmt.Fprintln(wr, ".SH DESCRIPTION")

	x.formatForMan(wr, description)
	fmt.Fprintln(wr, "")

	fmt.Fprintln(wr, ".SH OPTIONS")

	x.writeManPageOptions(wr, x.Groups...)

	if len(x.Commander.Commands) > 0 {
		fmt.Fprintln(wr, ".SH COMMANDS")

		for k, v := range x.Commander.Commands {
			x.writeManPageCommand(wr, k, v)
		}
	}
}