-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
executable file
·561 lines (496 loc) · 17.6 KB
/
Copy pathcli.py
File metadata and controls
executable file
·561 lines (496 loc) · 17.6 KB
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
#!/usr/bin/env python3
import os
import shlex
import sys
import textwrap
try:
import backtrace
except ImportError:
from . import backtrace
import pipcl
class CliError(Exception):
pass
class ArgsEq:
'''
Enhanced iterator for list of arguments, optionally splitting args at '=',
for example `--foo=bar`.
>>> args = Args(shlex.split('--foo bar --qwert=123 --bar foo'))
>>> while 1:
... try:
... arg = args.next(spliteq=1)
... if arg.text is StopIteration:
... break
... except StopIteration:
... break
... print(arg.as_str())
--foo
bar
--qwert
123
--bar
foo
>>> args = Args(shlex.split('--foo bar --qwert=123 --bar foo'))
>>> while 1:
... try:
... arg = args.next(spliteq=1)
... if arg.text is StopIteration:
... break
... except StopIteration:
... break
... print(arg.text)
--foo
bar
--qwert
123
--bar
foo
'''
def __init__(self, argv, pos=0):
self.argv = argv
self.pos = pos
self.pos_sub = 0
self.current = None
self._returned_items = list()
def next(self, spliteq=False):
'''
Returns (item, pos) for next item in self.argv. Does not raise
exception on EOF.
item:
Text of next item on command line. Will be StopIteration (the
class itself, not an instance of the class) if EOF.
pos:
(pos, pos_sub):
pos:
index into self.argv.
pos_sub:
Start of text within self.argv[pos], usually zero.
Will be non-zero and point to character after '=' if
<spliteq> is true and argv[pos] contains '=' and we are
returning the value.
'''
pos = (self.pos, self.pos_sub)
if self.pos_sub:
# Return text after `=`.
assert self.argv[self.pos][self.pos_sub-1] == '='
ret = self.argv[self.pos][self.pos_sub:]
self.pos += 1
self.pos_sub = 0
else:
if self.pos == len(self.argv):
#pipcl.log(f'Returning StopIteration.')
ret = StopIteration
else:
ret = self.argv[self.pos]
eq = -1
if spliteq and (eq := ret.find('=')) >= 0:
# '--foo=bar'. Return '--foo' and make next call return 'bar'.
self.pos_sub = eq + 1
ret = ret[:eq]
else:
self.pos += 1
self.current = ret
self._returned_items.append((ret, pos))
return ret, pos
def replace(self, pos_a, pos_b, new_args):
'''
Replaces self.argv region pos_a:pos_b with <new_args> and
resets position to <pos_a>.
pos_a and pos_b are (pos, pos_sub) tuples.
As of 2026-02-17 we require that pos_a and pos_b both have pos_sub=0 -
we cannot yet replace `foo` or `bar` in `foo=bar`.
'''
pipcl.log(f'{pos_a=} {pos_b=} {new_args=}')
a1, a2 = pos_a
b1, b2 = pos_b
assert a2 == 0
assert b2 == 0
self.argv[a1:b1] = new_args
self.pos = a1
self.pos_sub = a2
def set(self, pos, new_arg):
pos, subpos = pos
arg = self.argv[pos]
arg = arg[:subpos] + new_arg
self.argv[pos] = arg
#assert a2 == 0, f'Cannot handle {self.argv[a1]!r}, use `--foo bar`.'
#self.argv[a1] = new_arg
def set_bool(self, pos, value):
'''
Should be called with <pos> set to the arg following `--foo`.
Changes `--foo` and `foo=...` to `--foo=<value>`.
'''
pos, pos_sub = pos
if pos_sub:
# <pos> is `bar` in `--foo=bar`.
value2 = self.argv[pos][:pos_sub] + str(value)
self.argv[pos] = value2
else:
# <pos> is the `--bar` in `--foo --bar`.
self.argv[pos-1] += f'={value}'
class Completions:
'''
Keeps a record of argument completions.
'''
def __init__(self):
self.items = list()
self.pos = (-1, 0)
def add(self, suggestion, pos):
if pos < self.pos:
pipcl.log(f'Warning: was not expecting {pos=} < {self.pos=}.')
self.items.clear()
self.pos = pos
if pos > self.pos:
self.items.clear()
self.pos = pos
self.items.append(suggestion)
def __repr__(self):
ret = ''
ret = f'{self.pos=}:\n'
for suggestion in self.items:
ret += f' {suggestion!r}\n'
return ret
class Arg:
'''
A command-line argument which automatically adds items to a Completion when
compared or converted to a specific type.
'''
def __init__(self, text, pos, completions):
'''
text:
A string or the class StopIteration.
'''
assert isinstance(text, str) or text is StopIteration
self.text = text
self.pos = pos
self.completions = completions
if pos > self.completions.pos:
# This isn't strictly necessary because any examination of us that
# implies a completion will clear existing completions, but it will
# make for easier debugging of calling code to clear now.
#pipcl.log(f'{pos=} {self.completions.pos=} {len(self.completions.items)=}')
#pipcl.log(f'{self.completions.items=}')
#Actually next line prevents listing of all completions on correct eof. Although
# still fails if not args at all.
self.completions.items.clear()
def __eq__(self, rhs):
#pipcl.log(f'Arg.__eq__() {rhs=}.')
ret = (self.text == rhs)
if not ret:
# We assume that <rhs> would have been a valid value, so add to
# completions.
self.completions.add(rhs, self.pos)
return ret
def __repr__(self):
#return f'{self.text=} {self.pos=} {self.completions=}.'
return f'{self.text=} {self.pos=}'
def _as_bool(self):
'''
Returns our value converted to a bool. If no match we add to
completions and raise.
Internal because user code should call Args.get_bool().
'''
#pipcl.log(f'{self.text=}')
# Our comparisons of <self> will add completions where they fail.
if self in ('0', 'false', 'False'):
return False
elif self in ('1', 'true', 'True'):
return True
raise Exception(f'Unrecognised bool value: {self.text!r}')
def as_float(self):
'''
Returns our value converted to a float. If no match we add `float` to
completions and raise.
'''
try:
return float(self.text)
except Exception:
self.completions.add(float, self.pos)
raise
def as_int(self):
'''
Returns our value converted to an int. If no match we add `int` to
completions and raise.
'''
try:
return int(self.text)
except Exception:
self.completions.add(int, self.pos)
raise
def as_str(self):
'''
Returns our value converted to an string. Only fails if we have reached
EOF, in which case we add `str` to completions.
'''
if not isinstance(self.text, str):
self.completions.add(str, self.pos)
raise StopIteration()
return self.text
def as_text(self):
'''
Compatibility wrapper for as_str().
'''
return self.as_str()
def startswith(self, rhs):
'''
Convenience fn saves caller from having to type `.as_str()`.
'''
if self.text is StopIteration:
return False
return self.text.startswith(rhs)
class Args:
'''
Enhanced args iterator, wrapping ArgsEq, that returns an Arg instead of a
string, so we can gather completion information.
'''
def __init__(self, argv, startpos=0):
#pipcl.log(f'{startpos=}')
self.startpos = startpos
self.args_eq = ArgsEq(argv, pos=startpos)
self.completions = Completions()
self.current = None # Always the last returned Arg.
def __next__(self):
'''
Support Python iteration.
'''
return self.next()
def next(self, spliteq=False):
'''
Returns an Arg for next item in argv.
If we have reached EOF, the returned Arg has .text = StopIteration (the
class itself, not an instance of the class).
'''
text, pos = self.args_eq.next(spliteq=spliteq)
arg = Arg(text, pos, self.completions)
self.current = arg
#pipcl.log(f'Returning: {arg=}')
return arg
def error_text(self, regions=None):
'''
Returns two-line text containing the command line and caret characters
pointing to where the command line was incorrect.
fixme: move this into ArgsEq.
'''
regions = regions or list()
#pipcl.log(f'{len(self.args_eq._returned_items)=}') # pylint: disable=protected-access)
region_name, region_pos = None, 0
for (region_name, region_pos) in reversed(regions):
#pipcl.log(f'{region_name=} {region_pos=}')
if region_pos <= len(self.args_eq._returned_items): # pylint: disable=protected-access)
break
#pipcl.log(f'{region_pos=} {region_name=}')
line1 = ''
line2 = ''
for i, (text, (_pos, pos_eq)) in enumerate(self.args_eq._returned_items[region_pos-self.startpos:]): # pylint: disable=protected-access)
i += region_pos-self.startpos
#if i:
# line1 += ' '
# line2 += ' '
if text is StopIteration:
line2 += '^'
else:
if i:
if pos_eq:
line1 += '='
else:
line1 += ' '
line2 += ' '
t = shlex.quote(text)
line1 += t
if i+1 == len(self.args_eq._returned_items): # pylint: disable=protected-access)
line2 += '^' * len(t)
else:
line2 += ' ' * len(t)
return f'{line1}\n{line2}\n', region_name
@property
def argv(self):
return self.args_eq.argv
def get_bool(self, overwrite=None):
'''
Should be used to get bool args instead of Arg._as_bool(), in order
to allow `--foo` to be treated as True. If current arg is the `--foo`
in `--foo=true` we return true etc, but otherwise (`--foo`) we simply
return True.
If <overwrite> is not None, we modify argv to set the value to
<overwrite>.
'''
pos = self.pos
if self.args_eq.pos_sub:
# <self> is the `--foo` in `--foo=...`.
ret = self.next()._as_bool() # pylint: disable=protected-access)
else:
# `--foo` is treated as True.
ret = True
if overwrite is not None:
self.args_eq.set_bool(pos, overwrite)
return ret
@property
def pos(self):
return self.args_eq.pos, self.args_eq.pos_sub
@property
def suggestions(self):
return self.completions.items
def final(self, regions=None):
'''
Handles command-line parsing errors and base completion. We write out
completions if COMP_LINE is set. Otherwise we write out diagnostics if
an exception is active.
'''
COMP_LINE = os.environ.get('COMP_LINE')
#COMP_POINT = os.environ.get('COMP_POINT')
#COMP_TYPE = os.environ.get('COMP_TYPE')
_, exception, _ = sys.exc_info()
def get_suggestions():
'''
If current arg matches the start of one or more suggestions, we
return only these matching suggestions. Otherwise we return all
suggestions.
This appears to make bash do what we want without us considering
COMP_TYPE. Perhaps COMP_TYPE is only required for non-dynamic
completion?
'''
n_match = 0
#pipcl.log(f'{self.suggestions=}')
for suggestion in self.suggestions:
if isinstance(self.current.text, str) and suggestion.startswith(self.current.text):
n_match += 1
ret = ''
for suggestion in self.suggestions:
if n_match == 0 or (isinstance(self.current.text, str) and suggestion.startswith(self.current.text)):
ret += f' {suggestion}\n'
return ret.rstrip()
if exception:
# Exception has been raised.
#print(f'{self.suggestions=}')
if COMP_LINE:
text = get_suggestions()
pipcl.log(f'get_suggestions() => {text=}')
#sq = "'"
#pipcl.log(f'{sq in text=}')
sys.stdout.write(text)
sys.exit()
else:
text = ''
text += 'Bad args'
text_description, region_name = self.error_text(regions)
if region_name:
text += f' in {region_name}'
text += '\n'
text += text_description
if isinstance(exception, StopIteration):
text += f'Ran out of arguments.\n'
suggestions = get_suggestions()
if suggestions:
text += f'Expected one of:\n'
text += suggestions
else:
pass
#text += f'(No suggestions available.)\n'
raise CliError(text.strip()) from exception
else:
if COMP_LINE:
show_all = False
for suggestion in self.suggestions:
if isinstance(self.current.text, str) and suggestion.startswith(self.current.text):
break
else:
show_all = True
for suggestion in self.suggestions:
if show_all or suggestion.startswith(self.current.text):
print(suggestion)
sys.exit(0)
def check_cl(command, expected, expected_error_text=None, expected_completions=None):
print(f'Testing:')
print(f' {command=}')
print(f' {expected=}')
argv = shlex.split(command)
args = Args(argv)
args.next()
items = list()
error_text = None
try:
while 1:
try:
arg = args.next(spliteq=1)
#if arg.text is StopIteration:
# break
except StopIteration:
pipcl.log(f'Exception: StopIteration')
break
if isinstance(arg.text, str):
items.append(arg.as_str())
pipcl.log(f'{arg=}')
if arg == '-i':
item = args.next().as_int()
elif arg == '--foo':
item = args.next().as_str()
elif arg == '-f':
item = args.next().as_float()
elif arg.text is StopIteration:
pipcl.log(f'StopIteration => break. {args.completions=} {args.completions.items=}')
break
else:
assert 0, f'Unexpected {arg=}.'
items.append(item)
except Exception as ee:
e = ee
error_text = args.error_text()
print(f'Exception: {e=}')
backtrace.show()
else:
e = None
if expected_completions is not None:
print(f'{expected_completions=}')
print(f'{args.completions.items=}')
assert args.completions.items == expected_completions
print(f' {items=}')
if error_text:
print(f' error_text:\n{textwrap.indent(error_text, " ")}')
if expected_error_text:
print(f' error_text:\n{textwrap.indent(expected_error_text, " ")}')
assert items == expected, f'\n{expected=}\n {items=}'
assert error_text == expected_error_text, ('\n'
f'{expected_error_text=}\n'
f' {error_text=}\n'
)
print(f' Ok.')
def test():
check_cl(
'prog -i 23 --foo=bar -f=0.234 -i 123',
[
'-i',
23,
'--foo',
'bar',
'-f',
0.234,
'-i',
123,
],
)
check_cl(
'prog -i 23 --foo=bar -f=0.234 -i 12.3',
[
'-i',
23,
'--foo',
'bar',
'-f',
0.234,
'-i',
],
'prog -i 23 --foo=bar -f=0.234 -i 12.3\n'
' ^^^^\n'
)
check_cl(
'prog',
expected=[],
expected_completions=['-i', '--foo', '-f'],
)
check_cl(
'prog -i',
expected=['-i'],
expected_error_text='prog -i\n \n',
expected_completions=[int],
)
if __name__ == '__main__':
test()