-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommand.go
316 lines (292 loc) · 7 KB
/
command.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package mcli
import (
"reflect"
"sort"
"strings"
)
// Command holds the information of a command.
type Command struct {
Name string
AliasOf string
Description string
Hidden bool
app *App
f func()
cmdOpts []CmdOpt
parseOpts []ParseOpt
idx int
level int
isRoot bool
isGroup bool
isCompletion bool
}
// NewCommand accepts a typed function and returns a Command.
// The type parameter T must be a struct, else it panics.
// When the command is matched, mcli will parse "args" and pass it to f,
// thus user must not call "Parse" again in f, else it panics.
// If option `WithErrorHandling(flag.ContinueOnError)` is used,
// user can use Context.ArgsError to check error that occurred during parsing
// flags and arguments.
// In case you want to get the parsed flag.FlagSet, check Context.FlagSet.
func NewCommand[T any](f func(ctx *Context, args *T), opts ...ParseOpt) *Command {
if reflect.TypeOf(*(new(T))).Kind() != reflect.Struct {
panic("mcli: NewCommand args type T must be a struct")
}
cmd := &Command{
cmdOpts: []CmdOpt{EnableFlagCompletion()},
parseOpts: opts,
}
cmd.f = func() {
ctx := newContext(cmd.app)
args := new(T)
cmd.app.parseArgs(args, cmd.parseOpts...)
f(ctx, args)
}
return cmd
}
func newUntypedCommand(f func(), opts ...CmdOpt) *Command {
return &Command{
f: f,
cmdOpts: opts,
}
}
func newUntypedCtxCommand(f func(*Context), opts ...CmdOpt) *Command {
cmd := &Command{
cmdOpts: opts,
}
cmd.f = func() {
ctx := newContext(cmd.app)
f(ctx)
}
return cmd
}
func normalizeCmdName(name string) string {
name = strings.TrimSpace(name)
return strings.Join(strings.Fields(name), " ")
}
func getGroupName(name string) string {
if name == "" {
return ""
}
fields := strings.Fields(name)
return strings.Join(fields[:len(fields)-1], " ")
}
func isSubCommand(parent, sub string) bool {
return parent != sub && strings.HasPrefix(sub, parent+" ")
}
type commands []*Command
func (p *commands) add(cmd *Command) {
cmd.idx = len(*p) + 1
cmd.level = len(strings.Fields(cmd.Name))
*p = append(*p, cmd)
}
func (p commands) isValid(cmd string) bool {
for _, c := range p {
if c.Name == cmd || isSubCommand(cmd, c.Name) {
return true
}
}
return false
}
func (p commands) sort() {
sort.Slice(p, func(i, j int) bool {
return p[i].Name < p[j].Name
})
}
func (p commands) search(ctx *parsingContext, cmdArgs []string) (hasSub bool) {
flagIdx := findFlagIndex(cmdArgs)
tryName := ""
ambiguousIdx := -1
args := cmdArgs[:]
var cmd *Command
for i := 0; i < len(cmdArgs); i++ {
arg := cmdArgs[i]
if strings.HasPrefix(arg, "-") {
break
}
args = cmdArgs[i+1:]
if tryName != "" {
tryName += " "
}
tryName += arg
idx := sort.Search(len(p), func(i int) bool {
return p[i].Name >= tryName
})
if idx < len(p) &&
(p[idx].Name == tryName || isSubCommand(tryName, p[idx].Name)) {
hasSub = true
ambiguousIdx = i + 1
ctx.name = tryName
if p[idx].Name == tryName {
cmd = p[idx]
} else {
cmd = nil
}
continue
} else {
hasSub = false
}
if ambiguousIdx == -1 {
ambiguousIdx = i
}
args = cmdArgs[flagIdx:]
break
}
ctx.cmd = cmd
ctx.args = &args
if ambiguousIdx >= 0 {
ctx.ambiguousArgs = clip(cmdArgs[ambiguousIdx:flagIdx])
}
return
}
func (p commands) listSubCommandsToPrint(name string, showHidden bool) (sub commands) {
sub = p._listSubCommandsToPrint(name, showHidden, false)
if len(sub) > 10 {
sub = p._listSubCommandsToPrint(name, showHidden, true)
}
return sub
}
func (p commands) _listSubCommandsToPrint(name string, showHidden, onlyNextLevel bool) (sub commands) {
name = normalizeCmdName(name)
wantLevel := len(strings.Fields(name)) + 1
preCmd := &Command{}
for _, cmd := range p {
if cmd.Name != name && strings.HasPrefix(cmd.Name, name) {
// Don't print hidden commands.
if cmd.Hidden && !showHidden {
continue
}
if onlyNextLevel {
if cmd.level < wantLevel {
continue
}
if cmd.level > wantLevel {
_names := strings.Fields(cmd.Name)
parentCmdName := strings.Join(_names[:wantLevel], " ")
if parentCmdName == preCmd.Name {
continue
}
cmd = &Command{
Name: parentCmdName,
Description: "(Use -h to see available sub commands)",
}
}
// else cmd.Level == wantLevel
}
sub = append(sub, cmd)
preCmd = cmd
}
}
return
}
func (p commands) suggest(name string) []string {
type withDistance struct {
name string
distance int
}
const minDistance = 2
var levenshteinSuggestions []string
var prefixSuggestions []withDistance
for _, cmd := range p {
if !cmd.Hidden {
levenshteinDistance := ld(name, cmd.Name, true)
isPrefix := strings.HasPrefix(strings.ToLower(cmd.Name), strings.ToLower(name))
if levenshteinDistance <= minDistance {
levenshteinSuggestions = append(levenshteinSuggestions, cmd.Name)
} else if isPrefix {
prefixSuggestions = append(prefixSuggestions, withDistance{cmd.Name, levenshteinDistance})
}
}
}
sort.SliceStable(prefixSuggestions, func(i, j int) bool {
return prefixSuggestions[i].distance < prefixSuggestions[j].distance
})
suggestions := levenshteinSuggestions
for _, x := range prefixSuggestions {
suggestions = append(suggestions, x.name)
}
if len(suggestions) > 5 {
suggestions = suggestions[:5]
}
return suggestions
}
// ld compares two strings and returns the levenshtein distance between them.
func ld(s, t string, ignoreCase bool) int {
if ignoreCase {
s = strings.ToLower(s)
t = strings.ToLower(t)
}
d := make([][]int, len(s)+1)
for i := range d {
d[i] = make([]int, len(t)+1)
}
for i := range d {
d[i][0] = i
}
for j := range d[0] {
d[0][j] = j
}
for j := 1; j <= len(t); j++ {
for i := 1; i <= len(s); i++ {
if s[i-1] == t[j-1] {
d[i][j] = d[i-1][j-1]
} else {
min := d[i-1][j]
if d[i][j-1] < min {
min = d[i][j-1]
}
if d[i-1][j-1] < min {
min = d[i-1][j-1]
}
d[i][j] = min + 1
}
}
}
return d[len(s)][len(t)]
}
type categoryCommands struct {
category string
commands commands
}
func (p commands) groupByCategory() ([]*categoryCommands, bool) {
var result []*categoryCommands
hasCategories := false
for _, c := range p {
opts := newCmdOptions(c.cmdOpts...)
if opts.category != "" {
hasCategories = true
break
}
}
if !hasCategories {
return nil, false
}
categoryIdxMap := make(map[string]int)
var noCategoryCmds []*Command
for _, c := range p {
if c.level > 1 {
continue
}
opts := newCmdOptions(c.cmdOpts...)
if opts.category == "" {
noCategoryCmds = append(noCategoryCmds, c)
continue
}
if idx, ok := categoryIdxMap[opts.category]; !ok {
categoryIdxMap[opts.category] = len(result)
result = append(result, &categoryCommands{
category: opts.category,
commands: commands{c},
})
} else {
result[idx].commands = append(result[idx].commands, c)
}
}
if len(noCategoryCmds) > 0 {
result = append(result, &categoryCommands{
category: "Other Commands",
commands: noCategoryCmds,
})
}
return result, true
}