-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmdargs.py
More file actions
48 lines (44 loc) · 1.93 KB
/
cmdargs.py
File metadata and controls
48 lines (44 loc) · 1.93 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
import argparse, sys, types
class tmp(types.ModuleType):
def __getitem__(self, name): return self.vars_args[name]
def __contains__(self, name):return name in self.vars_args
def subparse(self, var, *options):
parser = argparse.ArgumentParser(formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, width=9999, max_help_position=9999))
for arg in options:
if isinstance(arg, tuple):
arg, kwarg = arg
else:
kwarg = {}
if 'default' in kwarg:
if 'help' not in kwarg:
kwarg['help'] = ''
else:
kwarg['help'] += ' '
if type(kwarg['default']) == str:
kwarg['help'] += '(default: "%(default)s")'
else:
kwarg['help'] += '(default: %(default)s)'
if arg[0] in parser.prefix_chars:
kwarg['dest'] = arg.lstrip(parser.prefix_chars) # stop argparse confusingly converting - to _
parser.add_argument(arg, **kwarg)
return vars(parser.parse_args(var))
def parse(self, *options):
options = list(options)
for arg in options:
if isinstance(arg, str):
assert arg != '--debug'
elif arg[0] == '--debug':
assert 'type' in arg[1]
assert arg[1]['type'] == int
break
else:
options.append(('--debug',{'type':int,'default':0}))
options.append(('--quiet',{'action':'store_true'}))
self.vars_args = self.subparse(None, *options)
if (self.vars_args['debug'] > 0) or self.vars_args['quiet']:
import clog
clog.stdout.setLevel(
clog.logging.ERROR if self.vars_args['quiet']
else (clog.logging.NOTSET if self.vars_args['debug'] > 1 else clog.logging.DEBUG)
)
sys.modules[__name__] = tmp('')