来自metavar` [[USER @] HOST:] FILE`的Python argparse AssertionError

时间:2016-12-07 02:08:45

标签: python argparse

Argparse 1.1或1.4与AssertionError失败 - 读取metavar值的奇怪正则表达式似乎爆炸了argparse - 可能与Python argparse AssertionError when number of arguments exceeds threshold有关?

是否有其他方法可以创建或使用metavar [[USER@]HOST:]FILE

测试设置:

$ virtualenv-3.5 --always-copy test2
$ ./test2/bin/pip install argparse

Python代码test.py:

#!/Users/[username]/Development/test2/bin/python3.5

import os
import sys
print('sys.prefix', sys.prefix)
sys.path.insert(
    0, os.path.join(sys.prefix, 'lib/python3.5/site-packages'))

import argparse
print('argparse', argparse.__version__)

parser = argparse.ArgumentParser()
parser.add_argument(
    'files',
    metavar='[[USER@]HOST:]FILE',
    nargs=argparse.PARSER,
    )
parser.add_argument('-a', '-A', metavar='PTRN', dest='patterns', default=[])
parser.add_argument('-b', '-B', metavar='PTRN', dest='patterns', default=[])
parser.add_argument('-c', '-C', metavar='PTRN', dest='patterns', default=[])
parser.add_argument('-d', '-D', metavar='PTRN', dest='patterns', default=[])
parser.add_argument('-e', '-E', metavar='PTRN', dest='patterns', default=[])
parser.add_argument('-f', '-F', metavar='PTRN', dest='patterns', default=[])
print(parser.parse_args())

Shell输出:

$ ./test.py
sys.prefix /Users/[username]/Development/test2/bin/..
argparse 1.4.0
Traceback (most recent call last):
  File "./test.py", line 24, in <module>
    print(parser.parse_args())
  File "/Users/[username]/Development/test2/bin/../lib/python3.5/site-packages/argparse.py", line 1725, in parse_args
    args, argv = self.parse_known_args(args, namespace)
  File "/Users/[username]/Development/test2/bin/../lib/python3.5/site-packages/argparse.py", line 1754, in parse_known_args
    namespace, args = self._parse_known_args(args, namespace)
  File "/Users/[username]/Development/test2/bin/../lib/python3.5/site-packages/argparse.py", line 1971, in _parse_known_args
    self.error(_('too few arguments'))
  File "/Users/[username]/Development/test2/bin/../lib/python3.5/site-packages/argparse.py", line 2391, in error
    self.print_usage(_sys.stderr)
  File "/Users/[username]/Development/test2/bin/../lib/python3.5/site-packages/argparse.py", line 2353, in print_usage
    self._print_message(self.format_usage(), file)
  File "/Users/[username]/Development/test2/bin/../lib/python3.5/site-packages/argparse.py", line 2309, in format_usage
    return formatter.format_help()
  File "/Users/[username]/Development/test2/bin/../lib/python3.5/site-packages/argparse.py", line 306, in format_help
    help = self._root_section.format_help()
  File "/Users/[username]/Development/test2/bin/../lib/python3.5/site-packages/argparse.py", line 236, in format_help
    func(*args)
  File "/Users/[username]/Development/test2/bin/../lib/python3.5/site-packages/argparse.py", line 358, in _format_usage
    assert ' '.join(pos_parts) == pos_usage
AssertionError

2 个答案:

答案 0 :(得分:1)

解决方法是创建自定义帮助格式化程序并使用已清理的值进行后处理。一种方法可以这样:

class MyHelpFormatter(argparse.HelpFormatter):

    def _format_usage(self, usage, actions, groups, prefix):
        result = super(MyHelpFormatter, self)._format_usage(
            usage, actions, groups, prefix)
        return result.format(user_host_file='[[USER@]HOST]:FILE')

然后使用自定义formattter构建解析器

parser = argparse.ArgumentParser(formatter_class=MyHelpFormatter)
parser.add_argument(
    'files',
    metavar='{user_host_file}',
    nargs=argparse.PARSER,
    )

将其余部分插入所提供的代码中,产生类似的内容

$ python demo.py 
('argparse', '1.1')
usage: demo.py [-h] [-a PTRN] [-b PTRN] [-c PTRN] [-d PTRN] [-e PTRN]
               [-f PTRN]
               [[USER@]HOST]:FILE ...
demo.py: error: too few arguments

当然,确切的策略可以像在格式化程序中使用str.replace方法一样简单,也可以比str.format方法更复杂。

答案 1 :(得分:1)

是的,argparse usage格式化程序中存在已知错误。 metavar中的字符像括号一样产生这个断言错误。我可以指出错误/问题或解释问题。但最简单的解决方案是将METAVAR更改为更简单的方法。将额外信息放在帮助热线中。另一个简单的选择是提供自定义usage参数。

http://bugs.python.org/issue11874

HelpFormatter进行子类化并替换一个或两个方法也是很好的argparse练习。但这需要深入研究代码,并了解需要替换的内容。另一个答案是一个良好的开端。问题补丁中提出的更改更复杂,因为它试图成为通用目的(包括处理互斥组和可能跨越多行的用法)。