~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/option.py

  • Committer: Tarmac
  • Author(s): Vincent Ladeuil
  • Date: 2017-01-30 14:42:05 UTC
  • mfrom: (6620.1.1 trunk)
  • Revision ID: tarmac-20170130144205-r8fh2xpmiuxyozpv
Merge  2.7 into trunk including fix for bug #1657238 [r=vila]

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2004, 2005, 2006, 2007 Canonical Ltd
 
1
# Copyright (C) 2005-2010 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
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
 
25
27
from bzrlib import (
26
28
    errors,
27
29
    revisionspec,
 
30
    i18n,
28
31
    )
29
32
""")
30
33
 
40
43
    each revision specifier supplied.
41
44
 
42
45
    >>> _parse_revision_str('234')
43
 
    [<RevisionSpec_revno 234>]
 
46
    [<RevisionSpec_dwim 234>]
44
47
    >>> _parse_revision_str('234..567')
45
 
    [<RevisionSpec_revno 234>, <RevisionSpec_revno 567>]
 
48
    [<RevisionSpec_dwim 234>, <RevisionSpec_dwim 567>]
46
49
    >>> _parse_revision_str('..')
47
50
    [<RevisionSpec None>, <RevisionSpec None>]
48
51
    >>> _parse_revision_str('..234')
49
 
    [<RevisionSpec None>, <RevisionSpec_revno 234>]
 
52
    [<RevisionSpec None>, <RevisionSpec_dwim 234>]
50
53
    >>> _parse_revision_str('234..')
51
 
    [<RevisionSpec_revno 234>, <RevisionSpec None>]
 
54
    [<RevisionSpec_dwim 234>, <RevisionSpec None>]
52
55
    >>> _parse_revision_str('234..456..789') # Maybe this should be an error
53
 
    [<RevisionSpec_revno 234>, <RevisionSpec_revno 456>, <RevisionSpec_revno 789>]
 
56
    [<RevisionSpec_dwim 234>, <RevisionSpec_dwim 456>, <RevisionSpec_dwim 789>]
54
57
    >>> _parse_revision_str('234....789') #Error ?
55
 
    [<RevisionSpec_revno 234>, <RevisionSpec None>, <RevisionSpec_revno 789>]
 
58
    [<RevisionSpec_dwim 234>, <RevisionSpec None>, <RevisionSpec_dwim 789>]
56
59
    >>> _parse_revision_str('revid:test@other.com-234234')
57
60
    [<RevisionSpec_revid revid:test@other.com-234234>]
58
61
    >>> _parse_revision_str('revid:test@other.com-234234..revid:test@other.com-234235')
59
62
    [<RevisionSpec_revid revid:test@other.com-234234>, <RevisionSpec_revid revid:test@other.com-234235>]
60
63
    >>> _parse_revision_str('revid:test@other.com-234234..23')
61
 
    [<RevisionSpec_revid revid:test@other.com-234234>, <RevisionSpec_revno 23>]
 
64
    [<RevisionSpec_revid revid:test@other.com-234234>, <RevisionSpec_dwim 23>]
62
65
    >>> _parse_revision_str('date:2005-04-12')
63
66
    [<RevisionSpec_date date:2005-04-12>]
64
67
    >>> _parse_revision_str('date:2005-04-12 12:24:33')
68
71
    >>> _parse_revision_str('date:2005-04-12,12:24:33')
69
72
    [<RevisionSpec_date date:2005-04-12,12:24:33>]
70
73
    >>> _parse_revision_str('-5..23')
71
 
    [<RevisionSpec_revno -5>, <RevisionSpec_revno 23>]
 
74
    [<RevisionSpec_dwim -5>, <RevisionSpec_dwim 23>]
72
75
    >>> _parse_revision_str('-5')
73
 
    [<RevisionSpec_revno -5>]
 
76
    [<RevisionSpec_dwim -5>]
74
77
    >>> _parse_revision_str('123a')
75
 
    Traceback (most recent call last):
76
 
      ...
77
 
    NoSuchRevisionSpec: No namespace registered for string: '123a'
 
78
    [<RevisionSpec_dwim 123a>]
78
79
    >>> _parse_revision_str('abc')
79
 
    Traceback (most recent call last):
80
 
      ...
81
 
    NoSuchRevisionSpec: No namespace registered for string: 'abc'
 
80
    [<RevisionSpec_dwim abc>]
82
81
    >>> _parse_revision_str('branch:../branch2')
83
82
    [<RevisionSpec_branch branch:../branch2>]
84
83
    >>> _parse_revision_str('branch:../../branch2')
85
84
    [<RevisionSpec_branch branch:../../branch2>]
86
85
    >>> _parse_revision_str('branch:../../branch2..23')
87
 
    [<RevisionSpec_branch branch:../../branch2>, <RevisionSpec_revno 23>]
 
86
    [<RevisionSpec_branch branch:../../branch2>, <RevisionSpec_dwim 23>]
88
87
    >>> _parse_revision_str('branch:..\\\\branch2')
89
88
    [<RevisionSpec_branch branch:..\\branch2>]
90
89
    >>> _parse_revision_str('branch:..\\\\..\\\\branch2..23')
91
 
    [<RevisionSpec_branch branch:..\\..\\branch2>, <RevisionSpec_revno 23>]
 
90
    [<RevisionSpec_branch branch:..\\..\\branch2>, <RevisionSpec_dwim 23>]
92
91
    """
93
92
    # TODO: Maybe move this into revisionspec.py
94
93
    revs = []
104
103
    parent of the revision.
105
104
 
106
105
    >>> _parse_change_str('123')
107
 
    (<RevisionSpec_before before:123>, <RevisionSpec_revno 123>)
 
106
    (<RevisionSpec_before before:123>, <RevisionSpec_dwim 123>)
108
107
    >>> _parse_change_str('123..124')
109
108
    Traceback (most recent call last):
110
109
      ...
280
279
        parser.add_option(action='callback',
281
280
                          callback=self._optparse_callback,
282
281
                          type='string', metavar=self.argname.upper(),
283
 
                          help=self.help, default=[],
 
282
                          help=self.help, dest=self._param_name, default=[],
284
283
                          *option_strings)
285
284
 
286
285
    def _optparse_callback(self, option, opt, value, parser):
316
315
 
317
316
    def __init__(self, name, help, registry=None, converter=None,
318
317
        value_switches=False, title=None, enum_switch=True,
319
 
        lazy_registry=None):
 
318
        lazy_registry=None, short_name=None, short_value_switches=None):
320
319
        """
321
320
        Constructor.
322
321
 
332
331
            which takes a value.
333
332
        :param lazy_registry: A tuple of (module name, attribute name) for a
334
333
            registry to be lazily loaded.
 
334
        :param short_name: The short name for the enum switch, if any
 
335
        :param short_value_switches: A dict mapping values to short names
335
336
        """
336
 
        Option.__init__(self, name, help, type=self.convert)
 
337
        Option.__init__(self, name, help, type=self.convert,
 
338
                        short_name=short_name)
337
339
        self._registry = registry
338
340
        if registry is None:
339
341
            if lazy_registry is None:
348
350
        self.converter = converter
349
351
        self.value_switches = value_switches
350
352
        self.enum_switch = enum_switch
 
353
        self.short_value_switches = short_value_switches
351
354
        self.title = title
352
355
        if self.title is None:
353
356
            self.title = name
365
368
 
366
369
        name, help, value_switches and enum_switch are passed to the
367
370
        RegistryOption constructor.  Any other keyword arguments are treated
368
 
        as values for the option, and they value is treated as the help.
 
371
        as values for the option, and their value is treated as the help.
