The add_optparse_keys_option()
method
is used to set up the cooperation between the command line and configuration
file parsers to allow the user to specify a keys list from the command line
to control the keys used to obtain option settings from the configuration file.
For more information on how command line keys are used to obtain option settings
see "Keys" (section 6).
option_group [, switches] [, dest] [, help] ) |
option_group is a required positional argument and must be set to
an instance of the optparse module's OptionParser
class or an option group of that class. The OptionParser
instance must also be passed into the parse()
method.
switches is an optional keyword argument and is used to set
the command line switches the user can use to specify section keys
to be used by the parser for finding option settings.
switches must be a tuple and defaults to
('-k','--keys',)
when switches is omitted.
dest is an optional string keyword argument and is used set the
name of the destination attribute of the options object returned
by the command line option parser. When omitted, dest defaults
to 'cfgparse_keys'
.
help is an optional string keyword argument and is used to set the command line switch help string. When omitted a reasonable default help string is utilized.
For example:
# file: coop_keys.ini [DEV0] # path to device #0 path = 192.168.0.0 [DEV1] # path to device #1 path = 192.168.0.1
And script:
# file: coop_keys.py import optparse import cfgparse o = optparse.OptionParser() c = cfgparse.ConfigParser() c.add_optparse_keys_option(o) c.add_option('path') c.add_file('coop_keys.ini') (opts,args) = c.parse(o) print "Path:",opts.path
Results in:
$ python coop_keys.py ERROR: Configuration File Parser Option: path No valid default found. keys=DEFAULT $ python coop_keys.py --keys=DEV0 Path: 192.168.0.0