python argparse模块详解多级_Python 利用argparse模块实现脚本命令行参数解析

study.py内容如下

#!/usr/bin/env python

# -*- coding:utf-8 -*-

__author__ = 'shouke'

import argparse

def argparseFunc():

'''

基于argparse模块实现命令参数解析功能

执行示例:

python study.py -i 172.19.7.236 -p 8080 -a -r

python study.py --ip 172.19.7.236 --port 7077 --auth -w -v True

'''

parser = argparse.ArgumentParser(description="study.py usage help document")

# 添加不带默认值的可解析参数

parser.add_argument("-i", "--ip", help="ip addr") #注意: -h、--help为内置参数,不可用

parser.add_argument("-p", "--port",help="host port")

# 添加带默认值的可解析参数(# action = store_true 表示是如果使用了这个参数,则值参数值设置为True # 更多action配置可参考源码

# 需要注意的是,不能为带默认值参数指定参数值,会报错,该参数值会被当作不识别的参数

parser.add_argument("-a", "--auth", help="if auth need", action="store_true")

# 添加互斥参数(比如 例中的-r和-w 同时只能用一个)

exclusive_group = parser.add_mutually_exclusive_group()

exclusive_group.add_argument("-r","--read", help="read enabled" , action="store_true")

exclusive_group.add_argument("-w","--write", help="write enabled", action="store_true")

# 添加参数时不设置设置参数说明

parser.add_argument('-v') # show verbose

# 添加参数时不设置参数全名

parser.add_argument('-V', help="version")

ARGS = parser.parse_args() # 获取命令行参数

print('ARGS:', ARGS)

# 获取某个参数值

if ARGS.ip: # 注意,这里的参数名,必须使用参数全称

print("host addr is: %s" % ARGS.ip)

if ARGS.port:

print("host port is: : %s" % ARGS.port)

if ARGS.auth:

print("auth need: : %s" % ARGS.auth)

if ARGS.read:

print("read enabled: %s" % ARGS.read)

if ARGS.write:

print("write enabled: %s" % ARGS.write)

argparseFunc()

运行测试

python study.py -i 172.19.7.236 -p 8080 -a -r

python study.py --ip 172.19.7.236 --port 7077 --auth -w -v True

结果如下

97ad34e42660d20ae3fb68a11fddb705.png

python study.py -i127.0.0.1 # 注意,参数和参数值之间可以没有空格

结果如下

7a8adabd934c44a5ed2040908f7a03fd.png

python study.py -notExists 1

结果如下

605fc2de4aeec94d9c77735775e9d201.png

如上,以上代码实现是针对单个模块脚本,如果要在多个模块中使用咋办?解决方法为封装为类,具体参见“代码实践2”

#代码实践2

argument_parser.py

#!/usr/bin/env python

# -*- coding:utf-8 -*-

'''

@Author : shouke

'''

import argparse

class ArgParser(object):

'''

参数解析器

'''

def __init__(self, none_exclusive_arguments, exclusive_arguments, description=''):

self.parser = argparse.ArgumentParser(description=description)

self.add_none_exclusive_arguments(none_exclusive_arguments)

self.add_exclusive_arguments(exclusive_arguments)

def add_none_exclusive_arguments(self, options:list):

'''

添加常规选项(非互斥选项)

:param options 格式为list类型,形如

[

'"-a", "--all", help="do not ignore entries starting with ."',

'"-b", "--block", help="scale sizes by SIZE before printing them"',

'"-C", "--color", help="colorize the output; WHEN can be 'never', 'auto'"',

'"-flag", help="make flag", action="store_true"', # action="store_true" 表示如果不设置该选项的值,则默认值为true,类似的action="store_false" 表示默认值为false

]

其中,每个list元素为argparse.ArgumentParserlei add_argument类函数实参的字符串表示,add_argument函数定义add_argument(self, *args,**kwargs)

'''

for option in options:

eval('self.parser.add_argument(%s)' % option)

def add_exclusive_arguments(self, options:list):

'''

添加互斥选项

:param options 格式为list,形如以下

[

('"-r","--read",help="Read Action",action="store_true"',

'"-w","--write",help="Write Action",action="store_true"')

]

'''

for option_tuple in options:

exptypegroup = self.parser.add_mutually_exclusive_group()

for item in option_tuple:

eval('exptypegroup.add_argument(%s)' % item)

@property

def args(self):

return self.parser.parse_args()

在xxx.py中引用(注意:为了让参数解析器起到应起的作用,建议在脚本最上方构造参数解析器对象)

study.py内容如下

#!/usr/bin/env python

# -*- coding:utf-8 -*-

__author__ = 'shouke'

from argument_parser import ArgParser

none_exclusive_arguments = [

'"-ip", help="自动化测试服务平台地址"',

'"-projectId", help="自动化测试项目id"',

'"-runEnv", help="自动化测试项目运行环境"',

'"-logLevel", help="日志级别"',

'"-masterHost", help="master服务地址"',

'"-masterPort", help="master服务端口"'

]

exclusive_arguments = [

('"-r", "--read", help="Read Action",action="store_true"',

'"-w", "--write", help="Write Action",action="store_true"')

]

args = ArgParser(none_exclusive_arguments, exclusive_arguments).args

print(args)

print(args.ip)

print(args.read)

运行测试

python study.py -i 127.0.0.1 -r

运行结果如下

b82c652cf9ff7c7655daf5dbe1b36402.pngjs

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值