optparse is a powerful, flexible, extensible, easy-to-use command-line parsing library that is part of the Python standard library. configparse is built on top of optparse and uses the same interface to allow simple parsing of configuration files.
configparse requires Python >= 2.3. Installation is done the standard way:
$ python setup.py build <switch to root> # python setup.py install
It will not be described here how to construct an option parser with a list of options, it is assumed you have basic knowledge on how to do this. If not, it is recommended that you read the documentation for the optparse module before you proceed.
In order to be able to read configfiles, configparse adds a keyword files to the parse_args() method of the OptionParser. The files argument accepts a sequence of filenames or file objects opened for reading. parse_args() will not raise an error if a filename does not refer to an existing or readable file - this allows reading configfiles from a standard set of locations. The values parse_args() returns are collected in the following order:
default values -> config files -> command-line parameters
So, the options passed on the command-line will always supersede values read from configfiles.
configparse discriminates between three different kinds of options: options that will be accepted on the command-line, options that can only be read from configfiles and those that will be accepted on both command-line and from configfiles. This behaviour is controlled using the configparse-specific config keyword together with add_option() or the Option() constructor.
The dest keyword plays an important role, too. Besides defining the option's attribute name in the Values object that is returned by parse_args(), it also provides the name by which the option is referenced in the configfile.
It is also possible to write a representation of the parser's values to a writable file object using its write() method.
In case of errors in configfiles configparse knows three possible reactions which can be set with the error_handler keyword to the OptionParser constructor or with the set_error_handler() method:
There are some constraints involved because the configparse module is built around optparse which is focused on command-line parsing and not processing configfiles:
The config keyword accepts the following values:
The following example illustrates how to use configparse in your own program, in this case called program.py:
import os
from configparse import OptionParser
parser = OptionParser()
# Read this option from both the command line and the config file (config="true").
parser.add_option("-d", "--directory", action="store", type="string",
dest="directory", default="/home/foo", help="...",
config="true")
# Read this option only from the command line (config="false" is omitted in this
# case because it is the default).
parser.add_option("-v", "--verbose", action="store_true", dest="verbose",
help="...", default=False)
# Read this option only from the config file (config="only"). Note that there are no
# option strings allowed. This option will not appear in help output.
parser.add_option(action="store_true", dest="debug", default=False,
help="...", config="only")
opts, args = parser.parse_args(files=[os.path.expanduser("~/.programrc")])
print opts.directory
print opts.verbose
print opts.debug
Call program.py without arguments, it will print the default values:
$ program.py /home/foo False False
Create a file ~/.programrc:
# This is a comment. ; This is a comment, too. directory = "/home/bar" debug = True
Call program.py a few more times:
$ program /home/bar False True $ program.py --directory /home/baz /home/baz False True $ program.py -v /home/bar True True