您的位置:首页 > 编程语言 > Python开发

python命令行参数解析实例

2014-09-02 11:28 549 查看
闲言少述,直接上代码

#!/usr/bin/env python

#

#

import json

import getopt, sys

def usage():

print sys.argv[0] + ' -i irt -s status'

print sys.argv[0] + ' -i irt -n seg -t stime'

print sys.argv[0] + ' -h # get help info'

def parse_cmd_line_param():

try:

opts, args = getopt.getopt(sys.argv[1:], "hi:s:n:t:",["help","irt=","status=","seg=","stime="])

except getopt.GetoptError, err:

# print help information and exit;

usage()

sys.exit(2)

irt = None

status = None

seg =0

stime = 0

for op, value in opts:

if op == '-i' or op == '--irt':

irtmp = value

elif op == '-s' or op == '--status':

status = value

elif op == '-n' or op == '--seg':

segnum = int(value)

elif op == '-t' or op == '--stime':

stime = int(value)

elif op == '-h':

usage()

sys.exit()

print "CMD : irt=%s, status=%s, seg=%d, stime=%d" % (irt, status, seg, stime)

def main():

parse_cmd_line_param()

if __name__ == "__main__":

main()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: