fromOptparseImportOptionparser 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='Do not print status messages to stdout') (options, args)=Parser.parse_args () Note: You can also pass a command-line argument list to Parse_args () if not, the default is to use sys.argv[1:]parse_args () returns the two values options are object Optpars. Values are saved with a command-line parameter value, as long as you know the command-line parameter name, such as file can access its corresponding value Options.fileargs a list of positional arguments action is 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, by default, store, which means that the command line parameter values are saved in the Options object. Example Parser.add_option ('- F','--file', action='Store', type='string', dest='filename') args= ['- F','foo.txt'] (options, args)=Parser.parse_args (args)PrintOptions.filename will eventually print out foo.txt when optparse resolves to-F, will continue to parse the following foo.txt, and then save Foo.txt to Options.filename when Parser.args () is called, Options.filename value is foo.txt You can also specify ADD_OP The type parameter in the tion () method is a different value, such as int or float, and so on Parser.add_option ('- N', type='int', dest='Num'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, that are used to handle cases with no values after the command line arguments. As-V,-Q and other command line parameters: Parser.add_option ('- v', action='store_true', dest='verbose') parser.add_option ('- Q', action='Store_false', dest='verbose'in this case, when the command line resolves to the-V Options.verbose will be given a True value, instead, parse to-Q will be given a value of False the other actions values also have the Store_const, append, Count, Callback Parse_args () methods provide a default parameter for setting defaults parser.add_op tion ('- F','--file', action='Store', dest='filename', default='foo.txt') parser.add_option ('- v', action='store_true', dest='verbose', default=True) or use Set_defaults () parser.set_defaults (filename='foo.txt', verbose=True) parser.add_option (...) (options, args)=The Parser.parse_args () Build program helps optparse another handy feature that is automatically generated by the program's help information. You only need to specify the Metavar parameter in the Add_option method for the help parameter of the Add_option () method, which helps to remind the user of the parameters that the command-line arguments expect, such as Metavar='Mode'Note: The string in the Metavar parameter is automatically capitalized in the Help parameter%default can insert the default value for this command line parameter
For more information, refer to http://www.cnblogs.com/captain_jack/archive/2011/01/11/1933366.html
Python command line parameter processing module Optparse use reference