如果未指定,则argparse存储为false

问题:如果未指定,则argparse存储为false

parser.add_argument('-auto', action='store_true')

如果-auto未指定,如何存储假?我可以淡淡地记得这种方式,如果未指定,它将存储None

parser.add_argument('-auto', action='store_true')

How can I store false if -auto is unspecified? I can faintly remember that this way, it stores None if unspecified


回答 0

store_true选项自动创建默认值False

同样,当不存在命令行参数时,store_false默认为True

此行为的来源简洁明了:http : //hg.python.org/cpython/file/2.7/Lib/argparse.py#l861

argparse文档尚不清楚,所以我现在将对其进行更新:http : //hg.python.org/cpython/rev/49677cc6d83a

The store_true option automatically creates a default value of False.

Likewise, store_false will default to True when the command-line argument is not present.

The source for this behavior is succinct and clear: http://hg.python.org/cpython/file/2.7/Lib/argparse.py#l861

The argparse docs aren’t clear on the subject, so I’ll update them now: http://hg.python.org/cpython/rev/49677cc6d83a


回答 1

import argparse
parser=argparse.ArgumentParser()
parser.add_argument('-auto', action='store_true', )
args=parser.parse_args()
print(args)

跑步

% test.py

Yield

Namespace(auto=False)

因此False,默认情况下它似乎正在存储。

With

import argparse
parser=argparse.ArgumentParser()
parser.add_argument('-auto', action='store_true', )
args=parser.parse_args()
print(args)

running

% test.py

yields

Namespace(auto=False)

So it appears to be storing False by default.


回答 2

Raymond Hettinger已经回答了OP的问题。

但是,我的小组使用“ store_false”遇到了可读性问题。特别是当新成员加入我们的小组时。这是因为最直观的思考方式是,当用户指定参数时,与该参数相对应的值为True或1。

例如,如果代码是-

parser.add_argument('--stop_logging', action='store_false')

当stop_logging中的值为true时,代码阅读器可能希望关闭日志记录语句。但是,如下所示的代码将导致所需行为的反面

if not stop_logging:
    #log

另一方面,如果将接口定义为以下内容,则“ if语句”有效并且更直观地阅读-

parser.add_argument('--stop_logging', action='store_true')
if not stop_logging:
    #log

Raymond Hettinger answers OP’s question already.

However, my group has experienced readability issues using “store_false”. Especially when new members join our group. This is because it is most intuitive way to think is that when a user specifies an argument, the value corresponding to that argument will be True or 1.

For example, if the code is –

parser.add_argument('--stop_logging', action='store_false')

The code reader may likely expect the logging statement to be off when the value in stop_logging is true. But code such as the following will lead to the opposite of the desired behavior –

if not stop_logging:
    #log

On the other hand, if the interface is defined as the following, then the “if-statement” works and is more intuitive to read –

parser.add_argument('--stop_logging', action='store_true')
if not stop_logging:
    #log

回答 3

store_false实际上将默认为0默认值(您可以测试验证)。要更改默认设置,只需添加default=True到声明中即可。

因此,在这种情况下: parser.add_argument('-auto', action='store_true', default=True)

store_false will actually default to 0 by default (you can test to verify). To change what it defaults to, just add default=True to your declaration.

So in this case: parser.add_argument('-auto', action='store_true', default=True)