/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

  • Committer: Robert Collins
  • Date: 2010-05-06 11:08:10 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506110810-h3j07fh5gmw54s25
Cleaner matcher matching revised unlocking protocol.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
# TODO: For things like --diff-prefix, we want a way to customize the display
18
18
# of the option argument.
19
19
 
20
 
from __future__ import absolute_import
21
 
 
22
20
import optparse
23
21
import re
24
22
 
25
 
from . import (
 
23
from bzrlib.lazy_import import lazy_import
 
24
lazy_import(globals(), """
 
25
from bzrlib import (
26
26
    errors,
 
27
    revisionspec,
 
28
    )
 
29
""")
 
30
 
 
31
from bzrlib import (
27
32
    registry as _mod_registry,
28
 
    revisionspec,
29
 
    )
30
 
from .sixish import (
31
 
    text_type,
32
 
    viewitems,
33
 
    )
34
 
 
35
 
 
36
 
class BadOptionValue(errors.BzrError):
37
 
 
38
 
    _fmt = """Bad value "%(value)s" for option "%(name)s"."""
39
 
 
40
 
    def __init__(self, name, value):
41
 
        errors.BzrError.__init__(self, name=name, value=value)
 
33
    )
42
34
 
43
35
 
44
36
def _parse_revision_str(revstr):
131
123
        return merge_types[typestring][0]
132
124
    except KeyError:
133
125
        templ = '%s%%7s: %%s' % (' '*12)
134
 
        lines = [templ % (f[0], f[1][1]) for f in merge_types.items()]
 
126
        lines = [templ % (f[0], f[1][1]) for f in merge_types.iteritems()]
135
127
        type_list = '\n'.join(lines)
136
128
        msg = "No known merge type %s. Supported types are:\n%s" %\
137
129
            (typestring, type_list)
308
300
    def validate_value(self, value):
309
301
        """Validate a value name"""
310
302
        if value not in self.registry:
311
 
            raise BadOptionValue(self.name, value)
 
303
            raise errors.BadOptionValue(self.name, value)
312
304
 
313
305
    def convert(self, value):
314
306
        """Convert a value name into an output type"""
320
312
 
321
313
    def __init__(self, name, help, registry=None, converter=None,
322
314
        value_switches=False, title=None, enum_switch=True,
323
 
        lazy_registry=None, short_name=None, short_value_switches=None):
 
315
        lazy_registry=None):
324
316
        """
325
317
        Constructor.
326
318
 
336
328
            which takes a value.
337
329
        :param lazy_registry: A tuple of (module name, attribute name) for a
338
330
            registry to be lazily loaded.
339
 
        :param short_name: The short name for the enum switch, if any
340
 
        :param short_value_switches: A dict mapping values to short names
341
331
        """
342
 
        Option.__init__(self, name, help, type=self.convert,
343
 
                        short_name=short_name)
 
332
        Option.__init__(self, name, help, type=self.convert)
344
333
        self._registry = registry
345
334
        if registry is None:
346
335
            if lazy_registry is None:
355
344
        self.converter = converter
356
345
        self.value_switches = value_switches
357
346
        self.enum_switch = enum_switch
358
 
        self.short_value_switches = short_value_switches
359
347
        self.title = title
360
348
        if self.title is None:
361
349
            self.title = name
393
381
        if self.enum_switch:
394
382
            Option.add_option(self, parser, short_name)
395
383
        if self.value_switches:
396
 
            alias_map = self.registry.alias_map()
397
384
            for key in self.registry.keys():
398
 
                if key in self.registry.aliases():
399
 
                    continue
400
 
                option_strings = [
401
 
                    ('--%s' % name)
402
 
                    for name in [key] +
403
 
                    [alias for alias in alias_map.get(key, [])
404
 
                        if not self.is_hidden(alias)]]
 
385
                option_strings = ['--%s' % key]
405
386
                if self.is_hidden(key):
406
387
                    help = optparse.SUPPRESS_HELP
407
388
                else:
408
389
                    help = self.registry.get_help(key)
409
 
                if (self.short_value_switches and
410
 
                    key in self.short_value_switches):
411
 
                    option_strings.append('-%s' %
412
 
                                          self.short_value_switches[key])
