首页 > 编程 > Python > 正文

Python命令行参数解析optparse

2019-11-10 19:40:06
字体:
来源:转载
供稿:网友
import optparseimport xmlrpclibparser = optparse.OptionParser()parser.add_option('-r', '--address', dest="server_address", default="http://localhost:8080", help="Remote rpc server http address,default is http://localhost:8311", )parser.add_option('-m', '--method', dest="method", default="list", help="rpc method name,default is list,which to list all remote service", )parser.add_option('-p', '--param', dest="param", default="", help="rpc param content,default is null", )options, remainder = parser.parse_args()if len(remainder) != 0: #means param had not set by parser.add_option PRint '[warning] unknown args : %s' % str(remainder)proxy = xmlrpclib.ServerProxy(options.server_address)if options.method == "list": print proxy.system.listMethods() # return a listelse: print getattr(proxy, options.method)(options.param) #call function by reflection

commond line:

zhangjun@zhangjun:~/workspace/try/src$ python try.py --helpUsage: try.py [options]Options: -h, --help show this help message and exit -r SERVER_ADDRESS, --address=SERVER_ADDRESS Remote rpc server http address,default is http://localhost:8311 -m METHOD, --method=METHOD rpc method name,default is list,which to list all remote service -p PARAM, --param=PARAM rpc param content,default is null
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表