我在 python 中玩 argparse。我有如下内容:
group_gen.add_argument('--switch', nargs=2, metavar=('PARAM1', 'PARAM2'))
然后我删除了 PARAM2 所以我有:
group_gen.add_argument('--switch', nargs=2, metavar=('PARAM1', 'PARAM2'))
然后,每当 argparse 必须打印帮助时,我都会通过堆栈跟踪得到以下异常
Traceback (most recent call last):
File "./script.py", line 20, in <module>
args = parser.parse_args()
File "/usr/lib/python2.7/argparse.py", line 1688, in parse_args
args, argv = self.parse_known_args(args, namespace)
File "/usr/lib/python2.7/argparse.py", line 1727, in parse_known_args
self.error(str(err))
File "/usr/lib/python2.7/argparse.py", line 2346, in error
self.print_usage(_sys.stderr)
File "/usr/lib/python2.7/argparse.py", line 2308, in print_usage
self._print_message(self.format_usage(), file)
File "/usr/lib/python2.7/argparse.py", line 2264, in format_usage
return formatter.format_help()
File "/usr/lib/python2.7/argparse.py", line 279, in format_help
help = self._root_section.format_help()
File "/usr/lib/python2.7/argparse.py", line 209, in format_help
func(*args)
File "/usr/lib/python2.7/argparse.py", line 317, in _format_usage
action_usage = format(optionals + positionals, groups)
File "/usr/lib/python2.7/argparse.py", line 449, in _format_actions_usage
args_string = self._format_args(action, default)
File "/usr/lib/python2.7/argparse.py", line 587, in _format_args
result = ' '.join(formats) % get_metavar(action.nargs)
TypeError: not enough arguments for format string
为什么我会收到此错误?
我无法弄清楚发生了什么,然后我突然想到,使用命令格式字符串