-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmicrocli.py
More file actions
62 lines (49 loc) · 1.83 KB
/
microcli.py
File metadata and controls
62 lines (49 loc) · 1.83 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
""" This module provides a decorator that translates type hints and a formatted docstring into
a command line interface. The intent is to provide an easier-to-use and more intuitive
(though less powerful) alternative to the argparse module.
"""
import sys
import micro_inspect as mui
import cliparse as cli_p
from functools import wraps
def microcli(app):
""" This decorator implements the microcli interface -- wrapping an application in
with the microcli decorator will parse arguments from the command line
and attempt to match them to arguments in the wrapped function using its
argspec.
"""
@wraps(app)
def wrapper(argv):
""" sys.argv should be passed to this wrapper directly. It will handle removing
the script name from sys.argv.
"""
argl = argv[1:]
if "-h" in argl or "--help" in argl:
print(app.__doc__)
quit(0)
debug = "-d" in argl or "--debug" in argl
argspec = mui.getfullargspec(app)
pos, opt = cli_p.parseargs(argl)
if debug:
print("Initial parse successful.")
print(f"Parsed positional arguments: {pos}")
print(f"Parsed options: {opt}")
print(f"argspec: {argspec}")
m_pos, m_opt = cli_p.matchargs(pos, opt, argspec)
if m_pos is None:
if debug:
print(f"Match error: {m_opt}")
exit(1)
if debug:
print("Secondary parse successful.")
print(f"Matched positional arguments: {m_pos}")
print(f"Matched options: {m_opt}")
try:
return app(*m_pos, **m_opt)
except Exception as e:
if debug:
raise e
else:
print("Fatal exception.")
exit(1)
return wrapper