iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Pythonargparse中的action=store_true用法小结
  • 510
分享到

Pythonargparse中的action=store_true用法小结

Pythonargparse中的action=store_truePythonaction=store_true 2023-02-10 12:02:49 510人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

目录python argparse中的action=store_true用法前言示例官方文档多了解一点儿自定义小结思考补充:Python库Argparse中的可选参数设置 actio

Python argparse中的action=store_true用法

前言

Python的命令行参数解析模块学习

示例

参数解析模块支持action参数,这个参数可以设置为’store_true’、‘store_false’、'store_const’等。
例如下面这行代码,表示如果命令行参数中出现了"–PARAM_NAME",就把PARAM_NAME设置为True,否则为False。

parser.add_argument("--PARAM_NAME", action="store_true", help="HELP_INFO")

官方文档

‘store_true’ and ‘store_false’ - These are special cases of ‘store_const’ used for storing the values True and False respectively. In addition, they create default values of False and True respectively. For example:

‘store_true’ 和 ‘store_false’ -这两个是’store_const’的特例,分别用来设置True和False。另外,他们还会创建默认值。

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo', action='store_true')
>>> parser.add_argument('--bar', action='store_false')
>>> parser.add_argument('--baz', action='store_false')
>>> parser.parse_args('--foo --bar'.split())
Namespace(foo=True, bar=False, baz=True)

多了解一点儿

自定义

你可以通过给定一个Action的子类或其他实现了相同接口的对象,来指定一个任意的action
BooleanOptionalAction就是一个可以使用的action,它增加了布尔action特性,支持--foo--no-foo的形式。

>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo', action=argparse.BooleanOptionalAction)
>>> parser.parse_args(['--no-foo'])
Namespace(foo=False)

小结

'--foo', action='store_true',可以很方便地实现布尔类型的参数。

思考

python3 开始,很多内置模块都转向了面向对象范式。
对于早期开始使用Python的用户来说,见到的代码更多是面向过程或者是函数风格的,例如,从Google开源的一些项目可以看到很多Python 2.x的代码风格。

补充:python库Argparse中的可选参数设置 action=‘store_true‘ 的用法

store_true 是指带触发action时为真,不触发则为假。

通俗讲是指运行程序是否带参数,看例子就明白了。

一、没有default

import argparse
 
parser = argparse.ArgumentParser(description='test.py')
parser.add_argument('--cuda', type=bool, default=True,  help='use cuda')
parser.add_argument('--cpu',action='store_true',help='use cpu')
args = parser.parse_args()
 
print("cuda: ",args.cuda)
print("cpu: ",args.cpu)

如果运行命令为:python test.py

则输出为:

cuda:  True
cpu:  False

如果运行命令为:python test.py --cpu

则输出为:

cuda:  True
cpu:  True

二、有default

当然 ‘store_true’ 也可以设置 default ,虽然这样看起来很奇怪,也不好用。如:

parser.add_argument('--cpu',default=True,action='store_true',help='use cpu')
print("cpu: ",args.cpu)

default=True时运行程序时加不加 “ --cpu ” 输出都是 cpu: True

但default=False就不一样了:

parser.add_argument('--cpu',default=False,action='store_true',help='use cpu')
print("cpu: ",args.cpu)

若运行命令是 python test.py,则输出 cpu: False

若运行命令是 python test.py --cpu,则输出 cpu: True

到此这篇关于Python argparse中的action=store_true用法小结的文章就介绍到这了,更多相关Python argparse中的action=store_true内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Pythonargparse中的action=store_true用法小结

本文链接: https://www.lsjlt.com/news/195504.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作