Python USES getopt to parse command line input parameter instances
- 2020-04-02 14:12:26
- OfStack
This article illustrates how python USES getopt to parse command line input parameters.
The specific example code is as follows:
import getopt
import sys
config = {
"input":"",
"output":".",
}
#getopt Three choices. The first one is usually sys.argv[1:], The second parameter is a short parameter, if the parameter must be followed by the value, must be added : , the third parameter is a long parameter
# It's a list,
opts, args = getopt.getopt(sys.argv[1:], 'hi:o:d',
[
'input=',
'output=',
'help'
]
)
# Parameter parsing process , Long parameters for -- , the 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
Input parameters:
--input=c:tempaa -o c:tempoutput -d
Printed results:
usage -d
{'input': 'c:\temp\aa', 'output': 'c:\temp\output'}
I hope this article has helped you with your Python programming.