Optionparser can easily generate standard, UNIX/POSIX-compliant command-line instructions.
From optparse Import optionparserparser = Optionparser () parser.add_option ('-P ', '--pdbk ', action = ' store_true ', dest = ' P DCL ', default = False, help = ' Write PDBK data to Oracle DB ') parser.add_option ('-Z ', '--zdbk ', action = ' s Tore_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. '
Simple process
# Introduce Optionparser class, create Optionparser object from optparse import Optionparserparser = Optionparser () # define command line arguments parser.add_option ( Opt_str, ..., attr = value, ...) # Parse command-line arguments (options, args) = Parser.parse_args ()
You can manually pass a command line list to Parse_args () in the program, otherwise the default is to use Sys.argv[:-1]
Parse_args () returns two values, Options holds command-line parameter values, and args is a list of positional arguments
Add_option () To add options, Parse_args () parsing options
Example:
From optparse Import optionparserparser = Optionparser () parser.add_option ('-f ', '--file ', dest = ' filename ', Metavar = ' FI LE ', help = ' Write report to FILE ') parser.add_option ('-Q ', '--quit ', action = ' store_false ', dest = ' Verbo Se ', default = True, help = ' don\ ' t print status message to stdout. ') (options, args) = Parser.parse_args ()
The action default store indicates that the parameter values are saved in the options object
Example:
From optparse Import optionparserparser = Optionparser () parser.add_option ('-f ', '--file ', action = ' store ', type = ' String ' ', dest = ' filename ') args = ['-F ', ' foo.txt '] (options, args) = Parser.parse_args (args) print Options.filename
Type default ' String ', can also be ' int ' or ' float ', etc., long parameter name is optional, dest will use command line parameter name to access the value of the options object when unspecified
There are two other forms of the store: Store_true and Store_false, and Store_const, append, Count, callback
Example:
Parser.add_option ('-V ', action = ' store_true ', dest = ' verbose ') parser.add_option ('-Q ', action = ' store_false ', dest = ' ve Rbose ') # when parsing to '-V ', Options.verbose is True, parsing to '-Q ', Options.verbose to False
Default setting parameter Defaults
Example:
Parser.add_option ('-f ', action = ' Store ', dest = ' filename ', default = ' Foo.txt ') parser.add_option ('-V ', action = ' store_t Rue ', dest = ' verbose ', default = True) # can also use Set_default () parser.set_defaults (filename = ' foo.txt ', verbose = True) parse R.add_option (...) (options, args) = Parser.parse_args ()
Help Generation Assistance Information
Example:
usage = ' usage: %prog [options] arg1 arg2 ' parser = optionparser (usage = usage) parser.add_option ('-V ', '--verbose ', action = ' store_true ', dest = ' verbose ', default = true, help = ' make lots of noise [ Default] ') parser.add_option ('-Q ', '--quiet ', action = ' store_false ', dest = ' verbose ', help = ' Be very quiet ') parser.add_option ('-F ', '--filename ', metavar = ' FILE ', help = ' Write output to file ') parser.add_option ('-M ', '--mode ', metavar= ' MODE ', default = ' Intermediate ', help = ' interaction mode: novice, intermediate, or Expert [default: %default] ')
Optparse parsing to help no longer resolves other command line parameters, the usage information will be printed first, the default "usage:%prog [options]"
Metavar reminds the user of the parameters expected by this parameter, such as Metavar = ' mode ' will be shown in the help as-M mode,--mode=mode
OptionGroup parameter Grouping
Example:
Group = OptionGroup (parser, ' dangerous options ', ' caution:use these options at your own risk. It is believed that some of them bite. ') Group.add_option ('-g ', action = ' store_true ', help = ' Group option. ') Parser.add_option_group (Group)
Version specifies this parameter when creating a Optionparser object, which is interpreted as a--version command-line argument
Optparser can automatically detect and handle some user exceptions, or you can use the Parser.error () method to customize the handling of partial exceptions
If Options.a and Options.b:parser.error (' options-a and-b is mutually exclusive ')
Python's Optionparser module