In this paper, we describe Python's method of using getopt to parse command line input parameters and share them for your reference.
The specific instance code is as follows:
Import getopt Import sys config = { "input": "", "Output": ".", } #getopt三个选项, the first one is generally sys.argv[1:], The second parameter is a short parameter, if the argument must follow the value, add:, the third parameter is a long parameter #是一个列表, opts, args = Getopt.getopt (sys.argv[1:], ' hi:o:d ', [ ' input= ', ' output= ', ' help ' ] ) #参数的解析过程, the long parameter is--, the short parameter is-for option, the value in the 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:\temp\aa-o c:\temp\output-d
Printed results:
usage-d{' input ': ' c:\\temp\\aa ', ' Output ': ' C:\\temp\\output '}
Hopefully this article will help you with Python programming.