python之命令行解析工具argparse
以前写python的时候都会自己在文件开头写一个usgae函数,用来加上各种注释,给用这个脚本的人提供帮助文档。
今天才知道原来python已经有一个自带的命令行解析工具argparse,用了一下,效果还不错。
argparse的官方文档请看 https://docs.python.org/2/howto/argparse.html#id1
from argparse import ArgumentParser p = ArgumentParser(usage='it is usage tip', description='this is a test')
p.add_argument('--one', default=1, type=int, help='the first argument')
args = p.parse_args()
print args
运行这个脚本test.py.
python test.py -h
输出:
usage: it is usage tip
this is a test
optional arguments:
-h, --help show this help message and exit
--one ONE the first argument
python test.py --one 8
输出:
Namespace(one=8)
可以看到argparse自动给程序加上了-h的选项,最后p.parse_args()得到的是你运行脚本时输入的参数。
如果我们需要用这些参数要怎么办呢,可以通过vars()转换把Namespace转换成dict。
from argparse import ArgumentParser p = ArgumentParser(usage='it is usage tip', description='this is a test')
p.add_argument('--one', default=1, type=int, help='the first argument')
p.add_argument('--two', default='hello', type=str, help='the second argument') args = p.parse_args()
print args
mydict = vars(args)
print mydict
print mydict['two']
运行test.py.
python test.py --one 8 --two good
输出:
Namespace(one=8, two='good')
{'two': 'good', 'one': 8}
good
python之命令行解析工具argparse的更多相关文章
- 【python】命令行解析工具argparse用法
python的命令行参数 之前有用到optget, optparse, 现在这些都被弃用了. import argparse parser = argparse.ArgumentParser() ar ...
- Python 命令行解析工具 Argparse介绍
最近在研究pathon的命令行解析工具,argparse,它是Python标准库中推荐使用的编写命令行程序的工具. 以前老是做UI程序,今天试了下命令行程序,感觉相当好,不用再花大把时间去研究界面问题 ...
- python命令行解析工具argparse模块【1】
argpaser是python中很好用的一个命令行解析模块,使用它我们可以很方便的创建用户友好型命令行程序.而且argparse会自动生成帮助信息和错误信息. 一.示例 例如下面的例子,从命令行中获取 ...
- python实现命令行解析的argparse的使用
参考https://docs.python.org/3.6/library/argparse.html argparse模块使编写用户友好的命令行界面变得很容易.程序定义了它需要什么参数,argpar ...
- python命令行解析工具argparse模块【2】
上一节,我们简要的介绍了argparse的用法,接下来几节,将详细讲解其中的参数及用法,这一节我们讲解ArgumentParser对象. argparse.ArgumentParser([descri ...
- python命令行解析工具argparse模块【3】
上一节,我们讲解了ArgumentParser对象,这一节我们将学习这个对象的add_argument()方法. add_argument()方法的定义了如何解析一个命令行参数,每个参 ...
- python命令行解析工具argparse模块【5】
上一节我们学习了parse_args()的用法,这一节,我们将继续学习argparse的其他一些用法. 1.sub-commands子命令 argpar ...
- python命令行解析工具argparse模块【4】
上一节我们讲解了add_argument()方法,这一节我们将学习parse_args()方法. parse_args()方法的作用是解析命令行参数,并返回解析之后的 ...
- 【python】命令行解析工具getopt用法
处理命令行参数的模块 用法: opts, args = getopt.getopt( sys.args[1:], shortStr, longList) 输入: shortStr 形式如下: &q ...
随机推荐
- 《转》Ubuntu14.04 openstack juno配置之 ceilometer遥測模块安装配置
(一)在控制节点上 1.安装的遥測服务 apt-get install -y ceilometer-api ceilometer-collector ceilometer-agent-central ...
- js instanceof (2)
instanceof运算符可以用来判断某个构造函数的prototype属性是否存在另外一个要检测对象的原型链上.实例一:普遍用法 A instanceof B :检测B.prototype是否存在于参 ...
- python3----splitlines
Python中的splitlines用来分割行.当传入的参数为True时,表示保留换行符 \n.通过下面的例子就很明白了: mulLine = """Hello!!! W ...
- nth-child 和 nth-of-type 的区别
css3中有两个新的选择器可以选择父元素下对应的子元素,一个是:nth-child 另一个是:nth-of-type,它们2个的区别是: nth-of-type为什么要叫:nth-of-type?因为 ...
- SQL创建表脚本
<1>SQL Server设置主键自增长列 SQL Server设置主键自增长列 1.新建一数据表,里面有字段id,将id设为为主键 www.2cto.com create t ...
- exe4j中"this executable was created with an evaluation version of exe4j"
在使用exe4j时,如果您的exe4j没有注册,在运行有exe4j转换的*.jar为*.exe的可执行文件是会提示:"this executable was created with an ...
- JavaScript获取地址栏内容
例如地址为:http://www.mazey.net/baby/blog/index.php?a=1&b=2#c var query = window.location.href; //htt ...
- Oracle 提供的start with 关键字用法
在相关业务查询中,我们常常遇到相关的上下级关系情况,如下图中行政区划关联,此时就要用到Oracle 提供的start with 关键字来帮助我们进行递归查询 基本语法 SELECT ... FROM ...
- laydate日历控件
var start = { elem: '#start_0', format: 'YYYY-MM-DD', max: laydate.now(-), istime: false, istoday: f ...
- SqlProfiler的替代品-ExpressProfiler
可以用来跟踪执行的sql语句.安装SqlServer之后SqlServerManagementStudio自带一个SqlProfiler,但是如果安装的SqlExpress,那就没有了. 项目的主页在 ...