Python topic-optparse Module

Source: Internet
Author: User

Optparse = OptionParser () # The class parameters can also be defined here.

 

                  attr=value, ...)

Parser. add_option (,...) #-f and -- file are tags used as parameters during the call and will be recognized

(Options, args) = parser. parse_args () # parse_args can have parameters. If not defined, use the default sys. argv [1:]

Storage Methods: store, store_false, and store_true. If the action value is not specified, the default value is store, which tells optparse to continue reading the next parameter (type) to ensure the correctness of the type, store the value in a variable (dest), that is, the string entered in the command line saves it as the attribute of options, which can be called directly. I am dizzy with a bunch of worships ~~~~, Let's look at an example first!

>>> parser.add_option(, ,action=, type=, dest=<Option at 0x2d72f48: -f/--file>>>> args = [, >>> (options, args) =>>>

 

The value of options.filenameis foo.txt. This is stored as a "string" type. Of course, the type can also be int or float, for example:

parser.add_option(, type=, dest=)

>>> parser.add_option(, type=, dest=<Option at 0x2d71308: -n>>>> (options, args) = parser.parse_args([>>> 22

  

parser.add_option(, dest=) 

Flag is set to False. After reading the following example, it is completely clear: when Option v is encountered, it sets options. verbose to True. When option q is encountered, it sets options. verbose to False:

>>>  optparse >>> parser=

Default parameter:

>>> parser.add_option(, action=, dest=, default=<Option at 0x2d77148: -x>>>> options, args = parser.parse_args() >>>

parser.add_option(, action=, dest=, action=, dest=, default=True)

Help parameters:

>>>  optparse >>> optParser.add_option(,,action = ,type=,dest = ,help=<Option at 0x2bdb888: -f/--file>>>> fakeArgs = [,>>> options, args =>>>Usage: myprog[ -f <filename>][-s <xyz>-h, --help            show this help message -f FILENAME, --file=Usage: myprog[ -f <filename>][-s <xyz>>>> 

  

  

 

from optparse import OptionParserusage = "myprog[ -f <filename>][-s <xyz>] arg1[,arg2..]"optParser = OptionParser(usage)optParser.add_option("-f","--file",action = "store",type="string",dest = "fileName")ooptParser.add_option("-v","--vison", action="store_false", dest="verbose",default='None',                     help="make lots of noise [default]")fakeArgs = ['-f','file.txt','-v','good luck to you', 'arg2', 'arge']  options, args = optParser.parse_args(fakeArgs)print options.fileNameprint options.verboseprint optionsprint argsprint optParser.print_help()

 

: False, : , , -f <filename>][-s <xyz>-h, --help            show -f FILENAME, --file=-v, --vison           make lots of noise []

 optparse =,,action = ,type=,dest = ,, action=, dest=,default=,help== options.intNumber       i  range(1,5         i 

 

 

 

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.