您的位置:首页 > 编程语言 > Python开发

python_getopt解析命令行输入参数的使用

2014-09-01 13:31 696 查看
[cpp] view plaincopyprint?
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'}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息