~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/option.py

  • Committer: Andrew Bennetts
  • Date: 2009-09-08 08:09:25 UTC
  • mto: (4634.6.27 2.0)
  • mto: This revision was merged to the branch mainline in revision 4680.
  • Revision ID: andrew.bennetts@canonical.com-20090908080925-ccmjw4kzzz7bepg7
Fix more tests to cope with new commit_write_group strictness.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2004, 2005, 2006, 2007 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
25
25
from bzrlib import (
26
26
    errors,
27
27
    revisionspec,
28
 
    i18n,
29
28
    )
30
29
""")
31
30
 
41
40
    each revision specifier supplied.
42
41
 
43
42
    >>> _parse_revision_str('234')
44
 
    [<RevisionSpec_dwim 234>]
 
43
    [<RevisionSpec_revno 234>]
45
44
    >>> _parse_revision_str('234..567')
46
 
    [<RevisionSpec_dwim 234>, <RevisionSpec_dwim 567>]
 
45
    [<RevisionSpec_revno 234>, <RevisionSpec_revno 567>]
47
46
    >>> _parse_revision_str('..')
48
47
    [<RevisionSpec None>, <RevisionSpec None>]
49
48
    >>> _parse_revision_str('..234')
50
 
    [<RevisionSpec None>, <RevisionSpec_dwim 234>]
 
49
    [<RevisionSpec None>, <RevisionSpec_revno 234>]
51
50
    >>> _parse_revision_str('234..')
52
 
    [<RevisionSpec_dwim 234>, <RevisionSpec None>]
 
51
    [<RevisionSpec_revno 234>, <RevisionSpec None>]
53
52
    >>> _parse_revision_str('234..456..789') # Maybe this should be an error
54
 
    [<RevisionSpec_dwim 234>, <RevisionSpec_dwim 456>, <RevisionSpec_dwim 789>]
 
53
    [<RevisionSpec_revno 234>, <RevisionSpec_revno 456>, <RevisionSpec_revno 789>]
55
54
    >>> _parse_revision_str('234....789') #Error ?
56
 
    [<RevisionSpec_dwim 234>, <RevisionSpec None>, <RevisionSpec_dwim 789>]
 
55
    [<RevisionSpec_revno 234>, <RevisionSpec None>, <RevisionSpec_revno 789>]
57
56
    >>> _parse_revision_str('revid:test@other.com-234234')
58
57
    [<RevisionSpec_revid revid:test@other.com-234234>]
59
58
    >>> _parse_revision_str('revid:test@other.com-234234..revid:test@other.com-234235')
60
59
    [<RevisionSpec_revid revid:test@other.com-234234>, <RevisionSpec_revid revid:test@other.com-234235>]
61
60
    >>> _parse_revision_str('revid:test@other.com-234234..23')
62
 
    [<RevisionSpec_revid revid:test@other.com-234234>, <RevisionSpec_dwim 23>]
 
61
    [<RevisionSpec_revid revid:test@other.com-234234>, <RevisionSpec_revno 23>]
63
62
    >>> _parse_revision_str('date:2005-04-12')
64
63
    [<RevisionSpec_date date:2005-04-12>]
65
64
    >>> _parse_revision_str('date:2005-04-12 12:24:33')
69
68
    >>> _parse_revision_str('date:2005-04-12,12:24:33')
70
69
    [<RevisionSpec_date date:2005-04-12,12:24:33>]
71
70
    >>> _parse_revision_str('-5..23')
72
 
    [<RevisionSpec_dwim -5>, <RevisionSpec_dwim 23>]
 
71
    [<RevisionSpec_revno -5>, <RevisionSpec_revno 23>]
73
72
    >>> _parse_revision_str('-5')
74
 
    [<RevisionSpec_dwim -5>]
 
73
    [<RevisionSpec_revno -5>]
75
74
    >>> _parse_revision_str('123a')
76
 
    [<RevisionSpec_dwim 123a>]
 
75
    Traceback (most recent call last):
 
76
      ...
 
77
    NoSuchRevisionSpec: No namespace registered for string: '123a'
77
78
    >>> _parse_revision_str('abc')
78
 
    [<RevisionSpec_dwim abc>]
 
79
    Traceback (most recent call last):
 
80
      ...
 
81
    NoSuchRevisionSpec: No namespace registered for string: 'abc'
79
82
    >>> _parse_revision_str('branch:../branch2')
80
83
    [<RevisionSpec_branch branch:../branch2>]
81
84
    >>> _parse_revision_str('branch:../../branch2')
82
85
    [<RevisionSpec_branch branch:../../branch2>]
83
86
    >>> _parse_revision_str('branch:../../branch2..23')
84
 
    [<RevisionSpec_branch branch:../../branch2>, <RevisionSpec_dwim 23>]
 
87
    [<RevisionSpec_branch branch:../../branch2>, <RevisionSpec_revno 23>]
85
88
    >>> _parse_revision_str('branch:..\\\\branch2')
86
89
    [<RevisionSpec_branch branch:..\\branch2>]
87
90
    >>> _parse_revision_str('branch:..\\\\..\\\\branch2..23')
88
 
    [<RevisionSpec_branch branch:..\\..\\branch2>, <RevisionSpec_dwim 23>]
 
91
    [<RevisionSpec_branch branch:..\\..\\branch2>, <RevisionSpec_revno 23>]
89
92
    """
