/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 breezy/option.py

  • Committer: Jelmer Vernooij
  • Date: 2017-10-27 00:18:42 UTC
  • mto: This revision was merged to the branch mainline in revision 6799.
  • Revision ID: jelmer@jelmer.uk-20171027001842-o77sekj0g2t2zwbn
Properly escape backslashes.

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
 
20
22
import optparse
21
23
import re
22
24
 
23
 
from bzrlib.lazy_import import lazy_import
 
25
from .lazy_import import lazy_import
24
26
lazy_import(globals(), """
25
 
from bzrlib import (
26
 
    errors,
 
27
from breezy import (
27
28
    revisionspec,
 
29
    i18n,
28
30
    )
29
31
""")
30
32
 
31
 
from bzrlib import (
 
33
from . import (
 
34
    errors,
32
35
    registry as _mod_registry,
33
36
    )
 
37
from .sixish import (
 
38
    text_type,
 
39
    )
 
40
 
 
41
 
 
42
class BadOptionValue(errors.BzrError):
 
43
 
 
44
    _fmt = """Bad value "%(value)s" for option "%(name)s"."""
 
45
 
 
46
    def __init__(self, name, value):
 
47
        errors.BzrError.__init__(self, name=name, value=value)
34
48
 
35
49
 
36
50
def _parse_revision_str(revstr):
123
137
        return merge_types[typestring][0]
124
138
    except KeyError:
125
139
        templ = '%s%%7s: %%s' % (' '*12)
126
 
        lines = [templ % (f[0], f[1][1]) for f in merge_types.iteritems()]
 
140
        lines = [templ % (f[0], f[1][1]) for f in merge_types.items()]
127
141
        type_list = '\n'.join(lines)
128
142
        msg = "No known merge type %s. Supported types are:\n%s" %\
129
143
            (typestring, type_list)
300
314
    def validate_value(self, value):
301
315
        """Validate a value name"""
302
316
        if value not in self.registry:
303
 
            raise errors.BadOptionValue(self.name, value)
 
317
            raise BadOptionValue(self.name, value)
304
318
 
305
319
    def convert(self, value):
306
320
        """Convert a value name into an output type"""
312
326
 
313
327
    def __init__(self, name, help, registry=None, converter=None,
314
328
        value_switches=False, title=None, enum_switch=True,
315
 
        lazy_registry=None):
 
329
        lazy_registry=None, short_name=None, short_value_switches=None):
316
330
        """
317
331
        Constructor.
318
332
 
328
342
            which takes a value.
329
343
        :param lazy_registry: A tuple of (module name, attribute name) for a
330
344
            registry to be lazily loaded.
 
345
        :param short_name: The short name for the enum switch, if any
 
346
        :param short_value_switches: A dict mapping values to short names
331
347
        """
332
 
        Option.__init__(self, name, help, type=self.convert)
 
348
        Option.__init__(self, name, help, type=self.convert,
 
349
                        short_name=short_name)
333
350
        self._registry = registry
334
351
        if registry is None:
335
352
            if lazy_registry is None:
344
361
        self.converter = converter
345
362
        self.value_switches = value_switches
346
363
        self.enum_switch = enum_switch
 
364
        self.short_value_switches = short_value_switches
347
365
        self.title = title
348
366
        if self.title is None:
349
367
            self.title = name
387
405
                    help = optparse.SUPPRESS_HELP
388
406
                else:
389
407
                    help = self.registry.get_help(key)
 
408
                if (self.short_value_switches and
 
409
                    key in self.short_value_switches):
 
410
                    option_strings.append('-%s' %
 
411
                                          self.short_value_switches[key])
