来自字典的argparse add_argument

时间:2018-03-10 23:05:37

标签: python python-3.x argparse

我有以下字典我试图用来在argparse中生成参数:

inititateCmdMap = {
    '-l': {
        'help': 'list of transactions for type',
        'choices': ['all', 'utxq', 'mtxq']},
    '-u': {
        'help', 'url of rest server'},
    '-k': {
        'help': 'something helpful'},
    '-x': {
        'help': 'expression'}}

但我不确定如何将其作为以下例外传递:

[parser.add_argument(v[0], v[1]) for v in inititateCmdMap.items()]

 in add_argument
    kwargs = self._get_optional_kwargs(*args, **kwargs)
  File "/usr/local/Cellar/python/3.6.4_3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/argparse.py", line 1446, in _get_optional_kwargs
    if not option_string[0] in self.prefix_chars:
KeyError: 0

这甚至可能吗?

1 个答案:

答案 0 :(得分:2)

你一直在传递dict本身,而不是解压缩其内容。

试试这样:

for arg, kwargs in inititateCmdMap.items():
    parser.add_argument(arg, **kwargs):