237
237
:param converter: Callable to invoke with the value name to produce
238
238
the value. If not supplied, self.registry.get is used.
239
239
:param value_switches: If true, each possible value is assigned its
240
own switch. For example, instead of '--format metaweave',
241
'--metaweave' can be used interchangeably.
240
own switch. For example, instead of '--format knit',
241
'--knit' can be used interchangeably.
242
:param enum_switch: If true, a switch is provided with the option name,
243
245
Option.__init__(self, name, help, type=self.convert)
244
246
self.registry = registry
246
248
self.converter = converter
247
249
self.value_switches = value_switches
250
self.enum_switch = enum_switch
248
251
self.title = title
249
252
if self.title is None:
250
253
self.title = name
256
def from_kwargs(name_, help=None, title=None, value_switches=False,
257
enum_switch=True, **kwargs):
258
"""Convenience method to generate string-map registry options
260
name, help, value_switches and enum_switch are passed to the
261
RegistryOption constructor. Any other keyword arguments are treated
262
as values for the option, and they value is treated as the help.
264
reg = registry.Registry()
265
for name, help in kwargs.iteritems():
266
name = name.replace('_', '-')
267
reg.register(name, name, help=help)
268
return RegistryOption(name_, help, reg, title=title,
269
value_switches=value_switches, enum_switch=enum_switch)
252
271
def add_option(self, parser, short_name):
253
272
"""Add this option to an Optparse parser"""
254
273
if self.value_switches:
255
274
parser = parser.add_option_group(self.title)
256
Option.add_option(self, parser, short_name)
276
Option.add_option(self, parser, short_name)
257
277
if self.value_switches:
258
278
for key in self.registry.keys():
259
279
option_strings = ['--%s' % key]
280
if getattr(self.registry.get_info(key), 'hidden', False):
281
help = optparse.SUPPRESS_HELP
283
help = self.registry.get_help(key)
260
284
parser.add_option(action='callback',
261
285
callback=self._optparse_value_callback(key),
262
help=self.registry.get_help(key),
265
289
def _optparse_value_callback(self, cb_value):