python getopt使用
来源:互联网 发布:淘宝爱爱丸小虎牙模特 编辑:程序博客网 时间:2024/04/25 02:07
转载:http://blog.csdn.net/tianzhu123/article/details/7655499
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']
注意:定义命令行参数时,要先定义带'-'选项的参数,再定义没有‘-’的参数
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中的使用
- ant.xml文件
- C# 属性和字段 get set
- 分布式网站架构后续:zookeeper技术浅析
- drools入门(二)-----规则引擎Drools解决汽水问题(复杂逻辑)
- LeetCode 8 String to Integer (atoi)
- python getopt使用
- ALM损坏后的恢复步骤
- 初次使用keepalived应该注意的
- ListView.addHeadView添加ViewPager,ViewPager左右切换影响ListView上下滑动
- PHP生成静态页面详细教程
- cocoapods的各种坑
- linux cooked capture
- 百度地图2.4.1SDK监听覆盖物的点击事件
- mfc窗口创建的create与oncreate