Python中最好用的命令行解析工具:argparse

Python 做为一个脚本语言,可以很方便地写各种工具。当你在服务端要运行一个工具或服务时,输入参数似乎是一种硬需(当然你也可以通过配置文件来实现)。

如果要以命令行执行,那你需要解析一个命令行参数解析的模块来帮你做这个苦力活。

Python 本身就提供了三个命令行参数解析模块,我这里罗列一下它们的大致情况供你了解。

  • getopt,只能简单的处理命令行参数
  • optparse,功能强大,易于使用,可以方便地生成标准的、符合Unix/Posix 规范的命令行说明。(Python2.7以后弃用,不会继续发展)
  • argparse,使其更加容易的编写用户友好的命令行接口。它所需的程序进程了参数定义,argparse将更好的解析sys.argv。同时argparse模块还能自动生成帮助及用户输入错误参数时的提示信息。

很多初学者可能会使用getopt,上手简单功能也简单。比如说optget无法解析一个参数多个值的情况,如 --file file1 file2 file3,而 optparse 实际上我没有用过,但是考虑到它在Python2.7后已经不再维护,我们通常也不会使用它。

接下来只剩下 argparse 这一神器,它几乎能满足我对命令解析器的所有需求。它支持解析一参数多值,可以自动生成help命令和帮助文档,支持子解析器,支持限制参数取值范围等等功能。

0. HelloWorld

不管学习什么东西,首先第一步都应该是掌握它的大体框架。

而 使用 argparse 前,框架很简单,你只需要记住这三行。

# mytest.pyimport argparseparser = argparse.ArgumentParser(description="used for test")

args = parser.parse_args()

现在可以尝试一下

[root@localhost ~]# python mytest.py -husage: mytest.py [-h]

used for test

optional arguments:  -h, --help  show this help message and exit[root@localhost ~]# [root@localhost ~]# [root@localhost ~]# python mytest.py[root@localhost ~]# 

已经可以使用了。

1. 入门配置

这里先讲一下,比较常用的参数配置。

  • 调试:debug
  • 版本号:version
import argparseparser = argparse.ArgumentParser()

parser.add_argument(‘--version‘, ‘-v‘, action=‘version‘,                    version=‘%(prog)s version : v 0.01‘, help=‘show the version‘)

parser.add_argument(‘--debug‘, ‘-d‘, action=‘store_true‘,                    help=‘show the version‘,                    default=False)

args = parser.parse_args()print("=== end ===")

上面debug处的配置,需要讲一下的是 action=‘store_true‘default = False 的作用和区别

  • store_true:一旦指定了 -d 或者 --debug ,其值就为 True,store_false则相反
  • default=False:未指定 -d 或者 --debug,其值就默认为False

当我们执行 python mytest.py -v,就会打印 version 里的内容。

[root@localhost ~]# python mytest.py -vmytest.py version : v 0.01[root@localhost ~]# 

一旦执行时,指定了参数 -v ,执行到 parser.parse_args() 就会退出程序,不会打印最后的 === end ===

2. 参数种类

参数可分为 必选参数(positional arguments) 和 可选参数(optional arguments)。

在argsparse 里如何实现呢?

必选参数

用单词做参数,默认就为必选参数

# mytest.pyimport argparse

parser = argparse.ArgumentParser()parser.add_argument("name")

args = parser.parse_args()

print(args.name)

不指定name参数运行一下:python mytest.py

[root@localhost ~]# python mytest.py usage: mytest.py [-h] namemytest.py: error: too few arguments[root@localhost ~]#

如预期一样,报错了,说缺少参数。那我们指定一下:python mytest.py name wangbm

[root@localhost ~]# python mytest.py wangbmwangbm[root@localhost ~]# 

可选参数

有两种方式:

  1. 单下划线 - 来指定的短参数,如-h
  2. 双下划线 -- 来指定的长参数,如--help
# mytest.pyimport argparse

parser = argparse.ArgumentParser()parser.add_argument("-v", "--verbosity", help="increase output verbosity")

args = parser.parse_args()

if args.verbosity:    print("verbosity turned on")else:    print("verbosity turned off")

