python_getopt解析命令行输入参数的使用
2015-07-31 18:47
1011 查看
<span style="font-size:18px;">import getopt 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 </span>
import getopt 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实现Table To Point代码
- Python实现Table To Point代码 分类: Python 2015-07-31 18:45 3人阅读 评论(0) 收藏
- python 之 分割参数getopt
- python2在pycharm中的中文乱码问题处理方式
- python3.x中的urllib模块
- Python实现fibnacci
- Python初步
- Python调用windows下DLL详解 - ctypes库的使用
- python 函数式编程
- Python GUI编程各种实现的对比
- python Iterable和Iterator
- Python中的局部变量和全局变量
- python range()函数
- python实现爬图,不要再爬妹子图了,太没品
- python的range()函数用法
- spark 集群运行python作业
- python 生成器
- python初学小结4
- python初学小结2
- python初学小结3