/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-06-10 16:40:42 UTC
  • mfrom: (6653.6.7 rename-controldir)
  • mto: This revision was merged to the branch mainline in revision 6690.
  • Revision ID: jelmer@jelmer.uk-20170610164042-zrxqgy2htyduvke2
MergeĀ rename-controldirĀ branch.

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 (
 
27
from breezy import (
26
28
    errors,
27
29
    revisionspec,
 
30
    i18n,
28
31
    )
29
32
""")
30
33
 
31
 
from bzrlib import (
 
34
from . import (
32
35
    registry as _mod_registry,
33
36
    )
 
37
from .sixish import (
 
38
    text_type,
 
39
    )
34
40
 
35
41
 
36
42
def _parse_revision_str(revstr):
123
129
        return merge_types[typestring][0]
124
130
    except KeyError:
125
131
        templ = '%s%%7s: %%s' % (' '*12)
126
 
        lines = [templ % (f[0], f[1][1]) for f in merge_types.iteritems()]
 
132
        lines = [templ % (f[0], f[1][1]) for f in merge_types.items()]
127
133
        type_list = '\n'.join(lines)
128
134
        msg = "No known merge type %s. Supported types are:\n%s" %\
129
135
            (typestring, type_list)
312
318
 
313
319
    def __init__(self, name, help, registry=None, converter=None,
314
320
        value_switches=False, title=None, enum_switch=True,
315
 
        lazy_registry=None):
 
321
        lazy_registry=None, short_name=None, short_value_switches=None):
316
322
        """
317
323
        Constructor.
318
324
 
328
334
            which takes a value.
329
335
        :param lazy_registry: A tuple of (module name, attribute name) for a
330
336
            registry to be lazily loaded.
 
337
        :param short_name: The short name for the enum switch, if any
 
338
        :param short_value_switches: A dict mapping values to short names
331
339
        """
332
 
        Option.__init__(self, name, help, type=self.convert)
 
340
        Option.__init__(self, name, help, type=self.convert,
 
341
                        short_name=short_name)
333
342
        self._registry = registry
334
343
        if registry is None:
335
344
            if lazy_registry is None:
344
353
        self.converter = converter
345
354
        self.value_switches = value_switches
346
355
        self.enum_switch = enum_switch
 
356
        self.short_value_switches = short_value_switches
347
357
        self.title = title
348
358
        if self.title is None:
349
359
            self.title = name
387
397
                    help = optparse.SUPPRESS_HELP
388
398
                else:
389
399
                    help = self.registry.get_help(key)
 
400
                if (self.short_value_switches and
 
401
                    key in self.short_value_switches):
 
402
                    option_strings.append('-%s' %
 
403
                                          self.short_value_switches[key])
390
404
                parser.add_option(action='callback',
391
405
                              callback=self._optparse_value_callback(key),
392
406
                                  help=help,
422
436
 
423
437
    DEFAULT_VALUE = object()
424
438
 
 
439
    def __init__(self):
 
440
        optparse.OptionParser.__init__(self)
 
441
        self.formatter = GettextIndentedHelpFormatter()
 
442
 
425
443
    def error(self, message):
426
444
        raise errors.BzrCommandError(message)
427
445
 
428
446
 
 
447
class GettextIndentedHelpFormatter(optparse.IndentedHelpFormatter):
 
448
    """Adds gettext() call to format_option()"""
 
449
    def __init__(self):
 
450
        optparse.IndentedHelpFormatter.__init__(self)
 
451
 
 
452
    def format_option(self, option):
 
453
        """code taken from Python's optparse.py"""
 
454
        if option.help:
 
455
            option.help = i18n.gettext(option.help)
 
456
        return optparse.IndentedHelpFormatter.format_option(self, option)
 
457
 
 
458
 
429
459
def get_optparser(options):
430
 
    """Generate an optparse parser for bzrlib-style options"""
 
460
    """Generate an optparse parser for breezy-style options"""
431
461
 
432
462
    parser = OptionParser()
433
463
    parser.remove_option('--help')
434
 
    for option in options.itervalues():
 
464
    for option in options.values():
435
465
        option.add_option(parser, option.short_name())
436
466
    return parser
437
467
 
450
480
    Option.STD_OPTIONS[name] = Option(name, **kwargs)
451
481
    Option.OPTIONS[name] = Option.STD_OPTIONS[name]
452
482
 
 
483
def _standard_list_option(name, **kwargs):
 
484
    """Register a standard option."""
 
485
    # All standard options are implicitly 'global' ones
 
486
    Option.STD_OPTIONS[name] = ListOption(name, **kwargs)
 
487
    Option.OPTIONS[name] = Option.STD_OPTIONS[name]
 
488
 
453
489
 
454
490
def _global_option(name, **kwargs):
455
491
    """Register a global option."""
488
524
            _verbosity_level = -1
489
525
 
490
526
 
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
527
# Declare the standard options
507
528
_standard_option('help', short_name='h',
508
529
                 help='Show help message.')
 
530
_standard_option('quiet', short_name='q',
 
531
                 help="Only display errors and warnings.",
 
532
                 custom_callback=_verbosity_level_callback)
509
533
_standard_option('usage',
510
534
                 help='Show usage message and options.')
511
535
_standard_option('verbose', short_name='v',
512
536
                 help='Display more information.',
513
537
                 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
538
 
518
539
# Declare commonly used options
519
 
_global_option('all')
 
540
_global_option('change',
 
541
               type=_parse_change_str,
 
542
               short_name='c',
 
543
               param_name='revision',
 
544
               help='Select changes introduced by the specified revision. See also "help revisionspec".')
 
545
_global_option('directory', short_name='d', type=text_type,
 
546
               help='Branch to operate on, instead of working directory.')
 
547
_global_option('file', type=text_type, short_name='F')
 
548
_global_registry_option('log-format', "Use specified log format.",
 
549
                        lazy_registry=('breezy.log', 'log_formatter_registry'),
 
550
                        value_switches=True, title='Log format',
 
551
                        short_value_switches={'short': 'S'})
 
552
_global_registry_option('merge-type', 'Select a particular merge algorithm.',
 
553
                        lazy_registry=('breezy.merge', 'merge_type_registry'),
 
554
                        value_switches=True, title='Merge algorithm')
 
555
_global_option('message', type=text_type,
 
556
               short_name='m',
 
557
               help='Message string.')
 
558
_global_option('null', short_name='0',
 
559
                 help='Use an ASCII NUL (\\0) separator rather than '
 
560
                      'a newline.')
520
561
_global_option('overwrite', help='Ignore differences between branches and '
521
562
               '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.')
 
563
_global_option('remember', help='Remember the specified location as a'
 
564
               ' default.')
 
565
_global_option('reprocess', help='Reprocess to reduce spurious conflicts.')
535
566
_global_option('revision',
536
567
               type=_parse_revision_str,
537
568
               short_name='r',
538
569
               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
570
_global_option('show-ids',
545
571
               help='Show internal object ids.')
546
572
_global_option('timezone',
547
573
               type=str,
548
574
               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
575
 
574
576
diff_writer_registry = _mod_registry.Registry()
575
577
diff_writer_registry.register('plain', lambda x: x, 'Plaintext diff output.')