Python中getopt的使用(管理命令行参数)

转载自:http://blog.csdn.net/tianzhu123/article/details/7655499python中 getopt 模块,该模块是专门用来处理命令行参数的。函数getopt(args, shortopts, longopts = [])参数args就是命令行传入的,在python中一般是sys.argv[1:]参数分为长短两种:shortopts 短格式 (-)longopts 长格式(–)例如下述,第1行是短,第2行长:

python test.py -i 127.0.0.1 -p 80 55 66python test.py --ip=127.0.0.1 --port=80 55 66

下面是对应的代码:

import getopttry:    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

下面来解释一下:(1) 短格式 hp:i:h 后面没有冒号:表示后面不带参数,p:和 i:后面有冒号表示后面需要参数。(2) 长格式 [“help”,”ip=”,”port=”]help后面没有等号=,表示后面不带参数,其他三个有=,表示后面需要参数。返回值 options 是个包含元祖的列表,每个元祖是分析出来的格式信息,比如 [(‘-i’,’127.0.0.1′),(‘-p’,’80’)] ;args 是个列表,包含那些没有‘-’或‘–’的参数,比如:[’55’,’66’]注意:定义命令行参数时,要先定义带’-‘选项的参数,再定义没有‘-’的参数备注:一般情况下,有短参数足矣。

Python中getopt的使用(管理命令行参数)

相关文章:

你感兴趣的文章:

标签云: