Python getopt module, which is a function specifically designed to handle command-line arguments getopt (args, shortopts, longopts = []) parameters args are generally sys.argv[1:]shortopts short format (-)
On the command line, enter:
Python check_naginx.py-w5--warnint=5 --critical=
The following code:
Try: Options,args= Getopt.getopt (sys.argv[1:],"Hw:c:",[" Help","warning=","critical="])exceptgetopt. Getopterror:usage () Sys.exit (3) forName,valueinchOptions:ifNameinch("- H","--help"): Usage ()ifNameinch("- W","--warning"): Warning=valueifNameinch("- C","--critical"): Critical= value
"Hw:c:"
Short format---h is not followed by a colon: indicates that there is no parameter followed, p: and I: a colon after it indicates that a parameter is required later
["Help", "warning=", "critical="]
Long format---Help is not followed by an equal sign =, which means that no parameters are followed, the other three have =, indicating that the parameters are required later
Return value options is a list of meta-ancestors, each of which is the parsed format information, such as [('-w ', ' 5 '), ('-C ', ' 10 ')];
Args is a list that contains parameters that do not have '-' or '-', such as: [' 55 ', ' 66 ']
Note: When defining command-line arguments, define parameters with a '-' option, and then define parameters without '-'
Python getopt use