~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/option.py

  • Committer: Andrew Bennetts
  • Date: 2007-03-28 07:08:42 UTC
  • mfrom: (2380 +trunk)
  • mto: (2018.5.146 hpss)
  • mto: This revision was merged to the branch mainline in revision 2414.
  • Revision ID: andrew.bennetts@canonical.com-20070328070842-r843houy668oxb9o
Merge from bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
227
227
            return self.converter(value)
228
228
 
229
229
    def __init__(self, name, help, registry, converter=None,
230
 
        value_switches=False, title=None):
 
230
        value_switches=False, title=None, enum_switch=True):
231
231
        """
232
232
        Constructor.
233
233
 
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
            which takes a value.
242
244
        """
243
245
        Option.__init__(self, name, help, type=self.convert)
244
246
        self.registry = registry
245
247
        self.name = name
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
251
254
 
 
255
    @staticmethod
 
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
 
259
 
 
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.
 
263
        """
 
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)
 
270
 
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)
 
275
        if self.enum_switch:
 
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
 
282
                else:
 
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),
 
286
                                  help=help,
263
287
                                  *option_strings)
264
288
 
265
289
    def _optparse_value_callback(self, cb_value):