Python getopt use

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.