-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.go
213 lines (173 loc) · 5.1 KB
/
ui.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package cli
import (
"fmt"
"strings"
"github.com/awee-ai/structs"
)
// find command or any level of subcommand
func findCommandByArgs(commands []Command[any], args []string) Command[any] {
if len(args) == 0 {
return nil
}
for _, cmd := range commands {
if cmd.Name("") == args[0] {
if len(args) == 1 {
return cmd
}
return findCommandByArgs(cmd.Commands(), args[1:])
}
}
return nil
}
func displaySingleCommandHelp(appName string, commands []Command[any], command []string) []string {
help := []string{
fmt.Sprintf(`Usage: ` + appName + ` <command> <subcommand> [args] [options]`),
}
help = append(help, ``)
cmd := findCommandByArgs(commands, command)
if cmd == nil {
fmt.Println("Command not found")
return []string{}
}
help = append(help, cmd.Help())
line := fmt.Sprintf(`$ %s`, strings.Join(command, " "))
help = append(help, line)
options, _ := helpOptions(cmd.Options())
if len(options) > 0 {
help = append(help, options...)
}
return help
}
func displayAllCommandsHelp(appName string, commands []Command[any]) []string {
help := []string{
fmt.Sprintf(`Usage: ` + appName + ` <command> <subcommand> [args] [options]`),
}
help = append(help, ``)
help = append(help, `Options can be passed before or after the command and subcommand.`)
help = append(help, `Both -[opt] <arg> and --[opt]=<arg> are supported.`)
help = append(help, `Boolean flags can be passed without an argument to set them to true.`)
help = append(help, ``)
help = append(help, `Commands:`)
longestName := getLongestName(commands)
for _, cmd := range commands {
name := cmd.Name("")
if len(cmd.Commands()) > 0 {
// help = append(help, ``)
}
help = append(help, fmt.Sprintf(` %s %s%s`, name, pad(name, longestName), cmd.Help()))
if len(cmd.Commands()) > 0 {
for _, subCmd := range cmd.Commands() {
subName := name + " " + subCmd.Name("")
help = append(help, ` `+subName+``+pad(subName, longestName)+` `+subCmd.Help())
}
}
}
return help
}
func DisplayHelp(appName string, commands []Command[any], command []string) {
help := []string{`Usage: ` + appName + ` <command> <subcommand> [args] [options]`}
help = append(help, ``)
if len(command) == 0 {
help = displayAllCommandsHelp(appName, commands)
} else {
help = displaySingleCommandHelp(appName, commands, command)
}
help = append(help, ``)
help = append(help, `Global Options:`)
opts, err := helpOptions(&GlobalOptions{})
if err != nil {
fmt.Printf("Error printing global options: %v", err)
}
help = append(help, opts...)
fmt.Println(strings.Join(help, "\n"))
}
func getLongestName(commands []Command[any]) int {
longestName := 0
for _, cmd := range commands {
name := cmd.Name("")
if len(name) > longestName {
longestName = len(name)
}
if len(cmd.Commands()) > 0 {
for _, subCmd := range cmd.Commands() {
subName := name + " " + subCmd.Name("")
if len(subName) > longestName {
longestName = len(subName)
}
}
}
}
return longestName
}
type helpOption struct {
Args string
Help string
}
func newHelpOption(arg, short, help string) helpOption {
args := fmt.Sprintf(`-%s, --%s`, short, arg)
if short == "" {
args = fmt.Sprintf(`--%s`, arg)
} else if arg == "" {
args = fmt.Sprintf(`-%s`, short)
}
return helpOption{
Args: args,
Help: help,
}
}
func printableFields(fields []structs.Field) []string {
lines := []string{}
longestArg := maxLen(fields)
for _, field := range fields {
opt := newHelpOption(field.Tags["arg"], field.Tags["short"], field.Tags["help"])
padding := pad(opt.Args, longestArg)
line := ""
if len(field.Fields) == 0 {
line = fmt.Sprintf(` %s %s %s`, opt.Args, padding, opt.Help)
} else {
line = fmt.Sprintf(` [%s] %s %s`, opt.Args, padding, opt.Help)
}
lines = append(lines, line)
// top to bottom right char: └
for _, subField := range field.Fields {
opt := newHelpOption(subField.Tags["arg"], subField.Tags["short"], subField.Tags["help"])
padding := pad(opt.Args, longestArg)
line := fmt.Sprintf(` %s %s%s`, opt.Args, padding, " - "+opt.Help)
lines = append(lines, line)
}
}
return lines
}
func maxLen(fields []structs.Field) int {
longestArg := 0
for _, field := range fields {
opt := newHelpOption(field.Tags["arg"], field.Tags["short"], field.Tags["help"])
if len(opt.Args) > longestArg {
// slog.Info("longestArg", "len", longestArg, "opt.Args", opt.Args)
longestArg = len(opt.Args)
}
for _, subField := range field.Fields {
opt := newHelpOption(subField.Tags["arg"], subField.Tags["short"], subField.Tags["help"])
if len(opt.Args) > longestArg {
longestArg = len(opt.Args)
}
}
}
longestArg += 2
return longestArg
}
func helpOptions(structure any) ([]string, error) {
fields, err := structs.GetStructFields(structure, nil)
if err != nil {
return nil, fmt.Errorf("error getting global option fields: %w", err)
}
return printableFields(fields), nil
}
func indent(text string, indent int) string {
indentStr := strings.Repeat(" ", indent)
return indentStr + text
}
func pad(text string, indent int) string {
indentStr := strings.Repeat(" ", indent-len(text))
return indentStr
}