Python Module Learning--optparse

Source: Internet
Author: User

Optparse built-in modules are used to process Python command line parameters.

Simple Process

First, you must import the Optionparser class to create a Optionparser object:

  1. from  OPTPARSE  import  OPTIONPARSER  
  2. [...]
  3. Parser = Optionparser ()
 

Then, use add_option to define the command-line arguments:

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

Each command-line argument consists of the parameter name string and the parameter property. such as- f or –file are the long and short parameter names:

    1. parser.add_option ("-F", "--file", ...)

Finally, once you have defined all the command-line arguments, call Parse_args () to resolve the program's command line:

    1. (options, args) = Parser.parse_args ()
Example

Here 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 ()

Python Module Learning--optparse

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.