~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/option.py

  • Committer: Jonathan Riddell
  • Date: 2011-09-20 11:01:37 UTC
  • mto: This revision was merged to the branch mainline in revision 6153.
  • Revision ID: jriddell@canonical.com-20110920110137-mrps089ulocx1skd
gettext() in plugin.py and hooks.py

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
from bzrlib import (
26
26
    errors,
27
27
    revisionspec,
 
28
    i18n,
28
29
    )
29
30
""")
30
31
 
312
313
 
313
314
    def __init__(self, name, help, registry=None, converter=None,
314
315
        value_switches=False, title=None, enum_switch=True,
315
 
        lazy_registry=None):
 
316
        lazy_registry=None, short_name=None, short_value_switches=None):
316
317
        """
317
318
        Constructor.
318
319
 
328
329
            which takes a value.
329
330
        :param lazy_registry: A tuple of (module name, attribute name) for a
330
331
            registry to be lazily loaded.
 
332
        :param short_name: The short name for the enum switch, if any
 
333
        :param short_value_switches: A dict mapping values to short names
331
334
        """
332
 
        Option.__init__(self, name, help, type=self.convert)
 
335
        Option.__init__(self, name, help, type=self.convert, short_name=short_name)
333
336
        self._registry = registry
334
337
        if registry is None:
335
338
            if lazy_registry is None:
344
347
        self.converter = converter
345
348
        self.value_switches = value_switches
346
349
        self.enum_switch = enum_switch
 
350
        self.short_value_switches = short_value_switches
347
351
        self.title = title
348
352
        if self.title is None:
349
353
            self.title = name
387
391
                    help = optparse.SUPPRESS_HELP
388
392
                else:
389
393
                    help = self.registry.get_help(key)
 
394
                if (self.short_value_switches and
 
395
                    key in self.short_value_switches):
 
396
                    option_strings.append('-%s' %
 
397
                                          self.short_value_switches[key])
390
398
                parser.add_option(action='callback',
391
399
                              callback=self._optparse_value_callback(key),
392
400
                                  help=help,
422
430
 
423
431
    DEFAULT_VALUE = object()
424
432
 
 
433
    def __init__(self):
 
434
        optparse.OptionParser.__init__(self)
 
435
        self.formatter = GettextIndentedHelpFormatter()
 
436
 
425
437
    def error(self, message):
426
438
        raise errors.BzrCommandError(message)
427
439
 
 
440
class GettextIndentedHelpFormatter(optparse.IndentedHelpFormatter):
 
441
    """Adds gettext() call to format_option()"""
 
442
    def __init__(self):
 
443
        optparse.IndentedHelpFormatter.__init__(self)
 
444
 
 
445
    def format_option(self, option):
 
446
        """code taken from Python's optparse.py"""
 
447
        if option.help:
 
448
            option.help = i18n.gettext(option.help)
 
449
        return optparse.IndentedHelpFormatter.format_option(self, option)
428
450
 
429
451
def get_optparser(options):
430
452
    """Generate an optparse parser for bzrlib-style options"""
530
552
               short_name='m',
531
553
               help='Message string.')
532
554
_global_option('no-recurse')
 
555
_global_option('null', short_name='0',
 
556
                 help='Use an ASCII NUL (\\0) separator rather than '
 
557
                      'a newline.')
533
558
_global_option('profile',
534
559
               help='Show performance profiling information.')
535
560
_global_option('revision',
552
577
_global_option('update')
553
578
_global_registry_option('log-format', "Use specified log format.",
554
579
                        lazy_registry=('bzrlib.log', 'log_formatter_registry'),
555
 
                        value_switches=True, title='Log format')
 
580
                        value_switches=True, title='Log format',
 
581
                        short_value_switches={'short': 'S'})
556
582
_global_option('long', help='Use detailed log format. Same as --log-format long',
557
583
               short_name='l')
558
584
_global_option('short', help='Use moderately short log format. Same as --log-format short')
570
596
_global_option('dry-run',
571
597
               help="Show what would be done, but don't actually do anything.")
572
598
_global_option('name-from-revision', help='The path name in the old tree.')
 
599
_global_option('directory', short_name='d', type=unicode,
 
600
               help='Branch to operate on, instead of working directory')
573
601
 
574
602
diff_writer_registry = _mod_registry.Registry()
575
603
diff_writer_registry.register('plain', lambda x: x, 'Plaintext diff output.')