python getopt.getopt_python 参数格式化getopt

命令行参数格式化需要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()

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值