413
390
                parser.add_option(action='callback',
414
391
                              callback=self._optparse_value_callback(key),
415
392
                                  help=help,
434
411
            for key in sorted(self.registry.keys()):
435
412
                yield key, None, None, self.registry.get_help(key)
436
413
 
437
 
    def is_alias(self, name):
438
 
        """Check whether a particular format is an alias."""
439
 
        if name == self.name:
440
 
            return False
441
 
        return name in self.registry.aliases()
442
 
 
443
414
    def is_hidden(self, name):
444
415
        if name == self.name:
445
416
            return Option.is_hidden(self, name)
451
422
 
452
423
    DEFAULT_VALUE = object()
453
424
 
454
 
    def __init__(self):
455
 
        optparse.OptionParser.__init__(self)
456
 
        self.formatter = GettextIndentedHelpFormatter()
457
 
 
458
425
    def error(self, message):
459
426
        raise errors.BzrCommandError(message)
460
427
 
461
428
 
462
 
class GettextIndentedHelpFormatter(optparse.IndentedHelpFormatter):
463
 
    """Adds gettext() call to format_option()"""
464
 
    def __init__(self):
465
 
        optparse.IndentedHelpFormatter.__init__(self)
466
 
 
467
 
    def format_option(self, option):
468
 
        """code taken from Python's optparse.py"""
469
 
        if option.help:
470
 
            from .i18n import gettext
471
 
            option.help = gettext(option.help)
472
 
        return optparse.IndentedHelpFormatter.format_option(self, option)
473
 
 
474
 
 
475
429
def get_optparser(options):
476
 
    """Generate an optparse parser for breezy-style options"""
 
430
    """Generate an optparse parser for bzrlib-style options"""
477
431
 
478
432
    parser = OptionParser()
479
433
    parser.remove_option('--help')
480
 
    for option in options.values():
 
434
    for option in options.itervalues():
481
435
        option.add_option(parser, option.short_name())
482
436
    return parser
483
437
 
496
450
    Option.STD_OPTIONS[name] = Option(name, **kwargs)
497
451
    Option.OPTIONS[name] = Option.STD_OPTIONS[name]
498
452
 
499
 
def _standard_list_option(name, **kwargs):
500
 
    """Register a standard option."""
501
 
    # All standard options are implicitly 'global' ones
502
 
    Option.STD_OPTIONS[name] = ListOption(name, **kwargs)
503
 
    Option.OPTIONS[name] = Option.STD_OPTIONS[name]
504
 
 
505
453
 
506
454
def _global_option(name, **kwargs):
507
455
    """Register a global option."""
540
488
            _verbosity_level = -1
541
489
 
542
490
 
 
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
 
543
506
# Declare the standard options
544
507
_standard_option('help', short_name='h',
545
508
                 help='Show help message.')
546
 
_standard_option('quiet', short_name='q',
547
 
                 help="Only display errors and warnings.",
548
 
                 custom_callback=_verbosity_level_callback)
549
509
_standard_option('usage',
550
510
                 help='Show usage message and options.')
551
511
_standard_option('verbose', short_name='v',
552
512
                 help='Display more information.',
553
513
                 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)
554
517
 
555
518
# Declare commonly used options
556
 
_global_option('change',
557
 
               type=_parse_change_str,
558
 
               short_name='c',
559
 
               param_name='revision',
560
 
               help='Select changes introduced by the specified revision. See also "help revisionspec".')
561
 
_global_option('directory', short_name='d', type=text_type,
562
 
               help='Branch to operate on, instead of working directory.')
563
 
_global_option('file', type=text_type, short_name='F')
564
 
_global_registry_option('log-format', "Use specified log format.",
565
 
                        lazy_registry=('breezy.log', 'log_formatter_registry'),
566
 
                        value_switches=True, title='Log format',
567
 
                        short_value_switches={'short': 'S'})
568
 
_global_registry_option('merge-type', 'Select a particular merge algorithm.',
569
 
                        lazy_registry=('breezy.merge', 'merge_type_registry'),
570
 
                        value_switches=True, title='Merge algorithm')
571
 
_global_option('message', type=text_type,
 
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)
 
525
_global_option('file', type=unicode, short_name='F')
 
526
_global_option('force')
 
527
_global_option('format', type=unicode)
 
528
_global_option('forward')
 
529
_global_option('message', type=unicode,
572
530
               short_name='m',
573
531
               help='Message string.')
574
 
_global_option('null', short_name='0',
575
 
                 help='Use an ASCII NUL (\\0) separator rather than '
576
 
                      'a newline.')
577
 
_global_option('overwrite', help='Ignore differences between branches and '
578
 
               'overwrite unconditionally.')
579
 
_global_option('remember', help='Remember the specified location as a'
580
 
               ' default.')
581
 
_global_option('reprocess', help='Reprocess to reduce spurious conflicts.')
 
532
_global_option('no-recurse')
 
533
_global_option('profile',
 
534
               help='Show performance profiling information.')
582
535
_global_option('revision',
583
536
               type=_parse_revision_str,
584
537
               short_name='r',
585
538
               help='See "help revisionspec" for details.')
 
539
_global_option('change',
 
540
               type=_parse_change_str,
 
541
               short_name='c',
 
542
               param_name='revision',
 
543
               help='Select changes introduced by the specified revision. See also "help revisionspec".')
586
544
_global_option('show-ids',
587
545
               help='Show internal object ids.')
588
546
_global_option('timezone',
589
547
               type=str,
590
548
               help='Display timezone as local, original, or utc.')
 
549
_global_option('unbound')
 
550
_global_option('version')
 
551
_global_option('email')
 
552
_global_option('update')
 
553
_global_registry_option('log-format', "Use specified log format.",
 
554
                        lazy_registry=('bzrlib.log', 'log_formatter_registry'),
 
555
                        value_switches=True, title='Log format')
 
556
_global_option('long', help='Use detailed log format. Same as --log-format long',
 
557
               short_name='l')
 
558
_global_option('short', help='Use moderately short log format. Same as --log-format short')
 
559
_global_option('line', help='Use log format with one line per revision. Same as --log-format line')
 
560
_global_option('root', type=str)
 
561
_global_option('no-backup')
 
562
_global_registry_option('merge-type', 'Select a particular merge algorithm.',
 
563
                        _merge_type_registry, value_switches=True,
 
564
                        title='Merge algorithm')
 
565
_global_option('pattern', type=str)
 
566
_global_option('remember', help='Remember the specified location as a'
 
567
               ' default.')
 
568
_global_option('reprocess', help='Reprocess to reduce spurious conflicts.')
 
569
_global_option('kind', type=str)
 
570
_global_option('dry-run',
 
571
               help="Show what would be done, but don't actually do anything.")
 
572
_global_option('name-from-revision', help='The path name in the old tree.')
591
573
 
592
574
diff_writer_registry = _mod_registry.Registry()
593
575
diff_writer_registry.register('plain', lambda x: x, 'Plaintext diff output.')