python_getopt解析命令行输入参数的使用
2015-10-27 19:27
901 查看
import getopt
import sys
config = {
“input”:”“,
“output”:”.”,
}
[
‘input=’,
‘output=’,
‘help’
]
)
if option in [“-h”,”–help”]:
print “””
usage:%s –input=[value] –output=[value]
usage:%s -input value -o value
“””
elif option in [‘–input’, ‘-i’]:
config[“input”] = value
elif option in [‘–output’, ‘-o’]:
config[“output”] = value
elif option == “-d”:
print “usage -d”
print config
输入的参数:–input=c:\temp\aa -o c:\temp\output -d
打印的结果:
usage -d
{‘input’: ‘c:\temp\aa’, ‘output’: ‘c:\temp\output’}
import sys
config = {
“input”:”“,
“output”:”.”,
}
getopt三个选项,第一个一般为sys.argv[1:],第二个参数为短参数,如果参数后面必须跟值,须加:,第三个参数为长参数
是一个列表,
opts, args = getopt.getopt(sys.argv[1:], ‘hi:o:d’,[
‘input=’,
‘output=’,
‘help’
]
)
参数的解析过程,长参数为–,短参数为-
for option, value in opts:if option in [“-h”,”–help”]:
print “””
usage:%s –input=[value] –output=[value]
usage:%s -input value -o value
“””
elif option in [‘–input’, ‘-i’]:
config[“input”] = value
elif option in [‘–output’, ‘-o’]:
config[“output”] = value
elif option == “-d”:
print “usage -d”
print config
输入的参数:–input=c:\temp\aa -o c:\temp\output -d
打印的结果:
usage -d
{‘input’: ‘c:\temp\aa’, ‘output’: ‘c:\temp\output’}
相关文章推荐
- python学习——基础
- python 学习笔记(02)
- 类练习题1:将浮点数转化为金额的类
- python 学习笔记(01)
- Python 日记-1
- Ubuntu14.04 安装配置Opencv3.0和Python2.7
- sublime text2中运行java和python
- python中结巴包的一些东西
- python 装饰器
- python 自动化测试平台 Robot Framework 内置库 翻译
- python PIL模块的下载
- pyenv python多版本共存
- python学习记录之1027
- Python的集合类型详解17
- 【Python】Python-skier游戏[摘自.与孩子一起学编程]
- python异常处理
- python yield
- Python调试工具pdb
- python学习手册:字典,方法
- 卸载pythonwin&python