Python--命令行参数解析Demo
2016-10-15 13:04
465 查看
写没有操作界面的程序时,最讨厌的就是参数解析问题,尤其是很多参数那种,下面是一个小Demo,拿出来与各位分享:
当使用OptionParser时,会自动增加--help和-h参数,也会自动生成参数帮助,如:
对于代码:
--run 表示参数名
action表示将参数值如何处理,常用的有store/store_true/store_false,store即字面意思,store_true即将True作为参数值传递给参数,store_false将False作为参数值传递给参数
dest表示命令行参数解析后的参数名,
上面代码中--run作为命令行参数传递进来,由于action为store_true,因此参数值为True,解析后的参数名为is_run,通过(options, args) = opt.parse_args() 赋值后,便可以使用options.is_run来放问参数值。
更多帮助:https://docs.python.org/2/library/optparse.html
##========================================================##
对于参数较多或者参数值较大的情况,个人还是比较喜欢使用参数配置文件来实现,简单而且方便编辑,如创建一个run_config.py文件:
然后在其他文件中访问:
简单粗暴,没那么麻烦,土豹子的做法哈!
##===================================================##
# -*- coding:utf8 -*- import os import datetime import sys from optparse import OptionParser def get_user_paras(): try: opt = OptionParser() opt.add_option('--host_ip', dest='host_ip', type=str, help='the ip of the check host') opt.add_option('--run', action="store_true", dest="is_run", default=False, help="run the scripts") opt.add_option('--view', action="store_false", dest="is_run", default=False, help="only view but not run the scripts") opt.add_option('--show_type', dest="show_type", type=int, default=0, help="0 or 1, 0 only show the simple data, 1 show the full data") (options, args) = opt.parse_args() is_valid_paras = True error_messages = [] host_ip = options.host_ip is_run = options.is_run show_type = options.show_type if not host_ip: error_messages.append("host_ip must be set;") is_valid_paras = False if show_type not in [0, 1]: error_messages.append("show_type only can be 0 or 1;") is_valid_paras = False if is_valid_paras: user_paras = {"host_ip": host_ip, "is_run": is_run, "show_type": show_type} return user_paras else: for error_message in error_messages: print(error_message) opt.print_help() return None except Exception as ex: print("exception :{0}".format(str(ex))) return None def main(): user_paras = get_user_paras() if user_paras is None: sys.exit(0) info = "host_ip:{0}, is_run:{1}, show_type:{2}" info = info.format(user_paras["host_ip"], user_paras["is_run"], user_paras["show_type"]) print(info) if __name__ == '__main__': main()
当使用OptionParser时,会自动增加--help和-h参数,也会自动生成参数帮助,如:
对于代码:
opt.add_option('--run', action="store_true", dest="is_run", default=False, help="run the scripts")
--run 表示参数名
action表示将参数值如何处理,常用的有store/store_true/store_false,store即字面意思,store_true即将True作为参数值传递给参数,store_false将False作为参数值传递给参数
dest表示命令行参数解析后的参数名,
上面代码中--run作为命令行参数传递进来,由于action为store_true,因此参数值为True,解析后的参数名为is_run,通过(options, args) = opt.parse_args() 赋值后,便可以使用options.is_run来放问参数值。
更多帮助:https://docs.python.org/2/library/optparse.html
##========================================================##
对于参数较多或者参数值较大的情况,个人还是比较喜欢使用参数配置文件来实现,简单而且方便编辑,如创建一个run_config.py文件:
# -*- coding:utf8 -*- # run config class RunConfig(object): is_run = True show_status = 1 host_ip = "192.167.1.1" run_scripts = """ SELECT * FROM TB001 WHERE ID >1000 AND C1<300 """
然后在其他文件中访问:
# -*- coding:utf8 -*- from run_config import RunConfig def main(): print("is_run:{0}, host_ip:{1}".format(RunConfig.is_run,RunConfig.host_ip)) print("run_scripts:{0}".format(RunConfig.run_scripts)) if __name__ == '__main__': main()
简单粗暴,没那么麻烦,土豹子的做法哈!
##===================================================##
相关文章推荐
- python模块之argparse:强大的命令行参数解析模块
- python命令行参数解析示例应用
- python基于protobu+websocket+tornado实现多种二进制协议消息的接收发送和解析demo
- python 解析命令行参数
- python 命令行参数解析的标准模板
- python命令行参数解析OptionParser类
- 【Python】 配置解析ConfigParser & 命令行参数解析optparser
- Python发送http请求解析返回json小demo
- Python命令行参数解析:getopt和argparse
- python命令行参数解析实例
- Python命令行参数解析模块getopt使用实例
- python 命令行参数解析 argparse简单分析
- Python中的命令行参数解析工具之docopt详解
- python命令行参数解析模块argparse和docopt
- Python 列表解析 例子 讲解 学习 list comprehension in python demo
- Python解析命令行参数模块getopt
- python 命令行参数解析
- 【Python 命令行参数解析: optparse 模块】
- Python——解析命令行参数docopt模块
- python中对象序列化及tensorflow的命令行参数解析