-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargvsparser.py
74 lines (52 loc) · 1.52 KB
/
argvsparser.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import sys
from datetime import datetime
import functools
argv = sys.argv[1:]
def get_arg_value(type):
argv_len = len(argv)
index = argv.index(type) if type in argv else -1
if index < 0 or index >= argv_len - 1:
return None
else:
value = argv[index+1]
return value
def get_lable_value(type):
return type in argv
def parse_date(value, default):
if not value:
return default
else:
return datetime.strptime(value, '%Y-%m-%d')
def parse_int(value, default):
if not value:
return default
else:
return int(value)
def parse_str(value, default):
if not value:
return default
else:
return value
def parse_boolean(value, default):
if not value:
return default
else:
return bool(value)
def parse_func(type, default, islabel=False):
if islabel:
exists = get_lable_value(type)
if exists:
return functools.partial(parse_boolean, value='True',
default=default)
else:
return functools.partial(parse_boolean, value=default,
default=default)
valuestr = get_arg_value(type)
getterfunc = argv_getter.get(type)
return functools.partial(getterfunc, value=valuestr, default=default)
argv_getter = {'-d': parse_date,
'-c': parse_int,
'-s': parse_int,
'-f': parse_str,
'-f': parse_str,
'-e': parse_boolean}