-
Notifications
You must be signed in to change notification settings - Fork 8
/
StoreKeyValuePairAction.py
52 lines (41 loc) · 1.55 KB
/
StoreKeyValuePairAction.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
"""
@author: Xiao Liang Yu
"""
import argparse
class StoreKeyValuePairAction(argparse.Action):
def __init__(self,
option_strings,
dest,
nargs=None,
const=None,
default=None,
type=None,
choices=None,
required=False,
help=None,
metavar=None):
if type not in (None, str):
raise ValueError('type for StoreKeyValuePairAction must be str')
super(StoreKeyValuePairAction, self).__init__(
option_strings=option_strings,
dest=dest,
nargs=nargs,
const=const,
default=default,
type=type,
choices=choices,
required=required,
help=help,
metavar=metavar)
def __call__(self, parser, namespace, values, option_string=None):
dest = getattr(namespace, self.dest)
if dest is None:
setattr(namespace, self.dest, {})
dest = getattr(namespace, self.dest)
for value in values:
# Treat `=` after the first occurence part of the value
pair = value.split('=', maxsplit=1)
# if len(pair) != 2:
# raise ValueError('value {} for argument {} is incorrect'.format(value, self.dest))
k, v = pair
dest[k] = v