/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/option.py

Add test for exporting command help.
It checks ":Usage:" is not exported.

Show diffs side-by-side

added added

removed removed

Lines of Context:
203
203
        else:
204
204
            return 'no-' + self.name
205
205
 
206
 
    def add_option(self, parser, short_name):
 
206
    def get_gettext(self, l10n):
 
207
        """Returns the gettext function used to translate this Option's help.
 
208
 
 
209
        NOTE: Options provided by third party plugins should override this to
 
210
        use own i18n system.
 
211
        """
 
212
        if l10n:
 
213
            from bzrlib.i18n import gettext
 
214
            return gettext
 
215
        return lambda x: x
 
216
 
 
217
    def add_option(self, parser, short_name, l10n=False):
207
218
        """Add this option to an Optparse parser"""
 
219
        gettext = self.get_gettext(l10n)
208
220
        option_strings = ['--%s' % self.name]
209
221
        if short_name is not None:
210
222
            option_strings.append('-%s' % short_name)
211
223
        if self.hidden:
212
224
            help = optparse.SUPPRESS_HELP
213
225
        else:
214
 
            help = self.help
 
226
            help = gettext(self.help)
215
227
        optargfn = self.type
216
228
        if optargfn is None:
217
229
            parser.add_option(action='callback',
268
280
    sets the value of the 'foo' option to ['c'].
269
281
    """
270
282
 
271
 
    def add_option(self, parser, short_name):
 
283
    def add_option(self, parser, short_name, l10n=False):
272
284
        """Add this option to an Optparse parser."""
 
285
        gettext = self.get_gettext(l10n)
273
286
        option_strings = ['--%s' % self.name]
274
287
        if short_name is not None:
275
288
            option_strings.append('-%s' % short_name)
276
289
        parser.add_option(action='callback',
277
290
                          callback=self._optparse_callback,
278
291
                          type='string', metavar=self.argname.upper(),
279
 
                          help=self.help, dest=self._param_name, default=[],
280
 
                          *option_strings)
 
292
                          help=gettext(self.help), dest=self._param_name,
 
293
                          default=[], *option_strings)
281
294
 
282
295
    def _optparse_callback(self, option, opt, value, parser):
283
296
        values = getattr(parser.values, self._param_name)
312
325
 
313
326
    def __init__(self, name, help, registry=None, converter=None,
314
327
        value_switches=False, title=None, enum_switch=True,
315
 
        lazy_registry=None):
 
328
        lazy_registry=None, short_name=None):
316
329
        """
317
330
        Constructor.
318
331
 
329
342
        :param lazy_registry: A tuple of (module name, attribute name) for a
330
343
            registry to be lazily loaded.
331
344
        """
332
 
        Option.__init__(self, name, help, type=self.convert)
 
345
        Option.__init__(self, name, help, type=self.convert, short_name=short_name)
333
346
        self._registry = registry
334
347
        if registry is None:
335
348
            if lazy_registry is None:
374
387
        return RegistryOption(name_, help, reg, title=title,
375
388
            value_switches=value_switches, enum_switch=enum_switch)
376
389
 
377
 
    def add_option(self, parser, short_name):
 
390
    def add_option(self, parser, short_name, l10n=False):
378
391
        """Add this option to an Optparse parser"""
 
392
        gettext = self.get_gettext(l10n)
379
393
        if self.value_switches:
380
394
            parser = parser.add_option_group(self.title)
381
395
        if self.enum_switch:
386
400
                if self.is_hidden(key):
387
401
                    help = optparse.SUPPRESS_HELP
388
402
                else:
389
 
                    help = self.registry.get_help(key)
 
403
                    help = gettext(self.registry.get_help(key))
390
404
                parser.add_option(action='callback',
391
405
                              callback=self._optparse_value_callback(key),
392
406
                                  help=help,
426
440
        raise errors.BzrCommandError(message)
427
441
 
428
442
 
429
 
def get_optparser(options):
 
443
def get_optparser(options, l10n=False):
430
444
    """Generate an optparse parser for bzrlib-style options"""
431
445
 
432
446
    parser = OptionParser()
433
447
    parser.remove_option('--help')
434
448
    for option in options.itervalues():
435
 
        option.add_option(parser, option.short_name())
 
449
        option.add_option(parser, option.short_name(), l10n)
436
450
    return parser
437
451
 
438
452
 
530
544
               short_name='m',
531
545
               help='Message string.')
532
546
_global_option('no-recurse')
 
547
_global_option('null', short_name='0',
 
548
                 help='Use an ASCII NUL (\\0) separator rather than '
 
549
                      'a newline.')
533
550
_global_option('profile',
534
551
               help='Show performance profiling information.')
535
552
_global_option('revision',
570
587
_global_option('dry-run',
571
588
               help="Show what would be done, but don't actually do anything.")
572
589
_global_option('name-from-revision', help='The path name in the old tree.')
 
590
_global_option('directory', short_name='d', type=unicode,
 
591
               help='Branch to operate on, instead of working directory')
573
592
 
574
593
diff_writer_registry = _mod_registry.Registry()
575
594
diff_writer_registry.register('plain', lambda x: x, 'Plaintext diff output.')