~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/option.py

Merge bzr.dev, update to use new hooks.

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,
 
336
                        short_name=short_name)
333
337
        self._registry = registry
334
338
        if registry is None:
335
339
            if lazy_registry is None:
344
348
        self.converter = converter
345
349
        self.value_switches = value_switches
346
350
        self.enum_switch = enum_switch
 
351
        self.short_value_switches = short_value_switches
347
352
        self.title = title
348
353
        if self.title is None:
349
354
            self.title = name
387
392
                    help = optparse.SUPPRESS_HELP
388
393
                else:
389
394
                    help = self.registry.get_help(key)
 
395
                if (self.short_value_switches and
 
396
                    key in self.short_value_switches):
 
397
                    option_strings.append('-%s' %
 
398
                                          self.short_value_switches[key])
390
399
                parser.add_option(action='callback',
391
400
                              callback=self._optparse_value_callback(key),
392
401
                                  help=help,
422
431
 
423
432
    DEFAULT_VALUE = object()
424
433
 
 
434
    def __init__(self):
 
435
        optparse.OptionParser.__init__(self)
 
436
        self.formatter = GettextIndentedHelpFormatter()
 
437
 
425
438
    def error(self, message):
426
439
        raise errors.BzrCommandError(message)
427
440
 
428
441
 
 
442
class GettextIndentedHelpFormatter(optparse.IndentedHelpFormatter):
 
443
    """Adds gettext() call to format_option()"""
 
444
    def __init__(self):
 
445
        optparse.IndentedHelpFormatter.__init__(self)
 
446
 
 
447
    def format_option(self, option):
 
448
        """code taken from Python's optparse.py"""
 
449
        if option.help:
 
450
            option.help = i18n.gettext(option.help)
 
451
        return optparse.IndentedHelpFormatter.format_option(self, option)
 
452
 
 
453
 
429
454
def get_optparser(options):
430
455
    """Generate an optparse parser for bzrlib-style options"""
431
456
 
450
475
    Option.STD_OPTIONS[name] = Option(name, **kwargs)
451
476
    Option.OPTIONS[name] = Option.STD_OPTIONS[name]
452
477
 
 
478
def _standard_list_option(name, **kwargs):
 
479
    """Register a standard option."""
 
480
    # All standard options are implicitly 'global' ones
 
481
    Option.STD_OPTIONS[name] = ListOption(name, **kwargs)
 
482
    Option.OPTIONS[name] = Option.STD_OPTIONS[name]
 
483
 
453
484
 
454
485
def _global_option(name, **kwargs):
455
486
    """Register a global option."""
488
519
            _verbosity_level = -1
489
520
 
490
521
 
491
 
class MergeTypeRegistry(_mod_registry.Registry):
492
 
 
493
 
    pass
494
 
 
495
 
 
496
 
_merge_type_registry = MergeTypeRegistry()
497
 
_merge_type_registry.register_lazy('merge3', 'bzrlib.merge', 'Merge3Merger',
498
 
                                   "Native diff3-style merge")
499
 
_merge_type_registry.register_lazy('diff3', 'bzrlib.merge', 'Diff3Merger',
500
 
                                   "Merge using external diff3")
501
 
_merge_type_registry.register_lazy('weave', 'bzrlib.merge', 'WeaveMerger',
502
 
                                   "Weave-based merge")
503
 
_merge_type_registry.register_lazy('lca', 'bzrlib.merge', 'LCAMerger',
504
 
                                   "LCA-newness merge")
505
 
 
506
522
# Declare the standard options
507
523
_standard_option('help', short_name='h',
508
524
                 help='Show help message.')
 
525
_standard_option('quiet', short_name='q',
 
526
                 help="Only display errors and warnings.",
 
527
                 custom_callback=_verbosity_level_callback)
509
528
_standard_option('usage',
510
529
                 help='Show usage message and options.')
511
530
_standard_option('verbose', short_name='v',
512
531
                 help='Display more information.',
513
532
                 custom_callback=_verbosity_level_callback)
514
 
_standard_option('quiet', short_name='q',
515
 
                 help="Only display errors and warnings.",
516
 
                 custom_callback=_verbosity_level_callback)
517
533
 
518
534
# Declare commonly used options
519
 
_global_option('all')
520
 
_global_option('overwrite', help='Ignore differences between branches and '
521
 
               'overwrite unconditionally.')
522
 
_global_option('basis', type=str)
523
 
_global_option('bound')
524
 
_global_option('diff-options', type=str)
 
535
_global_option('change',
 
536
               type=_parse_change_str,
 
537
               short_name='c',
 
538
               param_name='revision',
 
539
               help='Select changes introduced by the specified revision. See also "help revisionspec".')
 
540
_global_option('directory', short_name='d', type=unicode,
 
541
               help='Branch to operate on, instead of working directory.')
525
542
_global_option('file', type=unicode, short_name='F')
526
 
_global_option('force')
527
 
_global_option('format', type=unicode)
528
 
_global_option('forward')
 
543
_global_registry_option('log-format', "Use specified log format.",
 
544
                        lazy_registry=('bzrlib.log', 'log_formatter_registry'),
 
545
                        value_switches=True, title='Log format',
 
546
                        short_value_switches={'short': 'S'})
 
547
_global_registry_option('merge-type', 'Select a particular merge algorithm.',
 
548
                        lazy_registry=('bzrlib.merge', 'merge_type_registry'),
 
549
                        value_switches=True, title='Merge algorithm')
529
550
_global_option('message', type=unicode,
530
551
               short_name='m',
531
552
               help='Message string.')
532
 
_global_option('no-recurse')
533
553
_global_option('null', short_name='0',
534
554
                 help='Use an ASCII NUL (\\0) separator rather than '
535
555
                      'a newline.')
536
 
_global_option('profile',
537
 
               help='Show performance profiling information.')
 
556
_global_option('overwrite', help='Ignore differences between branches and '
 
557
               'overwrite unconditionally.')
 
558
_global_option('remember', help='Remember the specified location as a'
 
559
               ' default.')
 
560
_global_option('reprocess', help='Reprocess to reduce spurious conflicts.')
538
561
_global_option('revision',
539
562
               type=_parse_revision_str,
540
563
               short_name='r',
541
564
               help='See "help revisionspec" for details.')
542
 
_global_option('change',
543
 
               type=_parse_change_str,
544
 
               short_name='c',
545
 
               param_name='revision',
546
 
               help='Select changes introduced by the specified revision. See also "help revisionspec".')
547
565
_global_option('show-ids',
548
566
               help='Show internal object ids.')
549
567
_global_option('timezone',
550
568
               type=str,
551
569
               help='Display timezone as local, original, or utc.')
552
 
_global_option('unbound')
553
 
_global_option('version')
554
 
_global_option('email')
555
 
_global_option('update')
556
 
_global_registry_option('log-format', "Use specified log format.",
557
 
                        lazy_registry=('bzrlib.log', 'log_formatter_registry'),
558
 
                        value_switches=True, title='Log format')
559
 
_global_option('long', help='Use detailed log format. Same as --log-format long',
560
 
               short_name='l')
561
 
_global_option('short', help='Use moderately short log format. Same as --log-format short')
562
 
_global_option('line', help='Use log format with one line per revision. Same as --log-format line')
563
 
_global_option('root', type=str)
564
 
_global_option('no-backup')
565
 
_global_registry_option('merge-type', 'Select a particular merge algorithm.',
566
 
                        _merge_type_registry, value_switches=True,
567
 
                        title='Merge algorithm')
568
 
_global_option('pattern', type=str)
569
 
_global_option('remember', help='Remember the specified location as a'
570
 
               ' default.')
571
 
_global_option('reprocess', help='Reprocess to reduce spurious conflicts.')
572
 
_global_option('kind', type=str)
573
 
_global_option('dry-run',
574
 
               help="Show what would be done, but don't actually do anything.")
575
 
_global_option('name-from-revision', help='The path name in the old tree.')
576
 
_global_option('directory', short_name='d', type=unicode,
577
 
               help='Branch to operate on, instead of working directory')
578
570
 
579
571
diff_writer_registry = _mod_registry.Registry()
580
572
diff_writer_registry.register('plain', lambda x: x, 'Plaintext diff output.')