-
Notifications
You must be signed in to change notification settings - Fork 48
/
cli.py
52 lines (40 loc) · 1.5 KB
/
cli.py
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
"""
The cli (command line interface) module provides the `cli.run` function that
can be used set up properties, initiate an output environment, and execute a
python function based arguments specified on the command line.
"""
def run(f, *, argv=None):
'''Command line interface for a single function.'''
import treelog, bottombar
from . import _util as util, matrix, parallel, cache, warnings
decorators = (
util.trap_sigint(),
bottombar.add(util.timer(), label='runtime', right=True, refresh=1), \
bottombar.add(util.memory(), label='memory', right=True, refresh=1), \
util.in_context(cache.caching),
util.in_context(parallel.maxprocs),
util.in_context(matrix.backend),
util.in_context(util.set_stdoutlog),
util.in_context(util.add_htmllog),
util.in_context(util.log_traceback),
util.in_context(util.post_mortem),
warnings.via(treelog.warning),
util.log_arguments,
util.timeit(),
)
for decorator in reversed(decorators):
f = decorator(f)
return util.cli(f, argv=argv)
def choose(*functions, argv=None):
'''Command line interface for multiple functions.'''
import sys
progname, *args = argv or sys.argv
fmap = {f.__name__: f for f in functions}
try:
choice = args.pop(0)
f = fmap[choice]
except:
sys.exit(f'USAGE: {progname} {"|".join(fmap)} [...]')
else:
return run(f, argv=(f'{progname} {choice}', *args))
# vim:sw=4:sts=4:et