Optparse is a module that is specifically designed to add options at the command line.
First look at a sample code
From Optparse import Optionparser Msg_usage = "myprog[-f][-s] arg1[,arg2 ...]" Optparser = Optionparser (msg_usage) Optparser.add_option ("-F", "--file", action = "store", type= "string", Dest = "FileName") Ooptparser.add_option ("-V", "--vison", action= "Store_false", dest= "verbose", default= ' gggggg ', 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.filename Print Options.verbose Print options Print args Print Optparser.print_help () |
|
Enter the result as
File.txt False {' Verbose ': False, ' fileName ': ' File.txt '} [' This was some what ', ' arg2 ', ' arge '] Usage:myprog[-F][-s] arg1[,arg2 ...] Options: -H,--help show this help message and exit -F FILENAME,--file=filename -V,--vison make lots of noise [default] |
Basic use Steps
1, produce a Optionparser object optparse. The value passed in Msg_usage can be displayed when the Print command is called.
Msg_usage = "myprog[-f][-s] arg1[,arg2 ...]" Optparser = Optionparser (msg_usage) |
2. Call Optionparser.add_option () to add options
Optparser.add_option ("-F", "--file", action = "Store", type = "string", Dest = "FileName") Optparser.add_option ("-V", "--vison", action= "Store_false", dest= "verbose", default= ' gggggg ', Help= "Make lots of noise [default]") |
Add_option () parameter description:
Action: Storage method, divided into three kinds of store, Store_false, store_true
Type: Types (I don't know what kind of type)
Dest: Stored variables
Default: Defaults
Help: Helpful Information
3. Call Optionparser.parse_args () to parse and return a directory and a list.
Fakeargs = ['-F ', ' file.txt ', '-V ', ' Good luck to you ', ' arg2 ', ' arge '] Options, args = Optparser.parse_args (Fakeargs) Print Options.filename Print Options.verbose Print options Print args |
Output results
File.txt False {' Verbose ': False, ' fileName ': ' File.txt '} [' This was some what ', ' arg2 ', ' arge '] |
Parse_args () Description:
If there is no incoming join, Parse_args defaults to the value of sys.argv[1:] as the default parameter. Here we will fakeargs the value of the analog input.
As you can see from the returned results,
L options is a directory whose contents are Fakeargs as "parameter/value" key-value pairs.
L args is a list whose contents are the remaining input after the Fakeargs is removed from the options.
L Options.version and Options.filename are taken to the directory values in the options.
4. Call OptionParser.optParser.print_help () to output help information
Show returned results
Usage:myprog[-F][-s] arg1[,arg2 ...] Options: -H,--help show this help message and exit -F FILENAME,--file=filename -V,--vison make lots of noise [default] |
Optparser.print_help () Description:
1. The first Msg_usage value: shown in this place.
2. Automatically adds the-H parameter.
Note: If you use%prog in Msg_usage, it will be automatically resolved to sys.args[0] that is the file name. If it is, msg_usage = "%prog [options] arg1 arg2", if the file name is Filexx, then it appears in Help
The message is "filexx[options] arg1 arg2".
In-depth analysis
Optionparser.add_option ()
Example: Optparser.add_option ("-V", "--vison", action= "Store_false", dest= "verbose", default= ' gggggg ',
Help= "Make lots of noise [default]")
Parameter action:
Storage method, divided into three kinds of store, Store_false, Store_true.
The following are three ways to explain:
The first type: action = "Store"
1. If "-V" is present in the input parameter Fakeargs, the value returned by verbose is the number immediately following '-V ' in Fakeargs, which is "good luck to You". This is exactly the key value corresponding to the options, and the remaining paired parameters are passed to args. Please see the following code
Optparser.add_option ("-F", "--file", action = "Store", type = "string", Dest = "FileName") Optparser.add_option ("-V", "--vison", action= "store", dest= "verbose") Fakeargs = ['-F ', ' file.txt ', '-V ', ' Good luck to you ', ' arg2 ', ' arge '] Options, args = Optparser.parse_args (Fakeargs) Print Optparse.verbose Print options Print args |
Input results
Good luck to you {' Verbose ': ' Good luck to you ', ' fileName ': ' File.txt '} [' arg2 ', ' arge '] |
2. If "-V" is not present in the input parameter Fakeargs, the return value of verbose is none.
Example code:
Optparser.add_option ("-F", "--file", action = "Store", type = "string", Dest = "FileName") Optparser.add_option ("-V", "--vison", action= "store", dest= "verbose") Fakeargs = ['-F ', ' file.txt ', ' Good luck to you ', ' arg2 ', ' arge '] Options, args = Optparser.parse_args (Fakeargs) Print Optparse.verbose Print options Print args |
Output results
None {' Verbose ': None, ' fileName ': ' File.txt '} [' Good luck to you ', ' arg2 ', ' arge '] |
The second type: action = "Store_true"
1. "-V" exists in Fakeargs, verbose will return true instead of "Good luck to You". Meaning the value of verbose with '-V '
The latter is irrelevant, only with the '-V ' deposit does not exist on the off.
Sample code
Optparser.add_option ("-F", "--file", action = "Store", type = "string", Dest = "FileName") Optparser.add_option ("-V", "--vison", action= "Store_true", dest= "verbose") Fakeargs = ['-F ', ' file.txt ', '-V ', ' Good luck to you ', ' arg2 ', ' arge '] Options, args = Optparser.parse_args (Fakeargs) Print Optparse.verbose Print options Print args |
Output results
True {' Verbose ': True, ' fileName ': ' File.txt '} [' Good luck to you ', ' arg2 ', ' arge '] |
2, Fakeargs does not exist in '-V ', verbose also return empty (I will not run the code).
The third type: action= "Store_false"
This is similar to action= "Store_true", where only the parameter '-V ' exists, the value of verbose is false, and if '-V ' does not exist, then the value of verbose is none.
Parameters: Default
Optparser.add_option ("-V", "--vison", action= "Store_false", dest= "verbose", default= ' GGGGGG ')
Setting some parameters is the return value used to return the verbose.
If action= "Store", default= ' GGGGGG ', the code is as follows.
Optparser.add_option ("-V", "--vison", action= "Store_false", dest= "verbose", default= ' GGGGGG ')
Fakeargs = ['-F ', ' file.txt ', '-V ', ' Good luck to you ', ' arg2 ', ' arge ']
If '-V ' is present in Fakeargs, the return value is, "Good luck to You"
If '-V ' is not present, the return value is, "GGGGGG"
If action = "Store_true", default= ' GGGGGG ', the code is as follows.
Optparser.add_option ("-V", "--vison", action= "Store_true", dest= "verbose", default= ' GGGGGG ')
If '-V ' is present in Fakeargs, the return value is true.
If '-V ' is not present in Fakeargs, the return value is None
Once again, if action= "Store_true", the value of verbose is only related to whether '-V '. Does it also indicate that the priority of Action_true is higher than default.
Note: action= "Store_false" functions like this, returning to false or none. Once again, it proves
Parameters: Help
Optparser.add_option ("-F", "--file", action = "Store", type = "string", Dest = "FileName")
Optparser.add_option ("-V", "--vison", action= "store", dest= "verbose", default= ' gggggg ',
Help= "Make lots of noise [default]")
Primarily used to display Help information, use OPTPARSER.PRINT_HELP () to display the Help bar.
When action= "restore", the "-F" with the help parameter was not used and '-V ', which was used for the "-a", with a single line of helpful information.
Usage:myprog[-F][-s] arg1[,arg2 ...] Options: -H,--help show this help message and exit -F FILENAME,--file=filename -V VERBOSE,--vison=verbose Make lots of noise [default] |
When action= "Restore_false".
Optparser.add_option ("-F", "--file", action = "Store", type = "string", Dest = "FileName")
Optparser.add_option ("-V", "--vison", action= "store", dest= "verbose", default= ' gggggg ',
Help= "Make lots of noise [default]")
The output of the two comparisons is as follows
Usage:myprog[-F][-s] arg1[,arg2 ...] Options: -H,--help show this help message and exit -F FILENAME,--file=filename -V,--vison make lots of noise [default] |
Parameter: Type
The action= "Store_false" and the action= "Store_true" will not be available if you type= "string" When you know it without careful testing. It is not known whether the type can be interpreted as a return value type of verbose.
A description of the input parameter Fakeargs
or using the previous code to analyze
From Optparse import Optionparser Msg_usage = "myprog[-f][-s] arg1[,arg2 ...]" Optparser = Optionparser (msg_usage) Optparser.add_option ("-F", "--file", action = "store", type= "string", Dest = "FileName") Optparser.add_option ("-V", "--vison", action= "store", dest= "verbose", default= ' gggggg ', 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 Print args |
The values in the Fakeargs are paired with the front and back two values for each option '-V ', '-f '.
1. Normal situation:
The results are as follows
The options value is: {' verbose ': ' Good luck to you ', ' fileName ': ' File.txt '} The value of args is: [' arg2 ', ' arge '] |
2, abnormal situation:
If there are two consecutive options '-f ', '-V '.
Fakeargs = ['-F ', '-V ', ' Good luck to you ', ' arg2 ', ' arge ']
'-V ' is passed as a value to filename.
But verbose returns the default value of ' GGGGGG ', which will return none if not set. In other words, there is no detection of the parameter '-V ' existence, which once again illustrates the idea of pairing the key values in the Fakeargs. The previous number as the option, and the latter as the value.
The results are as follows:
The options value is: {' verbose ': ' gggggg ', ' fileName ': '-V '} The value of args is: [' Good luck to you ', ' arg2 ', ' arge '] |
3, if more than one ' x ' is not defined, the program will error.
Fakeargs = ['-X ', '-f ', ' file.txt ', '-V ', ' Good luck to you ', ' arg2 ', ' arge ']
Well, the module on Optparse only learns these things.
The optparse of Python basics. Optionparser