我在使用 python 2.6.1 argparse 0.8 时遇到了这个问题,我需要添加%d
帮助描述,例如
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--range', metavar='range', type=str,
help='generates a list of host from given range e.g. "host%d.example.com 1 224"')
args = parser.parse_args()
它抛出错误
$ ./args.py -h
...
python2.6/site-packages/argparse.py", line 458, in _expand_help
return action.help % params
TypeError: %d format: a number is required, not dict
我能理解,所以我试图逃避,%
例如
parser.add_argument('--range', metavar='range', type=str,
help='generates a list of host from given range e.g. "host%%d.example.com 1 224"')
但我仍然得到另一个错误
$ ./args.py -h
...
python2.6/site-packages/argparse.py", line 252, in format_help
help = self._root_section.format_help() % dict(prog=self._prog)
TypeError: %d format: a number is required, not dict
所以我不确定如何正确转义,以便在帮助输出%
中看到%d