90
93
    # TODO: Maybe move this into revisionspec.py
91
94
    revs = []
101
104
    parent of the revision.
102
105
 
103
106
    >>> _parse_change_str('123')
104
 
    (<RevisionSpec_before before:123>, <RevisionSpec_dwim 123>)
 
107
    (<RevisionSpec_before before:123>, <RevisionSpec_revno 123>)
105
108
    >>> _parse_change_str('123..124')
106
109
    Traceback (most recent call last):
107
110
      ...
277
280
        parser.add_option(action='callback',
278
281
                          callback=self._optparse_callback,
279
282
                          type='string', metavar=self.argname.upper(),
280
 
                          help=self.help, dest=self._param_name, default=[],
 
283
                          help=self.help, default=[],
281
284
                          *option_strings)
282
285
 
283
286
    def _optparse_callback(self, option, opt, value, parser):
313
316
 
314
317
    def __init__(self, name, help, registry=None, converter=None,
315
318
        value_switches=False, title=None, enum_switch=True,
316
 
        lazy_registry=None, short_name=None, short_value_switches=None):
 
319
        lazy_registry=None):
317
320
        """
318
321
        Constructor.
319
322
 
329
332
            which takes a value.
330
333
        :param lazy_registry: A tuple of (module name, attribute name) for a
331
334
            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
334
335
        """
335
 
        Option.__init__(self, name, help, type=self.convert, short_name=short_name)
 
336
        Option.__init__(self, name, help, type=self.convert)
336
337
        self._registry = registry
337
338
        if registry is None:
338
339
            if lazy_registry is None:
347
348
        self.converter = converter
348
349
        self.value_switches = value_switches
349
350
        self.enum_switch = enum_switch
350
 
        self.short_value_switches = short_value_switches
351
351
        self.title = title
352
352
        if self.title is None:
353
353
            self.title = name
365
365
 
366
366
        name, help, value_switches and enum_switch are passed to the
367
367
        RegistryOption constructor.  Any other keyword arguments are treated
368
 
        as values for the option, and their value is treated as the help.
 
368
        as values for the option, and they value is treated as the help.
369
369
        """
370
370
        reg = _mod_registry.Registry()
371
 
        for name, switch_help in sorted(kwargs.items()):
 
371
        for name, switch_help in kwargs.iteritems():
372
372
            name = name.replace('_', '-')
373
373
            reg.register(name, name, help=switch_help)
374
374
            if not value_switches:
391
391
                    help = optparse.SUPPRESS_HELP
392
392
                else:
393
393
                    help = self.registry.get_help(key)
394
 
                if (self.short_value_switches and
395
 
                    key in self.short_value_switches):
396
 
                    option_strings.append('-%s' %
397
 
                                          self.short_value_switches[key])
398
394
                parser.add_option(action='callback',
399
395
                              callback=self._optparse_value_callback(key),
400
396
                                  help=help,
430
426
 
431
427
    DEFAULT_VALUE = object()
432
428
 
433
 
    def __init__(self):
434
 
        optparse.OptionParser.__init__(self)
435
 
        self.formatter = GettextIndentedHelpFormatter()
436
 
 
437
429
    def error(self, message):
438
430
        raise errors.BzrCommandError(message)
439
431
 
440
 
class GettextIndentedHelpFormatter(optparse.IndentedHelpFormatter):
441
 
    """Adds gettext() call to format_option()"""
442
 
    def __init__(self):
443
 
        optparse.IndentedHelpFormatter.__init__(self)
444
 
 
445
 
    def format_option(self, option):
446
 
        """code taken from Python's optparse.py"""
447
 
        if option.help:
448
 
            option.help = i18n.gettext(option.help)
449
 
        return optparse.IndentedHelpFormatter.format_option(self, option)
450
432
 
451
433
def get_optparser(options):
452
434
    """Generate an optparse parser for bzrlib-style options"""
552
534
               short_name='m',
553
535
               help='Message string.')
554
536
_global_option('no-recurse')
555
 
_global_option('null', short_name='0',
556
 
                 help='Use an ASCII NUL (\\0) separator rather than '
557
 
                      'a newline.')
558
537
_global_option('profile',
559
538
               help='Show performance profiling information.')
560
539
_global_option('revision',
577
556
_global_option('update')
578
557
_global_registry_option('log-format', "Use specified log format.",
579
558
                        lazy_registry=('bzrlib.log', 'log_formatter_registry'),
580
 
                        value_switches=True, title='Log format',
581
 
                        short_value_switches={'short': 'S'})
 
559
                        value_switches=True, title='Log format')
582
560
_global_option('long', help='Use detailed log format. Same as --log-format long',
583
561
               short_name='l')
584
562
_global_option('short', help='Use moderately short log format. Same as --log-format short')
596
574
_global_option('dry-run',
597
575
               help="Show what would be done, but don't actually do anything.")
598
576
_global_option('name-from-revision', help='The path name in the old tree.')
599
 
_global_option('directory', short_name='d', type=unicode,
600
 
               help='Branch to operate on, instead of working directory')
601
577
 
602
578
diff_writer_registry = _mod_registry.Registry()
603
579
diff_writer_registry.register('plain', lambda x: x, 'Plaintext diff output.')