369
372
        """
370
373
        reg = _mod_registry.Registry()
371
 
        for name, switch_help in kwargs.iteritems():
 
374
        for name, switch_help in sorted(kwargs.items()):
372
375
            name = name.replace('_', '-')
373
376
            reg.register(name, name, help=switch_help)
374
377
            if not value_switches:
391
394
                    help = optparse.SUPPRESS_HELP
392
395
                else:
393
396
                    help = self.registry.get_help(key)
 
397
                if (self.short_value_switches and
 
398
                    key in self.short_value_switches):
 
399
                    option_strings.append('-%s' %
 
400
                                          self.short_value_switches[key])
394
401
                parser.add_option(action='callback',
395
402
                              callback=self._optparse_value_callback(key),
396
403
                                  help=help,
426
433
 
427
434
    DEFAULT_VALUE = object()
428
435
 
 
436
    def __init__(self):
 
437
        optparse.OptionParser.__init__(self)
 
438
        self.formatter = GettextIndentedHelpFormatter()
 
439
 
429
440
    def error(self, message):
430
441
        raise errors.BzrCommandError(message)
431
442
 
432
443
 
 
444
class GettextIndentedHelpFormatter(optparse.IndentedHelpFormatter):
 
445
    """Adds gettext() call to format_option()"""
 
446
    def __init__(self):
 
447
        optparse.IndentedHelpFormatter.__init__(self)
 
448
 
 
449
    def format_option(self, option):
 
450
        """code taken from Python's optparse.py"""
 
451
        if option.help:
 
452
            option.help = i18n.gettext(option.help)
 
453
        return optparse.IndentedHelpFormatter.format_option(self, option)
 
454
 
 
455
 
433
456
def get_optparser(options):
434
457
    """Generate an optparse parser for bzrlib-style options"""
435
458
 
454
477
    Option.STD_OPTIONS[name] = Option(name, **kwargs)
455
478
    Option.OPTIONS[name] = Option.STD_OPTIONS[name]
456
479
 
 
480
def _standard_list_option(name, **kwargs):
 
481
    """Register a standard option."""
 
482
    # All standard options are implicitly 'global' ones
 
483
    Option.STD_OPTIONS[name] = ListOption(name, **kwargs)
 
484
    Option.OPTIONS[name] = Option.STD_OPTIONS[name]
 
485
 
457
486
 
458
487
def _global_option(name, **kwargs):
459
488
    """Register a global option."""
492
521
            _verbosity_level = -1
493
522
 
494
523
 
495
 
class MergeTypeRegistry(_mod_registry.Registry):
496
 
 
497
 
    pass
498
 
 
499
 
 
500
 
_merge_type_registry = MergeTypeRegistry()
501
 
_merge_type_registry.register_lazy('merge3', 'bzrlib.merge', 'Merge3Merger',
502
 
                                   "Native diff3-style merge")
503
 
_merge_type_registry.register_lazy('diff3', 'bzrlib.merge', 'Diff3Merger',
504
 
                                   "Merge using external diff3")
505
 
_merge_type_registry.register_lazy('weave', 'bzrlib.merge', 'WeaveMerger',
506
 
                                   "Weave-based merge")
507
 
_merge_type_registry.register_lazy('lca', 'bzrlib.merge', 'LCAMerger',
508
 
                                   "LCA-newness merge")
509
 
 
510
524
# Declare the standard options
511
525
_standard_option('help', short_name='h',
512
526
                 help='Show help message.')
 
527
_standard_option('quiet', short_name='q',
 
528
                 help="Only display errors and warnings.",
 
529
                 custom_callback=_verbosity_level_callback)
513
530
_standard_option('usage',
514
531
                 help='Show usage message and options.')
515
532
_standard_option('verbose', short_name='v',
516
533
                 help='Display more information.',
517
534
                 custom_callback=_verbosity_level_callback)
518
 
_standard_option('quiet', short_name='q',
519
 
                 help="Only display errors and warnings.",
520
 
                 custom_callback=_verbosity_level_callback)
521
535
 
522
536
# Declare commonly used options
523
 
_global_option('all')
524
 
