python getopt使用
来源:互联网 发布:单片机读写u盘 编辑:程序博客网 时间:2024/04/29 20:53
python中 getopt 模块,
该模块是专门用来处理命令行参数的
函数getopt(args, shortopts, longopts = [])
参数args一般是sys.argv[1:]
shortopts 短格式 (-)
longopts 长格式(--)
命令行中输入:
python test.py -i 127.0.0.1 -p 80 55 66
python test.py --ip=127.0.0.1 --port=80 55 66
下面的代码:
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
options,args = getopt.getopt(sys.argv[1:],"hp:i:",["help","ip=","port="])
“hp:i:”
短格式 --- h 后面没有冒号:表示后面不带参数,p:和 i:后面有冒号表示后面需要参数
["help","ip=","port="]
长格式 --- help后面没有等号=,表示后面不带参数,其他三个有=,表示后面需要参数
返回值 options 是个包含元祖的列表,每个元祖是分析出来的格式信息,比如 [('-i','127.0.0.1'),('-p','80')] ;
args 是个列表,包含那些没有‘-’或‘--’的参数,比如:['55','66']
注意:定义命令行参数时,要先定义带'-'选项的参数,再定义没有‘-’的参数
该模块是专门用来处理命令行参数的
函数getopt(args, shortopts, longopts = [])
参数args一般是sys.argv[1:]
shortopts 短格式 (-)
longopts 长格式(--)
命令行中输入:
python test.py -i 127.0.0.1 -p 80 55 66
python test.py --ip=127.0.0.1 --port=80 55 66
下面的代码:
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
options,args = getopt.getopt(sys.argv[1:],"hp:i:",["help","ip=","port="])
“hp:i:”
短格式 --- h 后面没有冒号:表示后面不带参数,p:和 i:后面有冒号表示后面需要参数
["help","ip=","port="]
长格式 --- help后面没有等号=,表示后面不带参数,其他三个有=,表示后面需要参数
返回值 options 是个包含元祖的列表,每个元祖是分析出来的格式信息,比如 [('-i','127.0.0.1'),('-p','80')] ;
args 是个列表,包含那些没有‘-’或‘--’的参数,比如:['55','66']
注意:定义命令行参数时,要先定义带'-'选项的参数,再定义没有‘-’的参数
0 0
- python中使用getopt
- python中使用getopt
- python getopt使用
- python getopt的使用
- python getopt使用
- python getopt使用
- python getopt使用
- python getopt使用
- python getopt使用
- python getopt使用
- python getopt使用
- python getopt的使用
- python getopt使用
- getopt在Python中的使用
- python 之 getopt的使用
- getopt在Python中的使用
- getopt在Python中的使用
- getopt在Python中的使用
- physdiskwrite使用教程
- HDU 1028 Ignatius and the Princess III
- jvm 内存分配
- 最长回文子串
- 如何成为真正的数据架构师?
- python getopt使用
- 在vmware虚拟机中使用openwrt
- asp.net mvc 与JavaScript ajax使用(刚学不久,想必很菜,希望对刚学的同学有用)
- mac下配置coreseek
- 递归应用——字符串全排列,字符串组合
- Java Android 线程池
- ORACLE实际生产中小工具总结
- linux下的NFS的配置详细知识点
- 将你的Vim 打造成轻巧强大的IDE