命令行参数格式化需要python getopt模块:
1.python 命令行参数格式化
写道
try:
options,args = getopt.getopt(sys.argv[1:],"hp:i:",["help","ip=","port="])
except getopt.GetoptError:
sys.exit()
for name,value in options:
if name in ("-h","--help"):
usage()
if name in ("-i","--ip"):
print 'ip is----',value
if name in ("-p","--port")
print 'port is----',value
使用方式:python test.py -i 127.0.0.1 -p 80 55 66
2.cmdloop 命令行参数格式化
写道
def do_get_opts(self, user_input):
opts, args = getopt.getopt(user_input.split()[0:], "h:p:t:s:c:async",
["host=", "port=", "thrift=", "service=", "config=", "async"])
for opt, arg in opts:
print "%s---%s" % (opt, arg)
if opt in ('--host', '-h'):
self.config.host = arg
elif opt in ('--port', '-p'):
self.config.port = arg
elif opt in ('--async'):
self.config.async = True
elif opt in ("--t", "--thrift"):
self.config.thrift_file = arg
elif opt in ("--s", "--service"):
self.config.service = arg
elif opt in ("--c", "--config"):
self.config.config = arg
使用方式:
写道
libin@libin-ThinkPad-T420:~/workspace/yoyosvn/yoyosyssvn/src/java/datacell+/trunk/bin$ ./op_tools.py
load config file: config.py
client>send2 --h=localhost --p=7912 --t=hello.thrift --s=hello.Hello.helloString("libin") --c=params
注意两个的差别: 一个使用的是:sys.argv[1:]
一个使用的是:user_input.split()