Python argparse 不带参数的命令行标志
如何向命令行参数添加可选标志?
例如。所以我可以写
python myprog.py
或者
python myprog.py -w
我尝试过
parser.add_argument('-w')
但我只是收到一条错误消息,说
Usage [-w W]
error: argument -w: expected one argument
我认为这意味着它需要 -w 选项的参数值。接受旗帜的方式是什么?
我发现 http://docs.python.org/library/argparse.html 在这个问题上相当不透明。
How do I add an optional flag to my command line 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?
I'm finding http://docs.python.org/library/argparse.html rather opaque on this question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
正如您所看到的,参数
w
需要命令行上-w
之后的值。如果您只是想通过设置变量True
或False
来切换开关,请查看 此处(特别是 store_true 和 store_false),其中
action='store_true'
暗示default=False
。相反,您可以使用
action='store_false'
,这意味着default=True
。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 variableTrue
orFalse
, have a look here (specifically store_true and store_false)where
action='store_true'
impliesdefault=False
.Conversely, you could have
action='store_false'
, which impliesdefault=True
.添加一个快速片段以使其准备好执行:
来源:myparser.py
用法:
Adding a quick snippet to have it ready to execute:
Source: myparser.py
Usage:
你的剧本是对的。但默认情况下是 None 类型。因此,除了 None 之外的任何其他值都被视为 true 被分配给 args.argument_name 变量。
我建议您添加一个 action="store_true"。这将形成 True/False 类型的标志。如果使用则为 True,否则为 False。
用法
解析后,用 args.f 检查时返回 true,
Your script is right. But by default is of None type. So it considers true of any other value other than None is assigned to args.argument_name variable.
I would suggest you to add a action="store_true". This would make the True/False type of flag. If used its True else False.
usage
After parsing when checked with args.f it returns true,
如果您正在寻找二进制标志,则 argparse 操作
store_true
或store_false
正是提供了这一点。 @Jdog 接受的答案很好地解释了这种方法。官方文档也相当清楚。我只会用一行完成该示例,因此为了非常清楚
store_true
/store_false
的作用:一种稍微更强大的方法是使用 <代码>计数操作。在运行命令时设置详细级别时,您通常已经使用了这种类型的标志。
例如,
ssh
的详细模式标志-v
是一个计数器:因此,如果您运行
ssh
,它是非详细的,ssh -v
稍微详细,ssh -vvv
是极其冗长。使用Python中的argparse,这样的计数器标志可以定义如下:
如果你想将它用作布尔值(
True
/False
)标志,那么您需要将 args.verbose 转换为布尔值。您可以自己明确地执行此操作,也可以依赖条件语句,例如if args.verbose: ...
。这是一个完整的工作示例,说明如何使用计数器标志:
使用脚本
test.py
:您将获得以下输出:
If you are looking for a binary flag, then the argparse actions
store_true
orstore_false
provide exactly this. This approach is well explained in the accepted answer by @Jdog.The official docs are also fairly clear. I would only complete the example with one line, so to make it very clear how the
store_true
/store_false
act:A slightly more powerful approach is to use the
count
action. You typically have used this type of flag already when setting the verbosity level when running a command.For example
ssh
's verbose mode flag-v
is a counter:So if you run
ssh
it's non verbose,ssh -v
is slightly verbose andssh -vvv
is maximally verbose.With argparse in python such a counter flag can be defined as follows:
If you want to use it as a boolena (
True
/False
) flag, then you need to castargs.verbose
into a boolean. You can either do this explicitly yourself, or rely a conditional statement likeif args.verbose: ...
.Here is a full working example to illustrate how you can use the counter flag:
With the script
test.py
:You get the following outputs:
这是一种快速的方法,除了
sys
之外不需要任何东西。尽管功能有限:flag = "--flag" in sys.argv[1:]
[1:]
是指完整文件名是--flag
Here's a quick way to do it, won't require anything besides
sys
.. though functionality is limited:flag = "--flag" in sys.argv[1:]
[1:]
is in case if the full file name is--flag