Wed*_*ood 13 python python-2.x argparse python-3.x
下面的代码,使用argparse的subparsers,在Python 3上失败,但在Python 2中按预期运行.在比较文档后,我仍然无法说明原因.
#!/usr/bin/env python
from __future__ import print_function
from argparse import ArgumentParser
def action(args):
print(args)
if __name__ == '__main__':
std = ArgumentParser(add_help=False)
std.add_argument('standard')
ap = ArgumentParser()
sp = ap.add_subparsers()
cmd = sp.add_parser('subcommand', parents=[std], description='Do subcommand')
cmd.add_argument('arg')
cmd.set_defaults(do=action)
args = ap.parse_args()
args.do(args)
Run Code Online (Sandbox Code Playgroud)
Python 2.7.6的输出是:
me@computer$ python test.py
usage: test.py [-h] {subcommand} ...
test.py: error: too few arguments
Run Code Online (Sandbox Code Playgroud)
在Python 3.3.5中,我得到:
me@computer$ python3 test.py
Traceback (most recent call last):
File "test.py", line 21, in <module>
args.do(args)
AttributeError: 'Namespace' object has no attribute 'do'
Run Code Online (Sandbox Code Playgroud)
hpa*_*ulj 19
最新argparse版本改变了它测试所需参数的方式,并且子分析师陷入了困境.它们不再是"必需的". http://bugs.python.org/issue9253#msg186387
当你得到时test.py: error: too few arguments,它反对你没有给它一个'子命令'参数.在3.3.5中它使它超过该步骤并返回args.
通过此更改,3.3.5应该与早期版本的行为相同:
ap = ArgumentParser()
sp = ap.add_subparsers(dest='parser') # dest needed for error message
sp.required = True # force 'required' testing
Run Code Online (Sandbox Code Playgroud)
注-既dest和required需要设置. dest需要在错误消息中为此参数指定名称.
这个错误:
AttributeError: 'Namespace' object has no attribute 'do'
Run Code Online (Sandbox Code Playgroud)
是因为cmdsubparser没有运行,并且没有将其参数(默认或不是)放入命名空间.您可以通过定义另一个subparser并查看结果来查看该效果args.