为什么这个argparse代码在Python 2和3中表现不同?
下面这段代码使用了argparse的子解析器,在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)
在Python 2.7.6中的输出是:
me@computer$ python test.py
usage: test.py [-h] {subcommand} ...
test.py: error: too few arguments
在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'
1 个回答
26
最近的 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
注意 - dest
和 required
都需要设置。dest
是用来给这个参数在错误信息中起个名字。
这个错误:
AttributeError: 'Namespace' object has no attribute 'do'
是因为 cmd
子解析器没有运行,所以没有把它的参数(无论是默认的还是自定义的)放入命名空间中。你可以通过定义另一个子解析器,来观察这个效果,并查看生成的 args
。