-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcli.py
166 lines (144 loc) · 5.73 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
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
"""
A number of argparse helpers, generally using custom argument typing for
additional input value transformations.
"""
import argparse
import re
import sys
from typing import List, Tuple, Union
def add_standard_params(parser: argparse.ArgumentParser) -> None:
"""
Adds -v, -p and -t.
"""
parser.add_argument("-v", "--verbose",
dest="verbose",
help="Print debug info to stdout",
action='store_true')
parser.add_argument("-p", "--post-to-github",
help="Post all issues to GitHub instead of stdout",
action="store_true")
parser.add_argument("-t", "--time-log-dir",
help="If set, time logs are written to that directory",
action="store", default=None)
def add_event_param(
parser: argparse.ArgumentParser,
dest: str = "event",
required: bool = False,
template: str = "{}y visit",
accepted_regex: str = r'^\d+$',
keep_nonmatch: bool = False) -> None:
"""
Handles and transforms arbitrarily entered events.
"""
nargs = '+' if required else '*'
def __event_handler(arg: str) -> Union[str, None]:
if re.match(accepted_regex, arg):
return template.format(arg)
elif keep_nonmatch:
return arg
else:
return None
parser.add_argument("-e", "--event",
help=("Event name matching regex {}, before "
"transformation to {}").format(accepted_regex,
template),
nargs=nargs,
required=required,
type=__event_handler)
def add_subject_param(parser: argparse.ArgumentParser,
dest="subject",
required: bool = False,
choices: List[str] = None) -> None:
"""
Add parameter for subject ID entry.
"""
nargs = '+' if required else '*'
parser.add_argument("-s", "--study-id", "--subject",
help="Subject IDs that the script should affect",
nargs=nargs,
dest=dest,
choices=choices,
# metavar="STUDY_ID"
required=required,
action="store")
def add_site_param(parser: argparse.ArgumentParser,
dest="site",
required: bool = False,
choices: List[str] = None) -> None:
"""
Add parameter for site ID entry.
"""
nargs = '+' if required else '*'
parser.add_argument("--site",
help="Site IDs that the script should affect (ucsd, sri, etc.)",
nargs=nargs,
dest=dest,
choices=choices,
# metavar="STUDY_ID"
required=required,
action="store")
def add_form_param(parser: argparse.ArgumentParser,
dest='forms',
required: bool = False,
eligible_forms: List[Tuple[str]] = [],
raise_missing: bool = True,
short_switch="-i") -> None:
"""
Add the forms parameter, with checking for form aliases.
eligible_forms: a list of tuples, where the first element in the tuple is
the canonical name of the form, and the remaining are allowable names
for the form
short_switch: the "short" option - `-i` by default (short for instruments),
but some CLIs use -f to mean --forms instead of --force.
"""
# _eligible_forms = []
# for x in eligible_forms:
# if isinstance(x, string_types):
# _eligible_forms.append((x))
# else:
# _eligible_forms.append(x)
def __form_handler(arg):
if len(eligible_forms) == 0:
return arg
forms_found = [x for x in eligible_forms if arg in x]
if len(forms_found) == 1:
return forms_found[0][0]
elif not raise_missing:
return None
else:
if len(forms_found) == 0:
raise ValueError("{} not found in eligible forms {}"
.format(arg, eligible_forms))
elif len(forms_found) > 1:
raise ValueError("Ambiguous {}, found in following forms: {}"
.format(arg, forms_found))
nargs = '+' if required else '*'
parser.add_argument(short_switch, '--forms', '--form', '--instrument',
help="Forms that the script should affect",
dest=dest,
nargs=nargs,
required=required,
type=__form_handler)
def transform_commas_in_args(arg_list: List[str] = sys.argv[1:]) -> List[str]:
"""
Essentially, turn comma splits into spaces.
Limited, in that it splices *any* commas, which might be actually valid
filename parts etc. Ideally, this would be part of an argparse.Action.
"""
_arg_list = []
for x in arg_list:
if "," in x:
_arg_list.extend(x.split(","))
else:
_arg_list.append(x)
return _arg_list
# if __name__ == '__main__':
# args = transform_commas_in_args(sys.argv[1:])
# parser = argparse.ArgumentParser()
# add_event_param(parser, template="visit_{}")
# add_subject_param(parser)
# add_form_param(parser, eligible_forms=[
# ('limesurvey_ssaga_youth', 'lssaga_youth', 'lssaga1_youth')
# ])
# add_standard_params(parser)
# print(parser.parse_args(args))