python argparser的具体使用


Posted in Python onNovember 10, 2019

一.正常运行:

咱们随便写个文件:

# test.py
import argparse

ap = argparse.ArgumentParser()
ap.add_argument('-i', '--image', help='传入图片文件')
args = vars(ap.parse_args())
print(args)

咱们运行一下:

python test.py --image './test.png'
python test.py -i './test.png'

没问题吧:

{'image':'./test.png'}

{'i':'./test.png'}

二.咱们改一下程序:

ap.add_argument('--image', help='传入图片文件')

第一个'-i'参数去掉, 一望而知,只能:

python test.py --image './test.png'

输出:

{'image':'./test.png'}

三.咱们再改一下程序:

ap.add_argument('-i', help='传入图片文件')

第一个'--image'参数去掉, 一望而知,只能:

python test.py -i './test.png'

输出:

{'i':'./test.png'}

也就是说,两个参数任选其一

四.传参数时改一下参数

在只传入一个'--image'的情况下:

ap.add_argument('--image', help='传入图片文件')

我们可以用'--image'、'--imag'、'--ima'、'--im'和'--i'

python test.py --image './test.png'
python test.py --imag './test.png'
python test.py --ima './test.png'
python test.py --im './test.png'
python test.py --i './test.png'

输出都是:

{'image':'./test.png'}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python实现系统状态监测和故障转移实例方法
Nov 18 Python
Python命令行参数解析模块optparse使用实例
Apr 13 Python
简析Python的闭包和装饰器
Feb 26 Python
Python设计模式中单例模式的实现及在Tornado中的应用
Mar 02 Python
Django自定义分页效果
Jun 27 Python
解决Django模板无法使用perms变量问题的方法
Sep 10 Python
django中的HTML控件及参数传递方法
Mar 20 Python
Python数据持久化shelve模块用法分析
Jun 29 Python
python3 BeautifulSoup模块使用字典的方法抓取a标签内的数据示例
Nov 28 Python
windows、linux下打包Python3程序详细方法
Mar 17 Python
Anaconda+spyder+pycharm的pytorch配置详解(GPU)
Oct 18 Python
Sentry错误日志监控使用方法解析
Nov 12 Python
python滑块验证码的破解实现
Nov 10 #Python
Python 中使用 PyMySQL模块操作数据库的方法
Nov 10 #Python
分享PyCharm的几个使用技巧
Nov 10 #Python
Python单元测试与测试用例简析
Nov 09 #Python
python自动化测试之异常及日志操作实例分析
Nov 09 #Python
Python多线程模块Threading用法示例小结
Nov 09 #Python
Python for循环及基础用法详解
Nov 08 #Python
You might like
PHP在引号前面添加反斜杠(PHP去除反斜杠)
2013/09/28 PHP
PHP防止表单重复提交的几种常用方法汇总
2014/08/19 PHP
PHP解析RSS的方法
2015/03/05 PHP
php从文件夹随机读取文件的方法
2015/06/01 PHP
浅谈PHP安全防护之Web攻击
2017/01/03 PHP
PHP使Laravel为JSON REST API返回自定义错误的问题
2018/10/16 PHP
关于laravel 数据库迁移中integer类型是无法指定长度的问题
2019/10/09 PHP
人人网javascript面试题 可以提前实现下
2012/01/05 Javascript
JQuery UI的拖拽功能实现方法小结
2012/03/14 Javascript
JS+css 图片自动缩放自适应大小
2013/08/08 Javascript
JS 数字转换研究总结
2013/12/26 Javascript
jquery 无限级下拉菜单的简单实现代码
2014/02/21 Javascript
javascript控制图片播放的实现代码
2020/07/29 Javascript
jQuery实现点击按钮文字变成input框点击保存变成文字
2016/05/09 Javascript
深入理解JavaScript中的call、apply、bind方法的区别
2016/05/30 Javascript
JS实现六边形3D拖拽翻转效果的方法
2016/09/11 Javascript
Django使用多数据库的方法
2017/09/06 Javascript
JS实现计算小于非负数n的素数的数量算法示例
2019/02/26 Javascript
微信小程序 冒泡事件原理解析
2019/09/27 Javascript
Python使用pymysql小技巧
2017/06/04 Python
python pandas.DataFrame选取、修改数据最好用.loc,.iloc,.ix实现
2018/06/11 Python
Django使用rest_framework写出API
2020/05/21 Python
Python参数传递对象的引用原理解析
2020/05/22 Python
一文带你掌握Pyecharts地理数据可视化的方法
2021/02/06 Python
HTML5、Select下拉框右边加图标的实现代码(增进用户体验)
2017/10/16 HTML / CSS
宝拉珍选澳大利亚官方购物网站:Paula’s Choice澳大利亚
2016/09/13 全球购物
德国在线订购鲜花:Fleurop
2018/08/25 全球购物
Antonioli美国在线商店:时尚前卫奢华
2019/07/29 全球购物
个人贷款承诺书
2014/03/28 职场文书
4s店市场专员岗位职责
2014/04/09 职场文书
违反单位工作制度检讨书
2014/10/25 职场文书
意向协议书
2015/01/27 职场文书
2015年电教工作总结
2015/05/26 职场文书
新员工试用期工作总结2015
2015/05/28 职场文书
2015秋季运动会通讯稿
2015/07/18 职场文书
SQLServer权限之只开启创建表权限
2022/04/12 SQL Server