-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelp.go
150 lines (119 loc) · 3.27 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
143
144
145
146
147
148
149
150
package cli
import (
"bytes"
"context"
_ "embed"
"fmt"
"io"
"os"
"strings"
"text/tabwriter"
"text/template"
"github.com/bobg/errors"
"github.com/samber/lo"
)
//go:embed help.tmpl
var rawHelpTemplate string
var helpTemplate = template.Must(template.New("help").Parse(rawHelpTemplate))
func helpHandler(ctx context.Context) error {
command, err := commandFromContext(ctx)
if err != nil {
return errors.Wrap(err, "getting command for help")
}
if err := command.renderHelp(os.Stdout); err != nil {
return errors.Wrap(err, "rendering help")
}
return nil
}
func (c *Command) helpContext() helpContext {
return helpContext{command: c}
}
func (c *Command) renderHelp(w io.Writer) error {
if err := helpTemplate.Execute(w, c.helpContext()); err != nil {
return errors.Wrap(err, "help template")
}
return nil
}
type helpContext struct {
command *Command
}
func (h helpContext) RootName() string {
return h.command.root().name
}
func (h helpContext) Version() string {
return h.command.findVersion()
}
func (h helpContext) RootDescription() string {
return h.command.root().description
}
func (h helpContext) QualifiedName() string {
return h.command.qualifiedName()
}
func (h helpContext) SubCommands() []*Command {
return h.command.subCommands
}
func (h helpContext) Arguments() []*Argument {
return h.command.arguments
}
func (h helpContext) Flags() []*Flag {
return h.command.flagsUpToRoot()
}
func (h helpContext) SubCommandsTable() (string, error) {
return tableToString(lo.Map(h.SubCommands(), func(command *Command, _ int) []string {
return []string{
"",
fmt.Sprintf("%s: %s", command.name, command.description),
}
}))
}
func (h helpContext) ArgumentList() string {
return strings.Join(lo.Map(h.Arguments(), func(argument *Argument, _ int) string { return argument.inBrackets() }), " ")
}
func (h helpContext) ArgumentTable() (string, error) {
return tableToString(lo.Map(h.Arguments(), func(argument *Argument, _ int) []string {
return []string{
"",
argument.inBrackets(),
argument.description,
fmt.Sprintf("(type: %T)", argument.parser.Type()),
}
}))
}
func (h helpContext) FlagTable() (string, error) {
return tableToString(lo.FilterMap(h.Flags(), func(flag *Flag, _ int) ([]string, bool) {
if flag.isHidden {
return nil, false
}
longs := lo.Map(append([]string{flag.name}, flag.aliases...), func(long string, _ int) string { return fmt.Sprintf("--%s", long) })
shorts := ""
if len(flag.shorts) > 0 {
shorts = fmt.Sprintf("-%s", string(flag.shorts))
}
return []string{
"",
strings.Join(longs, " "),
shorts,
flag.description,
fmt.Sprintf("(type: %T, default: %q)", flag.parser.Type(), fmt.Sprint(flag.defaultValue)),
}, true
}))
}
func tableToString(rows [][]string) (string, error) {
buffer := new(bytes.Buffer)
if err := writeTable(buffer, rows); err != nil {
return "", errors.Wrap(err, "writing table to string")
}
return buffer.String(), nil
}
func writeTable(w io.Writer, rows [][]string) error {
table := tabwriter.NewWriter(w, 0, 0, 2, ' ', 0)
for _, row := range rows {
if _, err := fmt.Fprintln(table, strings.Join(row, "\t")); err != nil {
return errors.Wrap(err, "writing table row")
}
}
if err := table.Flush(); err != nil {
return errors.Wrap(err, "flushing table")
}
return nil
}