没有参数的Python argparse命令行标记。

时间:2022-06-06 03:55:36

How do I add an optional flag to my command line args?

如何向命令行args添加可选标志?

eg. so I can write

如。所以我可以写

python myprog.py 

or

python myprog.py -w

I tried

我试着

parser.add_argument('-w')

But I just get an error message saying

但是我得到了一个错误消息

Usage [-w W]
error: argument -w: expected one argument

which I take it means that it wants an argument value for the -w option. What's the way of just accepting a flag?

我认为这意味着它想要-w选项的参数值。接受国旗的方式是什么?

I'm finding http://docs.python.org/library/argparse.html rather opaque on this question.

我发现http://docs.python.org/library/argparse.html在这个问题上很不透明。

4 个解决方案

#1


308  

As you have it, the argument w is expecting a value after -w on the command line. If you are just looking to flip a switch by setting a variable True or False, have a look at http://docs.python.org/dev/library/argparse.html#action (specifically store_true and store_false)

如您所见,参数w期望在命令行上-w之后有一个值。如果您只是想通过设置一个变量True或False来切换开关,请查看http://docs.python.org/dev/library/argparse.html#操作(具体是store_true和store_false)

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

Edit: As Sven points out, a default value in this case is superfluous.

编辑:正如Sven指出的,在这种情况下,默认值是多余的。

#2


18  

Adding a quick snippet to have it ready to execute:

添加一个快速代码片段,使其准备执行:

Source: myparser.py

来源:myparser.py

import argparse
parser = argparse.ArgumentParser(description="Flip a switch by setting a flag")
parser.add_argument('-w', action='store_true')

args = parser.parse_args()
print args.w

Usage:

用法:

python myparser.py -w
>> True

#3


-2  

while adding the argument to parser, action = "store_true" flag sets the default value for the argument to True. So, parser.add_argument('-w', action = "store_true") should solve the error.

在向解析器添加参数时,action = "store_true"标志将参数的默认值设置为True。因此,解析器。add_argument('-w', action = "store_true")应该可以解决这个错误。

#4


-3  

Here's a quick way to do it, won't require anything besides sys.. though functionality is limited:

这是一种简便易行的方法,除了系统之外什么都不需要。虽然功能是有限的:

flag = "--flag" in sys.argv[1:]

sys.argv中的flag = "- flag" [1:]

[1:] is in case if the full file name is --flag

[1:]是指如果完整文件名是——标志

#1


308  

As you have it, the argument w is expecting a value after -w on the command line. If you are just looking to flip a switch by setting a variable True or False, have a look at http://docs.python.org/dev/library/argparse.html#action (specifically store_true and store_false)

如您所见,参数w期望在命令行上-w之后有一个值。如果您只是想通过设置一个变量True或False来切换开关,请查看http://docs.python.org/dev/library/argparse.html#操作(具体是store_true和store_false)

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

Edit: As Sven points out, a default value in this case is superfluous.

编辑:正如Sven指出的,在这种情况下,默认值是多余的。

#2


18  

Adding a quick snippet to have it ready to execute:

添加一个快速代码片段,使其准备执行:

Source: myparser.py

来源:myparser.py

import argparse
parser = argparse.ArgumentParser(description="Flip a switch by setting a flag")
parser.add_argument('-w', action='store_true')

args = parser.parse_args()
print args.w

Usage:

用法:

python myparser.py -w
>> True

#3


-2  

while adding the argument to parser, action = "store_true" flag sets the default value for the argument to True. So, parser.add_argument('-w', action = "store_true") should solve the error.

在向解析器添加参数时,action = "store_true"标志将参数的默认值设置为True。因此,解析器。add_argument('-w', action = "store_true")应该可以解决这个错误。

#4


-3  

Here's a quick way to do it, won't require anything besides sys.. though functionality is limited:

这是一种简便易行的方法,除了系统之外什么都不需要。虽然功能是有限的:

flag = "--flag" in sys.argv[1:]

sys.argv中的flag = "- flag" [1:]

[1:] is in case if the full file name is --flag

[1:]是指如果完整文件名是——标志