Example
Copy Code code as follows:
From Optparse import Optionparser
[...]
def main ():
Usage = "Usage:%prog [options] arg"
Parser = Optionparser (usage)
Parser.add_option ("F", "--file", dest= "filename",
help= "read data from FILENAME")
Parser.add_option ("-V", "--verbose",
Action= "Store_true", dest= "verbose")
Parser.add_option ("Q", "--quiet",
Action= "Store_false", dest= "verbose")
[...]
(options, args) = Parser.parse_args ()
If Len (args)!= 1:
Parser.error ("Incorrect number of arguments")
If Options.verbose:
Print "Reading%s ..."% Options.filename
[...]
if __name__ = = "__main__":
Main ()
Add Options (Add_option ())
Copy Code code as follows:
Optionparser.add_option (option)
Optionparser.add_option (*opt_str, Attr=value, ...)
Define short options
Copy Code code as follows:
Parser.add_option ("F", Attr=value, ...)
Define long Options
Copy Code code as follows:
Parser.add_option ("–foo", Attr=value, ...)
If you define
Copy Code code as follows:
Parser.add_option ("F", "--file", action= "store", type= "string", dest= "filename")
The command line format can have the following form
Copy Code code as follows:
-ffoo
-F foo
--file=foo
--file Foo
After parsing results
Copy Code code as follows:
Parsing (Parse_args ())
Copy Code code as follows:
(options, args) = Parser.parse_args ()
Options parsed parameters, saved in dictionary form
Args arguments that cannot be resolved, saved as a list
Behavior (Action)
Store default behavior, save value to Dest
"Store_const" Save constants
"Append" append this option ' s argument to a list
"Count" increment a counter by one
"Callback" call a specified function
Set defaults (default)
Copy Code code as follows:
Parser.add_option ("-V", action= "Store_true", dest= "verbose", default=true)
Parser.set_defaults (Verbose=true)
Generate Help Tips
The Help option is available and can be printed with parser.print_help ()
Copy Code code as follows:
Parser.add_option ("F", "–file", dest= "filename", help= "write to File", metavar= "file")
set a Boolean value
Support for Store_true and store_false two behaviors
Copy Code code as follows:
Parser.add_option ("-V", action= "Store_true", dest= "verbose")
Parser.add_option ("Q", action= "Store_false", dest= "verbose")
If you encounter-v,verbose=true; if you encounter-q,verbose=false
Error handling
Copy Code code as follows:
(options, args) = Parser.parse_args ()
[...]
If OPTIONS.A and options.b:
Parser.error ("Options-a and-b are mutually exclusive")
option group (Grouping options)
Format is as follows
Class Optparse. OptionGroup (parser, title, Description=none)
Copy Code code as follows:
Group = OptionGroup (parser, "Dangerous Options",
"Caution:use 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)
The results of the prompts are as follows
Copy Code code as follows:
Usage: <yourscript> [options] arg1 arg2
Options:
-H,--help show this Help and exit
-V,--verbose make lots of noise [default]
-Q,--quiet be vewwy quiet (I ' m hunting wabbits)
-F FILE,--filename=file
Write output to FILE
-M MODE,--mode=mode interaction mode:novice, intermediate, or
Expert [Default:intermediate]
Dangerous Options:
Caution:use options at your own risk. It is believed that some
of them bite.
-G Group option.