_global_option('overwrite', help='Ignore differences between branches and '
525
 
               'overwrite unconditionally.')
526
 
_global_option('basis', type=str)
527
 
_global_option('bound')
528
 
_global_option('diff-options', type=str)
 
537
_global_option('change',
 
538
               type=_parse_change_str,
 
539
               short_name='c',
 
540
               param_name='revision',
 
541
               help='Select changes introduced by the specified revision. See also "help revisionspec".')
 
542
_global_option('directory', short_name='d', type=unicode,
 
543
               help='Branch to operate on, instead of working directory.')
529
544
_global_option('file', type=unicode, short_name='F')
530
 
_global_option('force')
531
 
_global_option('format', type=unicode)
532
 
_global_option('forward')
 
545
_global_registry_option('log-format', "Use specified log format.",
 
546
                        lazy_registry=('bzrlib.log', 'log_formatter_registry'),
 
547
                        value_switches=True, title='Log format',
 
548
                        short_value_switches={'short': 'S'})
 
549
_global_registry_option('merge-type', 'Select a particular merge algorithm.',
 
550
                        lazy_registry=('bzrlib.merge', 'merge_type_registry'),
 
551
                        value_switches=True, title='Merge algorithm')
533
552
_global_option('message', type=unicode,
534
553
               short_name='m',
535
554
               help='Message string.')
536
 
_global_option('no-recurse')
537
 
_global_option('profile',
538
 
               help='Show performance profiling information.')
 
555
_global_option('null', short_name='0',
 
556
                 help='Use an ASCII NUL (\\0) separator rather than '
 
557
                      'a newline.')
 
558
_global_option('overwrite', help='Ignore differences between branches and '
 
559
               'overwrite unconditionally.')
 
560
_global_option('remember', help='Remember the specified location as a'
 
561
               ' default.')
 
562
_global_option('reprocess', help='Reprocess to reduce spurious conflicts.')
539
563
_global_option('revision',
540
564
               type=_parse_revision_str,
541
565
               short_name='r',
542
566
               help='See "help revisionspec" for details.')
543
 
_global_option('change',
544
 
               type=_parse_change_str,
545
 
               short_name='c',
546
 
               param_name='revision',
547
 
               help='Select changes introduced by the specified revision. See also "help revisionspec".')
548
567
_global_option('show-ids',
549
568
               help='Show internal object ids.')
550
569
_global_option('timezone',
551
570
               type=str,
552
571
               help='Display timezone as local, original, or utc.')
553
 
_global_option('unbound')
554
 
_global_option('version')
555
 
_global_option('email')
556
 
_global_option('update')
557
 
_global_registry_option('log-format', "Use specified log format.",
558
 
                        lazy_registry=('bzrlib.log', 'log_formatter_registry'),
559
 
                        value_switches=True, title='Log format')
560
 
_global_option('long', help='Use detailed log format. Same as --log-format long',
561
 
               short_name='l')
562
 
_global_option('short', help='Use moderately short log format. Same as --log-format short')
563
 
_global_option('line', help='Use log format with one line per revision. Same as --log-format line')
564
 
_global_option('root', type=str)
565
 
_global_option('no-backup')
566
 
_global_registry_option('merge-type', 'Select a particular merge algorithm.',
567
 
                        _merge_type_registry, value_switches=True,
568
 
                        title='Merge algorithm')
569
 
_global_option('pattern', type=str)
570
 
_global_option('remember', help='Remember the specified location as a'
571
 
               ' default.')
572
 
_global_option('reprocess', help='Reprocess to reduce spurious conflicts.')
573
 
_global_option('kind', type=str)
574
 
_global_option('dry-run',
575
 
               help="Show what would be done, but don't actually do anything.")
576
 
_global_option('name-from-revision', help='The path name in the old tree.')
577
572
 
578
573
diff_writer_registry = _mod_registry.Registry()
579
574
diff_writer_registry.register('plain', lambda x: x, 'Plaintext diff output.')