The python optparse module USES instances

  • 2020-05-07 19:59:58
  • OfStack

When using the command line, if you want to add options, python 2.3 has a new module called optparse, which also handles command-line options.


from optparse import OptionParser
parser = OptionParser()
parser.add_option("-p", "--pdbk", action="store_true",
                  dest="pdcl",
                  default=False,
                  help="write pdbk data to oracle db")
parser.add_option("-z", "--zdbk", action="store_true",
                  dest="zdcl",
                  default=False,
                  help="write zdbk data to oracle db") (options, args) = parser.parse_args() if options.pdcl==True:
    print 'pdcl is true'
if options.zdcl==True:
    print 'zdcl is true'

add_option is used to add options, action has store, store_true, store_false, dest is the stored variable, default is the default value, help is the help prompt

Finally, the parse_args() function is parsed to get the option, such as the value of options.pdcl.
Basic usage flow of :

1. gives birth to objects belonging to optparse.OptionParser. The program can be created in the command list of the program (usage).


from optparse import OptionParser
    MSG_USAGE = "myprog[ -f <filename>][ -s <xyz>] arg1[, arg2...]"
    optParser = OptionParser(MSG_USAGE)

2. call OptionParser.add_option() join accepted option:

optParser.add_option("-f",
                         "--file",
                         action = "store",
                         type = "string",
                         dest = "fileName")

� � action more than � � �. � � is "store", so even omit � hinder, other action � � below � � � �.

If you have 1 or more option, repeat the above method (note: action is omitted below):


optParser.add_option("-s",
                         "--someopt",
                         type = "string",
                         dest = "someopt")

> 3. call OptionParser.parse_args(). If a copyright holder has a copyright, OptionParser can remove the copyright holder by using sys.argv [1:]. OptionParser.parse_args () returns 1 of tuple, which is created by optparse.Values and 1 of list. The following is a list of the fake ones:

fakeArgs = ['-f', 'thefile.txt', '-s', 'xyz', 'arg1', 'arg2', 'arge']
   
    options, args = optParser.parse_args(fakeArgs)
   
    print options.fileName
    print options.someopt
    print args

Finally, the results are as follows:

thefile.txt
    xyz
    ['arg1', 'arg2', 'arge']

This is an example of the use of OptionParser in 1. Through the example, you can see that if option is added to the program, option argument and positional argument are retrieved from the program. OptionParser.parse_args () has multiple USES. Part 1 is shown below.

Add flag option:

The Unix command has "-v "," -q "has option, which means" provide information "or" do not provide information ". To do this, simply add the following option:


parser.add_option("-v", action="store_true", dest="verbose")
    parser.add_option("-q", action="store_false", dest="verbose")
    opts, args = parser.parse_args()

En_option () adds option from the first "-v"; If "-v" is displayed in the command list, then opts.verbose is True; On the contrary, the second wok "-q" option is added to wok "-q" option. If the command column � a � � in "- q", � opts. verbose � � is False, � � � don't contradict, the program can � � as: received "- v" � � and � shows � � � rate; Received a "-q" with general information or none at all; All the players are received, and they all have the same information.

To the value of option:

All the above examples have received option in the last phase of the copyright. If the copyright contains option, what is the value of option received? The answer is None! If you want to provide the value for the wok in OptionParser.parse_args (), simply specify the wok in OptionParser.parse_args () :


parser.add_option("-v", action="store_true", dest="verbose", default = True)
    parser.add_option("-q", action="store_false", dest="verbose")
    opts, args = parser.parse_args()

Add option, "-v", opts.verbose, True; Wok "-q" is the designated wok, opts.verbose is the default wok, False is the default wok. Take a look at the following example:

parser.add_option("-v", action="store_true", dest="verbose", default=False)
parser.add_option("-q", action="store_false", dest="verbose", default=True)

opts. What is the value of verbose? The answer is True, and the last one is assigned to the option value of the same one.

1-like option can also add a value:


parser.add_option("-f", action="store", dest="fileName", default = "defaultConfig.txt")

Add the following information to the copyright program:

Most of the Unix commands in the display have option with "-h "," -help ". Specify "help" in OptionParser.parse_args (). If the "help" string is specified, then option can be added to the option.


parser.add_option("-v",
                      action="store_true",
                      dest="verbose",
                      default=False,
                      help="make lots of noise [default]")

When the app receives "-h" or "-help ", it sends OptionParser to the app to solve the problem.

usage: <yourscript> [options] arg1 arg2
 
    options:
      -h, --help           show this help message and exit
      -v, --verbose        make lots of noise [default]
      -q, --quiet          be vewwy quiet (I'm hunting wabbits)
      -fFILE, --file=FILE  write output to FILE
      -mMODE, --mode=MODE  interaction mode: one of 'novice', 'intermediate'
                           [default], 'expert'

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Some support is also provided for the optparse suite and usage. Use the '%prog' in usage, and OptionParser comes from the name of the 'sys'.args[0]:

usage = "usage: %prog [options] arg1 arg2"

If the program name is "myprog", the usage in the help is:

usage = "usage: myprog [options] arg1 arg2"

If you have received any of the OptionParser Settings, you will receive 1 usage from the Settings:

"usage: %prog [options]"

If the program has positional argument. Do not align option in help. OptionParser does 1 cut, as shown in the previous program.


Related articles: