Detailed python getopt module

 

getopt This function is used to extract the user input is determined to get sys.argv step.

getopt a module, and this module which there getopt function, so it need getopt use.

getopt.getopt ([command line parameter list], "short option" [a long list of options])

This function returns two values. Opts and args

opts there is a tuple of all options and their input values. When the input is determined, this value can not be changed.

args useful to remove the remaining part after the input.

Copy the code
1 import getopt,sys
2 shortargs = 'f:t' #短选项
3 longargs = ['directory-prefix=', 'format', '--f_long='] #长选项
4 opts,args= getopt.getopt( sys.argv[1:], shortargs, longargs)
5 print 'opts=',opts
6 print 'args=',args
Copy the code

 

Format getopt function is getopt.getopt ([command line parameter list], "short option" [a long list of options]) 
colon after the short option name (:) indicates that option must have additional parameters. 
Equal sign (=) after the long option name indicates that the option must have additional parameters.

Several output is:

Distinguish longargs long options which data is what must be added in front of the - otherwise abnormal. E.g

Correct format: ---- f_long = 'data'

Error Format: - f_long = 'data'

correct:

Guess you like

Origin www.cnblogs.com/linwenbin/p/10944261.html