试着运行一下 python mytest.py,不会报错。

[root@localhost ~]# python mytest.pyverbosity turned off[root@localhost ~]#

3. 参数类型

有的参数,是字符串,有的参数,是数值。

为了对命令行中的参数进行有效的约束,我们可以事先对参数的类型进行声明。argparse 会对参数进行校验,不通过时,会直接抛出错误。

# mytest.pyimport argparse

parser = argparse.ArgumentParser()parser.add_argument("name")parser.add_argument("age", type=int)

args = parser.parse_args()

print(args.name)print(args.age)

测试一下唄。

[root@localhost ~]# python mytest.py wangbm eighteenusage: mytest.py [-h] name agemytest.py: error: argument age: invalid int value: ‘eighteen‘[root@localhost ~]# [root@localhost ~]# python mytest.py wangbm 18wangbm18[root@localhost ~]#

你看,写 eighteen 就不行,提示类型不合法,只有写 18 才行。

4. 互斥参数

有些参数,是互斥的,有你无我。比如,性别。

在 argparse 中如何实现?

import argparse

parser = argparse.ArgumentParser()group = parser.add_mutually_exclusive_group()group.add_argument("-m", "--male", action="store_true")group.add_argument("-f", "--female", action="store_true")args = parser.parse_args()

如果同时指定了这两个参数,就会报错。

[root@localhost ~]# python mytest.py -f[root@localhost ~]# python mytest.py -m[root@localhost ~]# python mytest.py -m -f usage: mytest.py [-h] [-m | -f]mytest.py: error: argument -f/--female: not allowed with argument -m/--male[root@localhost ~]# 

5. 可选值

如果是性别,可以像上面那样放在两个参数里然后用互斥组来约束,也可以放在一个参数里,在argparse里限制再在外层做判断。

# mytest.pyimport argparse

parser = argparse.ArgumentParser()parser.add_argument("-g", "--gender", default=‘male‘,                    choices=[‘male‘, ‘female‘])

args = parser.parse_args()print(args.gender)

试着执行一下,发现性别只能是男或女,不能为人妖。

[root@localhost ~]# python mytest.py --gender malemale[root@localhost ~]# python mytest.py --gender femalefemale[root@localhost ~]# [root@localhost ~]# [root@localhost ~]# python mytest.py --gender otherusage: mytest.py [-h] [-g {male,female}]mytest.py: error: argument -g/--gender: invalid choice: ‘other‘ (choose from ‘male‘, ‘female‘)[root@localhost ~]#

6. 指定文件

经常会有那种要在脚本中指定配置文件或者其他文件的需求。可以使用下面的配置

import argparseparser = argparse.ArgumentParser()

parser.add_argument(‘--file‘, ‘-f‘, action=‘append‘,                    dest=‘files‘,                    help=(‘additional yaml configuration files to use‘),                    type=argparse.FileType(‘rb‘))

args = parser.parse_args()

dest=files,是说将命令行中,--file 的参数值赋值给变量files,你可以用args.files访问。

action=append,由于我们会有指定多个文件的需求,那就指定多次--file ,argparse会将其放在一个list里。

type=argparse.FileType(‘rb‘),既然是指定文件,那么参数应该为路径,并指定打开模式为rb,如果如果要取得文件内容,可以用 args.files[0].read()

7. 子解析器

如果你对命令行,有过足够多的接触,就会知道有些情况下会有子解析器。

这里我以自己工作中,碰到的例子来举个例子。

cloud-init --debug single -name mymodule

其中 single 后面是一个子解析器。

# cloud-init.py

def main_single(name, args):    print("name: ", name)    print("args: ", args)    print("I am main_single")

# 添加一个子解析器subparsers = parser.add_subparsers()

parser_single = subparsers.add_parser(‘single‘,help=‘run a single module‘)

# 对single 子解析器添加 action 函数。parser_single.set_defaults(action=(‘single‘, main_single))

# require=True,是说如果命令行指定了single解析器,就必须带上 --name 的参数。parser_single.add_argument("--name", ‘-n‘, action="store",                           help="module name to run",                           required=True)

