The example of this article is about Python using getopt to parse command line input parameters and share them for everyone's reference.
The specific example code is as follows:
Import getopt
import sys
config = {"
input": "",
"Output": ".",
}
#getopt三个选项, the first is generally SYS.ARGV [1:], the second parameter is a short parameter, if the parameter must be followed by a value, add:, the third parameter is a long parameter
#是一个列表,
opts, args = Getopt.getopt (sys.argv[1:], ' hi:o:d ',
[
' input= ',
' output= ', '
help '
]
#参数的解析过程, long parameter is--, short parameter is-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
Parameters entered:
--input=c:\temp\aa-o c:\temp\output-d
Results of Printing:
usage-d
{' input ': ' c:\\temp\\aa ', ' Output ': ' C:\\temp\\output '}
I hope this article will help you with your Python programming.