-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusage.go
137 lines (125 loc) · 3.05 KB
/
usage.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
package main
import (
"flag"
"fmt"
"io"
"strings"
"github.com/muesli/termenv"
)
type Usage struct {
Title string
Description string
commands [][]string
inputs [][]string
flags [][]string
Entrypoint []string
output *termenv.Output
}
func (u Usage) printSection(b io.Writer, header, content string) {
o := u.output
content = strings.ReplaceAll(content, "\n", "\n ")
content = strings.TrimSuffix(content, " ")
fmt.Fprintf(b, "%s\n %s\n",
o.String(header).Bold(),
content,
)
}
func (u Usage) printInstructions(b io.Writer, entries [][]string, header, prefix string) {
// Each entry in entries begins with the description followed by additional names
s := strings.Builder{}
col := 15
for i, entry := range entries {
name := strings.Join(entry[1:], ", ")
col = max(col, len([]rune(name)))
entries[i] = []string{entry[0], name}
}
col = col + 5
format := fmt.Sprintf("%%-%ds %%s\n", col)
for _, entry := range entries {
desc := entry[0]
name := fmt.Sprintf("%s%s", prefix, entry[1])
fmt.Fprintf(&s, format, name, desc)
}
u.printSection(b, header, s.String())
}
func (u Usage) usage() string {
params := []string{}
if len(u.Entrypoint) > 0 {
params = append(params, u.Entrypoint[0])
}
if len(u.flags) > 0 {
params = append(params, "[flags]")
}
if len(u.Entrypoint) > 1 {
params = append(params, u.Entrypoint[1:]...)
} else {
params = append(params, "<config>")
}
if len(u.commands) > 0 {
params = append(params, "<commands>")
}
if len(u.inputs) > 0 {
params = append(params, "[inputs]")
}
return strings.Join(params, " ")
}
func (u Usage) String() string {
b := strings.Builder{}
o := u.output
fmt.Fprintf(&b, "\n")
if s := u.Title; s != "" {
fmt.Fprintf(&b, "%s\n\n",
o.String(s).Underline(),
)
}
if s := u.Description; s != "" {
fmt.Fprintf(&b, "%s\n\n", s)
}
if b.Len() > 0 {
fmt.Fprintf(&b, "\n")
}
if s := u.usage(); s != "" {
u.printSection(&b, "USAGE", s)
b.WriteString("\n")
}
if len(u.commands) > 0 {
u.printInstructions(&b, u.commands, "COMMANDS", "")
}
if len(u.inputs) > 0 {
u.printInstructions(&b, u.inputs, "INPUTS", "-")
}
if len(u.flags) > 0 {
u.printInstructions(&b, u.flags, "FLAGS", "-")
}
b.WriteString("\n")
return b.String()
}
func (u Usage) Print() error {
_, err := u.output.WriteString(u.String())
return err
}
func (u Usage) ImportCommands(commands []ConfigCommand) Usage {
for _, command := range commands {
u.commands = append(u.commands, append([]string{command.Description, command.Name}, command.Aliases...))
}
return u
}
func (u Usage) ImportInputs(inputs []ConfigInput) Usage {
for _, input := range inputs {
u.inputs = append(u.inputs, []string{input.Description, input.Name})
}
return u
}
func (u Usage) ImportCommandSet(cs CommandSet) Usage {
return u.ImportCommands(cs.Subcommands()).ImportInputs(cs.Inputs())
}
func NewUsage(tty io.Writer) Usage {
u := Usage{
Title: "ILC",
output: termenv.NewOutput(tty),
}
mainFlagSet.VisitAll(func(f *flag.Flag) {
u.flags = append(u.flags, []string{f.Usage, f.Name})
})
return u
}