args = parser.parse_args()

(name, functor) = args.actionif name in ["single"]:    functor(name, args)

执行命令cloud-init single -name mymodule,输出如下

name:  singleargs:  Namespace(action=(‘single‘, <function main_single at 0x0000000003F161E0>), debug=False, file=None, name=‘mymodule‘)I am main_single

关注公众号,获取最新干货!

原文地址:https://www.cnblogs.com/wongbingming/p/10409919.html

时间: 2024-07-31 02:33:57

Python中最好用的命令行解析工具:argparse的相关文章

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'

转:python命令行解析工具Argparse

转自:http://www.cnblogs.com/jianboqi/archive/2013/01/10/2854726.html 最近在研究pathon的命令行解析工具,argparse,它是Python标准库中推荐使用的编写命令行程序的工具. 以前老是做UI程序,今天试了下命令行程序,感觉相当好,不用再花大把时间去研究界面问题,尤其是vc++中尤其繁琐. 现在用python来实现命令行,核心计算模块可以用c自己写扩展库,效果挺好. 学习了argparse,在官方文档中找到一篇toturia

python命令行解析工具argparse模块【1】

argpaser是python中很好用的一个命令行解析模块,使用它我们可以很方便的创建用户友好型命令行程序.而且argparse会自动生成帮助信息和错误信息. 一.示例 例如下面的例子,从命令行中获取几个整数,然后获取它们的和或者最大值. import argparse parser = argparse.ArgumentParser(description='Process some integers.') parser.add_argument('integers', metavar='N'

python命令行解析工具argparse模块【3】

上一节,我们讲解了ArgumentParser对象,这一节我们将学习这个对象的add_argument()方法. add_argument()方法的定义了如何解析一个命令行参数,每个参数都有各自独立的设置参数. 1.name or flags add_argument()必须知道参数是可选的还是必须的位置参数,第一个传递给add_arguments的参数必须是可选参数或者是位置参数,例如,下面是可选参数. >>> parser.add_argument('-f','--foo') 而位置

python命令行解析工具argparse模块【4】

上一节我们讲解了add_argument()方法,这一节我们将学习parse_args()方法. parse_args()方法的作用是解析命令行参数,并返回解析之后的命名空间.默认的,参数从sys.argv中获取.       1.参数值语法 parse_args()支持多种语法来解析参数,最简单的方式如下,参数与值分开传递 >>> parser = argparse.ArgumentParser(prog='PROG') >>> parser.add_argument

命令行解析工具argparse简单使用-1

1.基本使用#01.py import argparse parser = argparse.ArgumentParser()    parser.parse_args() $ python 01.py$$ python 01.py --helpusage: 01.py [-h] optional arguments:  -h, --help  show this help message and exit 2.位置参数#02.py import argparse parser = argpar

Python命令行解析库argparse

python标准库推荐使用argparse模块对命令行进行解析. 创建解析器 import argparse parser = argparse.ArgumentParser() 创建一个ArgumentParser实例对象,ArgumentParser对象的参数都为关键字参数. class ArgumentParser (prog=None, usage=None, description=None, epilog=None, parents=[], formatter_class=argpa

Python命令行解析库argparse(转)

原文:http://www.cnblogs.com/linxiyue/p/3908623.html 2.7之后python不再对optparse模块进行扩展,python标准库推荐使用argparse模块对命令行进行解析. 1.example 有一道面试题:编写一个脚本main.py,使用方式如下: main.py -u http://www.sohu.com -d 'a=1,b=2,c=3' -o /tmp/index.html 功能要求:打开-u指定的页面,将页面中所有的链接后面增加参数a=

Google开源命令行解析工具gflags

转自:https://blog.csdn.net/achelloworld/article/details/41959595# gflags是google开源的一套命令行参数解析工具,支持C++和Python语言,其使用方法: 1. 定义参数  使用gflags需要包含头文件#include <gflags/gflags.h>.将需要的命令行参数使用gflags的宏:DEFINE_xxxxx(变量名,默认值,help-string) 定义在文件当中,定义的参数是全局的,gflags支持的参数类