Python module learning-optparse, python module optparse

Source: Internet
Author: User

Python module learning-optparse, python module optparse

The optparse built-in module is used to process Python command line parameters.

Simple process

First, you must import the OptionParser class to create an OptionParser object:

  1. From optparse import OptionParser
  2. [...]
  3. Parser = OptionParser ()
 

Then, use add_option to define the command line parameters:

  1. Parser. add_option (opt_str ,...,
  2. Attr = value ,...)
 

Each command line parameter is composed of a parameter name string and parameter attributes. For example-FOr-FileThe parameter names are respectively long and short:

  1. Parser. add_option ("-f", "-- file ",...)
 

Finally, once you have defined all the command line parameters, call parse_args () to parse the program's command line:

  1. (Options, args) = parser. parse_args ()
Example

The following is a simple example of using optparse:

  1. From optparse import OptionParser
  2. [...]
  3. Parser = OptionParser ()
  4. Parser. add_option ("-f", "-- file", dest = "filename ",
  5. Help = "write report to FILE", metavar = "FILE ")
  6. Parser. add_option ("-q", "-- quiet ",
  7. Action = "store_false", dest = "verbose", default = True,
  8. Help = "don't print status messages to stdout ")
  9. (Options, args) = parser. parse_args ()

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.