390
412
                parser.add_option(action='callback',
391
413
                              callback=self._optparse_value_callback(key),
392
414
                                  help=help,
422
444
 
423
445
    DEFAULT_VALUE = object()
424
446
 
 
447
    def __init__(self):
 
448
        optparse.OptionParser.__init__(self)
 
449
        self.formatter = GettextIndentedHelpFormatter()
 
450
 
425
451
    def error(self, message):
426
452
        raise errors.BzrCommandError(message)
427
453
 
428
454
 
 
455
class GettextIndentedHelpFormatter(optparse.IndentedHelpFormatter):
 
456
    """Adds gettext() call to format_option()"""
 
457
    def __init__(self):
 
458
        optparse.IndentedHelpFormatter.__init__(self)
 
459
 
 
460
    def format_option(self, option):
 
461
        """code taken from Python's optparse.py"""
 
462
        if option.help:
 
463
            option.help = i18n.gettext(option.help)
 
464
        return optparse.IndentedHelpFormatter.format_option(self, option)
 
465
 
 
466
 
429
467
def get_optparser(options):
430
 
    """Generate an optparse parser for bzrlib-style options"""
 
468
    """Generate an optparse parser for breezy-style options"""
431
469
 
432
470
    parser = OptionParser()
433
471
    parser.remove_option('--help')
434
 
    for option in options.itervalues():
 
472
    for option in options.values():
435
473
        option.add_option(parser, option.short_name())
436
474
    return parser
437
475
 
450
488
    Option.STD_OPTIONS[name] = Option(name, **kwargs)
451
489
    Option.OPTIONS[name] = Option.STD_OPTIONS[name]
452
490
 
 
491
def _standard_list_option(name, **kwargs):
 
492
    """Register a standard option."""
 
493
    # All standard options are implicitly 'global' ones
 
494
    Option.STD_OPTIONS[name] = ListOption(name, **kwargs)
 
495
    Option.OPTIONS[name] = Option.STD_OPTIONS[name]
 
496
 
453
497
 
454
498
def _global_option(name, **kwargs):
455
499
    """Register a global option."""
488
532
            _verbosity_level = -1
489
533
 
490
534
 
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
535
# Declare the standard options
507
536
_standard_option('help', short_name='h',
508
537
                 help='Show help message.')
 
538
_standard_option('quiet', short_name='q',
 
539
                 help="Only display errors and warnings.",
 
540
                 custom_callback=_verbosity_level_callback)
509
541
_standard_option('usage',
510
542
                 help='Show usage message and options.')
511
543
_standard_option('verbose', short_name='v',
512
544
                 help='Display more information.',
513
545
                 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
546
 
518
547
# Declare commonly used options
519
 
_global_option('all')
 
548
_global_option('change',
 
549
               type=_parse_change_str,
 
550
               short_name='c',
 
551
               param_name='revision',
 
552
               help='Select changes introduced by the specified revision. See also "help revisionspec".')
 
553
_global_option('directory', short_name='d', type=text_type,
 
554
               help='Branch to operate on, instead of working directory.')
 
555
_global_option('file', type=text_type, short_name='F')
 
556
_global_registry_option('log-format', "Use specified log format.",
 
557
                        lazy_registry=('breezy.log', 'log_formatter_registry'),
 
558
                        value_switches=True, title='Log format',
 
559
                        short_value_switches={'short': 'S'})
 
560
_global_registry_option('merge-type', 'Select a particular merge algorithm.',
 
561
                        lazy_registry=('breezy.merge', 'merge_type_registry'),
 
562
                        value_switches=True, title='Merge algorithm')
 
563
_global_option('message', type=text_type,
 
564
               short_name='m',
 
565
               help='Message string.')
 
566
_global_option('null', short_name='0',
 
567
                 help='Use an ASCII NUL (\\0) separator rather than '
 
568
                      'a newline.')
520
569
_global_option('overwrite', help='Ignore differences between branches and '
521
570
               '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,
530
 
               short_name='m',
531
 
               help='Message string.')
532
 
_global_option('no-recurse')
533
 
_global_option('profile',
534
 
               help='Show performance profiling information.')
 
571
_global_option('remember', help='Remember the specified location as a'
 
572
               ' default.')
 
573
_global_option('reprocess', help='Reprocess to reduce spurious conflicts.')
535
574
_global_option('revision',
536
575
               type=_parse_revision_str,
537
576
               short_name='r',
538
577
               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".')
544
578
_global_option('show-ids',
545
579
               help='Show internal object ids.')
546
580
_global_option('timezone',
547
581
               type=str,
548
582
               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.')
573
583
 
574
584
diff_writer_registry = _mod_registry.Registry()
575
585
diff_writer_registry.register('plain', lambda x: x, 'Plaintext diff output.')