Python 利用argparse模块实现脚本命令行参数解析
利用argparse模块实现脚本命令行参数解析
By:授客 QQ:1033553122
#代码实践1
study.py内容如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | #!/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() |
运行测试
1 2 | 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 |
结果如下
1 | python study.py - i127. 0.0 . 1 # 注意,参数和参数值之间可以没有空格 |
结果如下
1 | python study.py - notExists 1 |
结果如下
如上,以上代码实现是针对单个模块脚本,如果要在多个模块中使用咋办?解决方法为封装为类,具体参见“代码实践2”
#代码实践2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | 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内容如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #!/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) |
运行测试
1 | python study.py - i 127.0 . 0.1 - r |
运行结果如下
作者:授客
微信/QQ:1033553122
全国软件测试QQ交流群:7156436
Git地址:https://gitee.com/ishouke
友情提示:限于时间仓促,文中可能存在错误,欢迎指正、评论!
作者五行缺钱,如果觉得文章对您有帮助,请扫描下边的二维码打赏作者,金额随意,您的支持将是我继续创作的源动力,打赏后如有任何疑问,请联系我!!!
微信打赏
支付宝打赏 全国软件测试交流QQ群
分类:
Python
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库