Python TypeError: __init__() 在 argparse 中有一个意外的关键字参数“type”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33574270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:34:44  来源:igfitidea点击:

TypeError: __init__() got an unexpected keyword argument 'type' in argparse

pythonparsingargparse

提问by Big_VAA

Hey so I'm using argparse to try and generate a quarterly report. This is what the code looks like:

嘿,我正在使用 argparse 来尝试生成季度报告。这是代码的样子:

parser  = argparse.ArgumentParser()

parser.add_argument('-q', "--quarter",  action='store_true', type=int, help="Enter a Quarter number: 1,2,3, or 4 ")
parser.add_argument('-y', "--year", action='store_true',type=str,help="Enter a year in the format YYYY ")
args = parser.parse_args()

the error I receive is:

我收到的错误是:

TypeError: init() got an unexpected keyword argument 'type'

TypeError: init() 得到了一个意外的关键字参数 'type'

as far as I can tell from the argparse documentation type is one of the parameters of the add_argument function. I tried removing this and updating the code to :

据我所知,argparse 文档类型是 add_argument 函数的参数之一。我尝试删除它并将代码更新为:

parser  = argparse.ArgumentParser()

parser.add_argument('-q', "--quarter",  action='store_true', help="Enter a Quarter number: 1,2,3, or 4 ")
parser.add_argument('-y', "--year", action='store_true',help="Enter a year in the format YYYY ")
args = parser.parse_args()

I then tried to run it with: python scriptname.py -q 1 -y 2015and it is giving me the following error:

然后我尝试使用它运行它:python scriptname.py -q 1 -y 2015它给了我以下错误:

error:unrecognized arguments: 1 2015

错误:无法识别的参数:1 2015

I don't know why that is either. Can anyone please shed some light on this.

我也不知道为什么会这样。任何人都可以对此有所了解。

回答by Sebastian

What action="store_true"means is that if the argument is given on the command line then a Truevalue should be stored in the parser. What you actually want is to store the given year (as a string) and quarter (as an int).

action="store_true"意味着如果参数是在命令行上给出的,那么True应该在解析器中存储一个值。您真正想要的是存储给定的年份(作为字符串)和季度(作为整数)。

parser  = argparse.ArgumentParser()

parser.add_argument('-q', "--quarter", type=int, help="Enter a Quarter number: 1,2,3, or 4 ")
parser.add_argument('-y', "--year", type=str, help="Enter a year in the format YYYY ")
args = parser.parse_args()

When you specify action='store_trueargparse is internally instantiating a _StoreActioninstance whose constructor does not accept a typeparameter (since it will always be a boolean (True/False)). You cannot supply action="store_true"and 'type' at the same time.

当您指定action='store_trueargparse 是在内部实例化_StoreAction其构造函数不接受type参数的实例时(因为它始终是布尔值(真/假))。您不能同时提供action="store_true"和“输入”。

回答by hpaulj

The argparsedocumentation is not as detailed as it could be (but still has more information than many users can absorb).

argparse文档不作详细介绍,因为它可以(但仍比许多用户可以吸收更多的信息)。

In particular, the actual information that an argument needs varies with action.

特别是,一个参数需要的实际信息随 变化action

parser.add_argument('-q', "--quarter",  action='store_true', type=int, help="Enter a Quarter number: 1,2,3, or 4 ")

A store_trueaction does not take any parameters (i.e. nargs=0). It's default value is False, and if used the attribute is set to True.

一个store_true动作不带任何参数(即 nargs=0)。它的默认值为False,如果使用,则该属性设置为True

If you want the user to give one of those four numbers I'd suggest using

如果您希望用户提供这四个数字之一,我建议您使用

parser.add_argument('-q', '--quarter', type=int, choices=[1,2,3,4], help="...")

https://docs.python.org/3/library/argparse.html#choiceshas a similar example.

https://docs.python.org/3/library/argparse.html#choices有一个类似的例子。

The examples in https://docs.python.org/3/library/argparse.html#actiongive a pretty good idea of what parameters each action class takes.

https://docs.python.org/3/library/argparse.html#action 中的示例很好地说明了每个操作类采用的参数。

There is a Python bug/issue discussing improving either the documentation, or the error message when unnecessary parameters are given in the function. As it stands, it's the Python subclass definition that is issuing the error message.

当函数中给出不必要的参数时,有一个 Python 错误/问题讨论改进文档或错误消息。就目前而言,发出错误消息的是 Python 子类定义。