首页 > 代码库 > python命令行参数解析实例
python命令行参数解析实例
闲言少述,直接上代码
??
?#!/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 = value
elif op == ‘-t‘ or op == ‘--stime‘:
stime = value
elif op == ‘-h‘:
usage()
sys.exit()
print "CMD : irt=%s, status=%s, seg=%d, stime=%d" % (irt, status, int(seg), int(stime))
def main():
parse_cmd_line_param()
if __name__ == "__main__":
main()
??
?#!/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 = value
elif op == ‘-t‘ or op == ‘--stime‘:
stime = value
elif op == ‘-h‘:
usage()
sys.exit()
print "CMD : irt=%s, status=%s, seg=%d, stime=%d" % (irt, status, int(seg), int(stime))
def main():
parse_cmd_line_param()
if __name__ == "__main__":
main()
python命令行参数解析实例
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。