Python的argparse库使用详解


Posted in Python onOctober 09, 2018

argparse是python标准库里面用来处理命令行参数的库

命令行参数分为位置参数和选项参数:

位置参数就是程序根据该参数出现的位置来确定的

如:[root@openstack_1 /]# ls root/    #其中root/是位置参数

选项参数是应用程序已经提前定义好的参数,不是随意指定的

如:[root@openstack_1 /]# ls -l    # -l 就是ls命令里的一个选项参数、

基本使用

import argparse

# 创建解析器
parser = argparse.ArgumentParser(description = 'This is a test')
parser.parse_args()

可以在shell中测试:

$ python test.py --help
...

添加参数

import argparse

parser = argparse.ArgumentParser(description = 'This is a test')
parser.add_argument("-p","--port",help='increase output port') # 定义了可选参数-p和--port,赋值后,其值保存在args.port中(其值都是保存在最后一个定义的参数中)
args = parser.parse_args()
print(args.echo)

argparse.ArgumentParser()方法参数须知:一般我们只选择用description

          prog=None     - 程序名

          description=None,    - help时显示的开始文字

           epilog=None,     - help时显示的结尾文字

          parents=[],        -若与其他参数的一些内容一样,可以继承

          formatter_class=argparse.HelpFormatter,     - 自定义帮助信息的格式

          prefix_chars='-',    - 命令的前缀,默认是‘-'

          fromfile_prefix_chars=None,     - 命令行参数从文件中读取

          argument_default=None,    - 设置一个全局的选项缺省值,一般每个选项单独设置

          conflict_handler='error',     - 定义两个add_argument中添加的选项名字发生冲突时怎么处理,默认处理是抛出异常

          add_help=True    - 是否增加-h/--help选项,默认是True)

add_argument()方法参数须知:

    name or flags...    - 必选,指定参数的形式,一般写两个,一个短参数,一个长参数

使用时候:

$ python test.py -p 50
或
$ python test.py --port 50

指定类型

我们也可以在添加参数的时候指定其类型。

import argparse

parser = argparse.ArgumentParser(description = 'This is a test')
parser.add_argument("square",help="display a given number",type=int) # 指定给square的参数为int类型

可选参数

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-v", help="increase output verbosity")
args = parser.parse_args()
if args.v:
  print("v turned on")

使用:

$ python test.py -v any

文档

更多应用请百度或者查文档:https://docs.python.org/3/library/argparse.html#module-argparse

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

Python 相关文章推荐
Python中使用 Selenium 实现网页截图实例
Jul 18 Python
python实现端口转发器的方法
Mar 13 Python
python 将print输出的内容保存到txt文件中
Jul 17 Python
python SQLAlchemy的Mapping与Declarative详解
Jul 04 Python
python lambda表达式(匿名函数)写法解析
Sep 16 Python
Django框架模板用法入门教程
Nov 04 Python
Python发送手机动态验证码代码实例
Feb 28 Python
在python中list作函数形参,防止被实参修改的实现方法
Jun 05 Python
Python限制内存和CPU使用量的方法(Unix系统适用)
Aug 04 Python
python 实现百度网盘非会员上传超过500个文件的方法
Jan 07 Python
VSCODE配置Markdown及Markdown基础语法详解
Jan 19 Python
Python if else条件语句形式详解
Mar 24 Python
详解Python3中的迭代器和生成器及其区别
Oct 09 #Python
不知道这5种下划线的含义,你就不算真的会Python!
Oct 09 #Python
详解利用django中间件django.middleware.csrf.CsrfViewMiddleware防止csrf攻击
Oct 09 #Python
详解如何将python3.6软件的py文件打包成exe程序
Oct 09 #Python
让代码变得更易维护的7个Python库
Oct 09 #Python
windows下cx_Freeze生成Python可执行程序的详细步骤
Oct 09 #Python
Python打包方法Pyinstaller的使用
Oct 09 #Python
You might like
两个强悍的php 图像处理类1
2009/06/15 PHP
phpcms模块开发之swfupload的使用介绍
2013/04/28 PHP
PHP将XML转数组过程详解
2013/11/13 PHP
php使用百度天气接口示例
2014/04/22 PHP
JavaScript中的原型和继承详解(图文)
2014/07/18 Javascript
nodejs 整合kindEditor实现图片上传
2015/02/03 NodeJs
javascript数组去重的六种方法汇总
2015/08/16 Javascript
Atitit.js的键盘按键事件捆绑and事件调度
2016/04/01 Javascript
微信小程序tabBar用法实例详解
2017/12/04 Javascript
Nodejs 和 Electron ubuntu下快速安装过程
2018/05/04 NodeJs
微信小程序 MinUI组件库系列之badge徽章组件示例
2018/08/20 Javascript
微信小程序onLaunch异步,首页onLoad先执行?
2018/09/20 Javascript
node静态服务器实现静态读取文件或文件夹
2019/12/03 Javascript
JavaScript对象字面量和构造函数原理与用法详解
2020/04/18 Javascript
探索node之事件循环的实现
2020/10/30 Javascript
[00:20]DOTA2荣耀之路7:-ah fu-抢盾
2018/05/31 DOTA
[48:54]VGJ.T vs infamous Supermajor小组赛D组败者组第一轮 BO3 第二场 6.3
2018/06/04 DOTA
python操作mongodb根据_id查询数据的实现方法
2015/05/20 Python
python实现查找两个字符串中相同字符并输出的方法
2015/07/11 Python
Python定时任务sched模块用法示例
2018/07/16 Python
基于随机梯度下降的矩阵分解推荐算法(python)
2018/08/31 Python
基于Django静态资源部署404的解决方法
2019/07/28 Python
Python selenium文件上传下载功能代码实例
2020/04/13 Python
详解python metaclass(元类)
2020/08/13 Python
CSS3教程(7):CSS3嵌入字体
2009/04/02 HTML / CSS
canvas 如何绘制线段的实现方法
2018/07/12 HTML / CSS
您在慕尼黑的跑步商店:Lauf-bar
2019/10/11 全球购物
俄罗斯购买自行车网站:Vamvelosiped
2021/01/29 全球购物
介绍Ibatis的核心类
2013/11/18 面试题
俄语专业毕业生推荐信
2013/10/28 职场文书
市场开发与营销专业求职信
2013/12/31 职场文书
预备党员党课思想汇报
2014/01/13 职场文书
高二学生评语大全
2014/04/25 职场文书
助理政工师申报材料
2014/06/03 职场文书
2015纪念九一八事变84周年演讲稿
2015/03/19 职场文书
护理自荐信
2019/05/14 职场文书