Python argparse中的action=store_true用法小结

作者:coder1479 时间:2023-07-31 22:35:02 

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

来源:https://blog.csdn.net/m0_48742971/article/details/124839044

标签:Python,argparse,action=store,true
0
投稿

猜你喜欢

  • python中必会的四大高级数据类型(字符,元组,列表,字典)

    2023-01-10 00:54:20
  • Python2.x与3​​.x版本有哪些区别

    2023-11-01 08:34:31
  • MySQL中select语句介绍及使用示例

    2024-01-23 06:47:07
  • MySQL为数据表建立索引的原则详解

    2024-01-13 14:46:18
  • python计算导数并绘图的实例

    2023-01-19 21:16:51
  • SQL SERVER 与ACCESS、EXCEL的数据转换

    2024-01-18 04:24:40
  • 关于mysql 的时间类型选择

    2024-01-17 11:44:20
  • 注册表单之死

    2008-08-07 13:02:00
  • tensorboard显示空白的解决

    2023-05-28 15:05:05
  • face_recognition库在python的安装

    2021-06-16 02:29:27
  • 原生JS实现几个常用DOM操作API实例

    2024-04-25 13:09:27
  • MySQL redo死锁问题排查及解决过程分析

    2024-01-17 08:17:02
  • 使用Python和Prometheus跟踪天气的使用方法

    2021-05-15 14:31:05
  • 怎样删除Git中缓存的用户名和密码

    2023-08-12 23:56:55
  • php实现微信支付之企业付款

    2023-11-24 02:06:43
  • Python批量修改文件名实例操作分享

    2022-04-11 06:14:47
  • Go语言中的Array、Slice、Map和Set使用详解

    2023-06-24 07:29:03
  • Python自动打印被调用函数变量名及对应值 

    2022-08-05 09:50:38
  • python中必要的名词解释

    2023-10-17 23:30:32
  • 关于设计规范

    2008-06-02 13:10:00
  • asp之家 网络编程 m.aspxhome.com