"Python" Optparse

Source: Internet
Author: User

Optparse

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

use add_option to define command-line arguments: -F   or   –file   are the long and short parameter names:

Finally, once you have defined all the command-line arguments, call Parse_args () to resolve the program's command line: You can also pass a command-line argument list to Parse_args (); otherwise, the default is to use Sys.argv[:1].

two values returned by Parse_args ():

    • options, which is an object (Optpars. Values), which holds the command-line parameter value. As long as you know the command line parameter name, such as file, you can access its corresponding value: Options.file.
    • args, which is a list made up of positional arguments.

From optparse import Optionparser  [...]  Parser = Optionparser ()  parser.add_option ("-F", "--file", dest= "filename",                    help= "write report to File", Metavar= "FILE")  parser.add_option ("-Q", "--quiet",                    action= "Store_false", dest= "verbose", Default=true,                    help= "Don ' t print status messages to stdout")    (options, args) = Parser.parse_args ()



<yourscript>--file=outfile-q  <yourscript>-f outfile--quiet  <yourscript>--quiet--file outfile  <yourscript>-q-foutfile  <yourscript>-qfoutfile



Add_option () parameter description:

Action: Storage method, divided into three kinds of store, Store_false, store_true

action is one of the parameters of the Parse_args () method, which indicates how optparse is handled when resolving to a command-line argument. Actions have a fixed set of values to choose from, the default is 'store ', which means that the command line parameter values are saved in the options object

Type: Types

By default, type is ' string '. As shown above, long parameter names are also optional. In fact, the dest parameter is also optional. If the Dest parameter is not specified, the value of the options object is accessed using the command line's parameter name.

The store also has two other forms: store_true and Store_false , which are used to handle cases with no values after the command line arguments. command-line arguments such as-v,-q: in this case, when parsing to '-V ', the options.verbose will be given a True value, and conversely, parsing to '-Q ' will be given a value of False.

Other actions are: Store_const , Append , Count , Callback .

Dest: Stored variables
Default: Defaults
Help: Helpful Information

The above commands are the same effect. In addition to this, Optparse also automatically generates command line help information for us:

<yourscript>-H  <yourscript>--help  #输出usage: <yourscript> [Options]    options:-    H ,--help            Show this help message and exit    -f file,--file=file write report to  file-    q,--quiet           don ' t P Rint status messages to stdout




"Python" Optparse

Related Article

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.