-
Notifications
You must be signed in to change notification settings - Fork 3
/
help.go
142 lines (129 loc) · 4.28 KB
/
help.go
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
// Copyright (c) 2016 Bob Ziuchkovski
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package writ
import (
"bytes"
"fmt"
"strings"
"text/template"
)
var templateFuncs = map[string]interface{}{
"formatCommand": formatCommand,
"formatOption": formatOption,
"wrapText": wrapText,
}
// The Help type is used for presentation purposes only, and does not affect
// argument parsing.
//
// The Command.ExitHelp() and Command.WriteHelp() methods execute the
// template assigned to the Template field, passing the Command as input.
// If the Template field is nil, the writ package's default template is used.
type Help struct {
OptionGroups []OptionGroup
CommandGroups []CommandGroup
// Optional
Template *template.Template // Used to render output
Usage string // Short message displayed at the top of output
Header string // Displayed after Usage
Footer string // Displayed at the end of output
}
// OptionGroup is used to customize help output. It groups related Options
// for output. When New() parses an input spec, it creates a single OptionGroup
// for all parsed options that have descriptions.
type OptionGroup struct {
Options []*Option
// Optional
Name string // Not displayed; for matching purposes within the template
Header string // Displayed before the group
Footer string // Displayed after the group
}
// CommandGroup is used to customize help output. It groups related Commands
// for output. When New() parses an input spec, it creates a single CommandGroup
// for all parsed commands that have descriptions.
type CommandGroup struct {
Commands []*Command
// Optional
Name string // Not displayed; for matching purposes within the template
Header string // Displayed before the group
Footer string // Displayed after the group
}
func formatOption(o *Option) string {
var placeholder string
if !o.Flag {
placeholder = o.Placeholder
if placeholder == "" {
placeholder = "ARG"
}
}
names := ""
short := o.ShortNames()
long := o.LongNames()
for i, s := range short {
names += "-" + s
if (i < len(short)-1) || len(long) != 0 {
names += ", "
}
}
if len(long) == 0 && placeholder != "" {
names += " " + placeholder
}
for i, l := range long {
names += "--" + l
if i < len(long)-1 {
names += ", "
} else if placeholder != "" {
names += "=" + placeholder
}
}
formatted := fmt.Sprintf(" %-24s %s", names, o.Description)
return wrapText(formatted, 80, 28)
}
func formatCommand(c *Command) string {
formatted := fmt.Sprintf(" %-24s %s", c.Name, c.Description)
return wrapText(formatted, 80, 28)
}
// This is a pretty naiive implementation, but it's late and I'm tired
// TODO: cleanup and probably try to wrap on nearest space or punctuation
func wrapText(s string, width int, indent int) string {
buf := bytes.NewBuffer(nil)
runes := []rune(s)
linelen, i := 0, 0
for i < len(runes) {
if runes[i] == '\n' {
buf.WriteString("\n")
if i < len(runes) {
buf.WriteString(strings.Repeat(" ", indent))
linelen = indent
}
} else if linelen == width {
buf.WriteString("\n")
if i < len(runes) {
buf.WriteString(strings.Repeat(" ", indent))
linelen = indent
}
buf.WriteRune(runes[i])
} else {
buf.WriteRune(runes[i])
}
i++
linelen++
}
return buf.String()
}