/
complete_getopt.go
369 lines (350 loc) · 9.8 KB
/
complete_getopt.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package edit
import (
"errors"
"fmt"
"strings"
"unicode/utf8"
"github.com/elves/elvish/pkg/eval"
"github.com/elves/elvish/pkg/eval/vals"
"github.com/elves/elvish/pkg/getopt"
"github.com/elves/elvish/pkg/parse"
"github.com/xiaq/persistent/hashmap"
)
//elvdoc:fn complete-getopt
//
// ```elvish
// edit:complete-getopt $args $opt-specs $arg-handlers
// ```
// Produces completions according to a specification of accepted command-line
// options (both short and long options are handled), positional handler
// functions for each command position, and the current arguments in the command
// line. The arguments are as follows:
//
// * `$args` is an array containing the current arguments in the command line
// (without the command itself). These are the arguments as passed to the
// [Argument Completer](#argument-completer) function.
//
// * `$opt-specs` is an array of maps, each one containing the definition of
// one possible command-line option. Matching options will be provided as
// completions when the last element of `$args` starts with a dash, but not
// otherwise. Each map can contain the following keys (at least one of `short`
// or `long` needs to be specified):
//
// - `short` contains the one-letter short option, if any, without the dash.
//
// - `long` contains the long option name, if any, without the initial two
// dashes.
//
// - `arg-optional`, if set to `$true`, specifies that the option receives an
// optional argument.
//
// - `arg-required`, if set to `$true`, specifies that the option receives a
// mandatory argument. Only one of `arg-optional` or `arg-required` can be
// set to `$true`.
//
// - `desc` can be set to a human-readable description of the option which
// will be displayed in the completion menu.
//
// - `completer` can be set to a function to generate possible completions for
// the option argument. The function receives as argument the element at
// that position and return zero or more candidates.
//
// * `$arg-handlers` is an array of functions, each one returning the possible
// completions for that position in the arguments. Each function receives
// as argument the last element of `$args`, and should return zero or more
// possible values for the completions at that point. The returned values can
// be plain strings or the output of `edit:complex-candidate`. If the last
// element of the list is the string `...`, then the last handler is reused
// for all following arguments.
//
// Example:
//
// ```elvish-transcript
// ~> fn complete [@args]{
// opt-specs = [ [&short=a &long=all &desc="Show all"]
// [&short=n &desc="Set name" &arg-required=$true
// &completer= [_]{ put name1 name2 }] ]
// arg-handlers = [ [_]{ put first1 first2 }
// [_]{ put second1 second2 } ... ]
// edit:complete-getopt $args $opt-specs $arg-handlers
// }
// ~> complete ''
// ▶ first1
// ▶ first2
// ~> complete '-'
// ▶ (edit:complex-candidate -a &display='-a (Show all)')
// ▶ (edit:complex-candidate --all &display='--all (Show all)')
// ▶ (edit:complex-candidate -n &display='-n (Set name)')
// ~> complete -n ''
// ▶ name1
// ▶ name2
// ~> complete -a ''
// ▶ first1
// ▶ first2
// ~> complete arg1 ''
// ▶ second1
// ▶ second2
// ~> complete arg1 arg2 ''
// ▶ second1
// ▶ second2
// ```
func completeGetopt(fm *eval.Frame, vArgs, vOpts, vArgHandlers interface{}) error {
args, err := parseGetoptArgs(vArgs)
if err != nil {
return err
}
opts, err := parseGetoptOptSpecs(vOpts)
if err != nil {
return err
}
argHandlers, variadic, err := parseGetoptArgHandlers(vArgHandlers)
if err != nil {
return err
}
// TODO(xiaq): Make the Config field configurable
g := getopt.Getopt{Options: opts.opts, Config: getopt.GNUGetoptLong}
_, parsedArgs, ctx := g.Parse(args)
out := fm.OutputChan()
putShortOpt := func(opt *getopt.Option) {
c := complexItem{Stem: "-" + string(opt.Short)}
if d, ok := opts.desc[opt]; ok {
if e, ok := opts.argDesc[opt]; ok {
c.Display = c.Stem + " " + e + " (" + d + ")"
} else {
c.Display = c.Stem + " (" + d + ")"
}
}
out <- c
}
putLongOpt := func(opt *getopt.Option) {
c := complexItem{Stem: "--" + opt.Long}
if d, ok := opts.desc[opt]; ok {
if e, ok := opts.argDesc[opt]; ok {
c.Display = c.Stem + " " + e + " (" + d + ")"
} else {
c.Display = c.Stem + " (" + d + ")"
}
}
out <- c
}
call := func(fn eval.Callable, args ...interface{}) {
fn.Call(fm, args, eval.NoOpts)
}
switch ctx.Type {
case getopt.NewOptionOrArgument, getopt.Argument:
// Find argument handler.
var argHandler eval.Callable
if len(parsedArgs) < len(argHandlers) {
argHandler = argHandlers[len(parsedArgs)]
} else if variadic {
argHandler = argHandlers[len(argHandlers)-1]
}
if argHandler != nil {
call(argHandler, ctx.Text)
} else {
// TODO(xiaq): Notify that there is no suitable argument completer.
}
case getopt.NewOption:
for _, opt := range opts.opts {
if opt.Short != 0 {
putShortOpt(opt)
}
if opt.Long != "" {
putLongOpt(opt)
}
}
case getopt.NewLongOption:
for _, opt := range opts.opts {
if opt.Long != "" {
putLongOpt(opt)
}
}
case getopt.LongOption:
for _, opt := range opts.opts {
if strings.HasPrefix(opt.Long, ctx.Text) {
putLongOpt(opt)
}
}
case getopt.ChainShortOption:
for _, opt := range opts.opts {
if opt.Short != 0 {
// TODO(xiaq): Loses chained options.
putShortOpt(opt)
}
}
case getopt.OptionArgument:
gen := opts.argGenerator[ctx.Option.Option]
if gen != nil {
call(gen, ctx.Option.Argument)
}
}
return nil
}
// TODO(xiaq): Simplify most of the parsing below with reflection.
func parseGetoptArgs(v interface{}) ([]string, error) {
var args []string
var err error
errIterate := vals.Iterate(v, func(v interface{}) bool {
arg, ok := v.(string)
if !ok {
err = fmt.Errorf("arg should be string, got %s", vals.Kind(v))
return false
}
args = append(args, arg)
return true
})
if errIterate != nil {
err = errIterate
}
return args, err
}
type parsedOptSpecs struct {
opts []*getopt.Option
desc map[*getopt.Option]string
argDesc map[*getopt.Option]string
argGenerator map[*getopt.Option]eval.Callable
}
func parseGetoptOptSpecs(v interface{}) (parsedOptSpecs, error) {
result := parsedOptSpecs{
nil, map[*getopt.Option]string{},
map[*getopt.Option]string{}, map[*getopt.Option]eval.Callable{}}
var err error
errIterate := vals.Iterate(v, func(v interface{}) bool {
m, ok := v.(hashmap.Map)
if !ok {
err = fmt.Errorf("opt should be map, got %s", vals.Kind(v))
return false
}
opt := &getopt.Option{}
getStringField := func(k string) (string, bool, error) {
v, ok := m.Index(k)
if !ok {
return "", false, nil
}
if vs, ok := v.(string); ok {
return vs, true, nil
}
return "", false,
fmt.Errorf("%s should be string, got %s", k, vals.Kind(v))
}
getCallableField := func(k string) (eval.Callable, bool, error) {
v, ok := m.Index(k)
if !ok {
return nil, false, nil
}
if vb, ok := v.(eval.Callable); ok {
return vb, true, nil
}
return nil, false,
fmt.Errorf("%s should be fn, got %s", k, vals.Kind(v))
}
getBoolField := func(k string) (bool, bool, error) {
v, ok := m.Index(k)
if !ok {
return false, false, nil
}
if vb, ok := v.(bool); ok {
return vb, true, nil
}
return false, false,
fmt.Errorf("%s should be bool, got %s", k, vals.Kind(v))
}
if s, ok, errGet := getStringField("short"); ok {
r, size := utf8.DecodeRuneInString(s)
if r == utf8.RuneError || size != len(s) {
err = fmt.Errorf(
"short option should be exactly one rune, got %v",
parse.Quote(s))
return false
}
opt.Short = r
} else if errGet != nil {
err = errGet
return false
}
if s, ok, errGet := getStringField("long"); ok {
opt.Long = s
} else if errGet != nil {
err = errGet
return false
}
if opt.Short == 0 && opt.Long == "" {
err = errors.New(
"opt should have at least one of short and long forms")
return false
}
argRequired, _, errGet := getBoolField("arg-required")
if errGet != nil {
err = errGet
return false
}
argOptional, _, errGet := getBoolField("arg-optional")
if errGet != nil {
err = errGet
return false
}
switch {
case argRequired && argOptional:
err = errors.New(
"opt cannot have both arg-required and arg-optional")
return false
case argRequired:
opt.HasArg = getopt.RequiredArgument
case argOptional:
opt.HasArg = getopt.OptionalArgument
}
if s, ok, errGet := getStringField("desc"); ok {
result.desc[opt] = s
} else if errGet != nil {
err = errGet
return false
}
if s, ok, errGet := getStringField("arg-desc"); ok {
result.argDesc[opt] = s
} else if errGet != nil {
err = errGet
return false
}
if f, ok, errGet := getCallableField("completer"); ok {
result.argGenerator[opt] = f
} else if errGet != nil {
err = errGet
return false
}
result.opts = append(result.opts, opt)
return true
})
if errIterate != nil {
err = errIterate
}
return result, err
}
func parseGetoptArgHandlers(v interface{}) ([]eval.Callable, bool, error) {
var argHandlers []eval.Callable
var variadic bool
var err error
errIterate := vals.Iterate(v, func(v interface{}) bool {
sv, ok := v.(string)
if ok {
if sv == "..." {
variadic = true
return true
}
err = fmt.Errorf(
"string except for ... not allowed as argument handler, got %s",
parse.Quote(sv))
return false
}
argHandler, ok := v.(eval.Callable)
if !ok {
err = fmt.Errorf(
"argument handler should be fn, got %s", vals.Kind(v))
}
argHandlers = append(argHandlers, argHandler)
return true
})
if errIterate != nil {
err = errIterate
}
return argHandlers, variadic, err
}