简体   繁体   English

为什么这个argparse代码在Python 2和3之间表现不同?

[英]Why does this argparse code behave differently between Python 2 and 3?

The following code, using argparse's subparsers, fails on Python 3 but runs as expected in Python 2. After comparing the docs, I still can't tell why. 下面的代码,使用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)

The output from Python 2.7.6 is: Python 2.7.6的输出是:

me@computer$ python test.py 
usage: test.py [-h] {subcommand} ...
test.py: error: too few arguments

In Python 3.3.5, I get: 在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'

the latest argparse release changed how it tested for required arguments, and subparsers fell through the cracks. 最新的argparse版本改变了它测试所需参数的方式,而subparsers则陷入了困境。 They are no longer 'required'. 它们不再是“必需的”。 http://bugs.python.org/issue9253#msg186387 http://bugs.python.org/issue9253#msg186387

When you get test.py: error: too few arguments , it's objecting that you did not give it a 'subcommand' argument. 当你得到test.py: error: too few arguments ,它反对你没有给它一个'子命令'参数。 In 3.3.5 it makes it past that step, and returns args . 在3.3.5中,它使它超过该步骤,并返回args

With this change, 3.3.5 should behave the same as earlier versions: 通过此更改,3.3.5应该与早期版本的行为相同:

ap = ArgumentParser()
sp = ap.add_subparsers(dest='parser')  # dest needed for error message
sp.required = True   # force 'required' testing

Note - both dest and required need to be set. 注意 - 需要设置destrequired dest is needed to give this argument a name in the error message. dest需要在错误消息中为此参数指定名称。


This error: 这个错误:

AttributeError: 'Namespace' object has no attribute 'do'

was produced because the cmd subparser did not run, and did not put its arguments (default or not) into the namespace. 是因为cmd subparser没有运行,并且没有将其参数(默认或不是)放入命名空间。 You can see that effect by defining another subparser, and looking at the resulting args . 您可以通过定义另一个subparser并查看生成的args来查看该效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 2-为什么“ with”在嵌入式C代码中表现不同? - python 2 - why does 'with' behave differently in embedded c code? 为什么 Python 3 for loop output 和行为不同? - Why does Python 3 for loop output and behave differently? 为什么Python3.1中的代码行为与Python2.6中的行为不同? - Why does this code behave differently in Python3.1 than in Python2.6? 为什么PyQt4在Jupyter和IPython Notebook之间的行为有所不同? - Why does PyQt4 behave differently between Jupyter and IPython notebook? 为什么这个TensorFlow代码在测试用例中表现不同? - Why does this TensorFlow code behave differently when inside a test case? 为什么Python列表的行为会根据声明的不同而有所不同? - Why does Python list of lists behave differently depending on their declaration? 为什么Python **运算符在数组和标量上的行为不同 - Why does the Python ** operator behave differently on arrays and scalars 为什么 Python 对相同的函数(例如“sum”或“and”)表现不同? - Why does Python behave differently for the same function such as 'sum' or 'and'? Python:为什么带有附加参数的方法的行为有所不同? - Python: Why Does a Method Behave Differently with an Added Parameter? 为什么RestrictedPython在与Python 3.6一起使用时表现不同? - Why does RestrictedPython behave differently when used with Python 3.6?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM