[英]How to use function arguments from list for function call
I want to do something like this我想做这样的事情
allowed_args = ['--email', '--password', '--verbose']
if '--email' in allowed_args:
parser.add_argument('--email')
if '--password' in allowed_args:
parser.add_argument('--password', help='Better dont use your password on command line')
if '--verbose' in allowed_args:
parser.add_argument('--verbose', action='store_true')
Because I will need to define, which arguments are allowed before parsing the arguments using argparse
.因为我需要定义,在使用argparse
解析 arguments 之前允许哪些 arguments 。
This works like expeted, but how can I avoid, using these repetitive if clauses (this is just an example, I would actually have a lot more...)?这就像 expeted 一样,但是我怎样才能避免使用这些重复的 if 子句(这只是一个例子,我实际上还有更多......)? I would like somehow to predefine the arguments and then choose which of them to use.我想以某种方式预定义 arguments,然后选择使用哪一个。
This is a little tricky:这有点棘手:
allowed_args = {
'--email':{},
'--password':{'help':'Better dont ...',}, # you can add more items
'--verbose':{'action':'store_true',}
}
then:然后:
for k,v in allowed_args.items():
parser.add_argument(k, **v)
You can add as many as arguments to your allowed_args
values( the dict inside
).您可以将多达 arguments 添加到您的allowed_args
值( the dict inside
)。
You could write that like this:你可以这样写:
for name, args, kwargs in \
[('--email', [], {}),
('--password', [], {help: 'Better dont use your password on command line'}),
('--verbose', [], {action: 'store_true'})]:
parser.add_argument(name, *args, **kwargs)
The mapping could also be stored in a dictionary, but by using a list of tuples, this ensures that options are added in the desired order even if used on older versions of Python.映射也可以存储在字典中,但通过使用元组列表,即使在旧版本的 Python 上使用,也可以确保按所需顺序添加选项。
You can define a dictionary of args and kwargs to pass in the event of a match:您可以定义一个 args 和 kwargs 字典以在匹配事件中传递:
parameters = {
"--email": {
"args": [],
"kwargs": {},
},
"--password": {
"args": [],
"kwargs": {
"help": "Better dont use your password on command line"
},
},
"--verbose": {
"args": [],
"kwargs": {
"action": "store_true"
}
}
}
Then, you can iterate over your input array and add the parameters in a loop:然后,您可以遍历输入数组并在循环中添加参数:
allowed_args = ['--email', '--password', '--verbose']
for arg in allowed_rags:
arg_parameters = parameters[arg]
parser.add_argument(arg, *arg_parameters['args'], **arg_parameters['kwargs'])
You could also check whether the given parameter exists and raise an exception:您还可以检查给定参数是否存在并引发异常:
allowed_args = ['--email', '--password', '--verbose']
for arg in allowed_rags:
if arg not in parameters:
raise NotImplementedError(arg)
arg_parameters = parameters[arg]
Furthermore, if you use a set as input, you could avoid some edge cases:此外,如果您使用集合作为输入,您可以避免一些极端情况:
allowed_args = {'--email', '--password', '--verbose'}
for arg in allowed_args & parameters:
arg_parameters = parameters[arg]
parser.add_argument(arg, *arg_parameters['args'], **arg_parameters['kwargs'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.