~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/option.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2006-05-19 06:14:38 UTC
  • mfrom: (1704.2.23 bzr.mbp.integration)
  • Revision ID: pqm@pqm.ubuntu.com-20060519061438-6300caf3926c3cff
(mbp) small fixes

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2004, 2005, 2006 by 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
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
#
 
7
 
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
12
 
#
 
12
 
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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
 
import optparse
21
20
import re
22
21
 
23
 
from bzrlib.trace import warning
 
22
import bzrlib.commands
 
23
from bzrlib.trace import warning, mutter
24
24
from bzrlib.revisionspec import RevisionSpec
25
25
from bzrlib.errors import BzrCommandError
26
26
 
32
32
    each revision specifier supplied.
33
33
 
34
34
    >>> _parse_revision_str('234')
35
 
    [<RevisionSpec_revno 234>]
 
35
    [<RevisionSpec_int 234>]
36
36
    >>> _parse_revision_str('234..567')
37
 
    [<RevisionSpec_revno 234>, <RevisionSpec_revno 567>]
 
37
    [<RevisionSpec_int 234>, <RevisionSpec_int 567>]
38
38
    >>> _parse_revision_str('..')
39
39
    [<RevisionSpec None>, <RevisionSpec None>]
40
40
    >>> _parse_revision_str('..234')
41
 
    [<RevisionSpec None>, <RevisionSpec_revno 234>]
 
41
    [<RevisionSpec None>, <RevisionSpec_int 234>]
42
42
    >>> _parse_revision_str('234..')
43
 
    [<RevisionSpec_revno 234>, <RevisionSpec None>]
 
43
    [<RevisionSpec_int 234>, <RevisionSpec None>]
44
44
    >>> _parse_revision_str('234..456..789') # Maybe this should be an error
45
 
    [<RevisionSpec_revno 234>, <RevisionSpec_revno 456>, <RevisionSpec_revno 789>]
 
45
    [<RevisionSpec_int 234>, <RevisionSpec_int 456>, <RevisionSpec_int 789>]
46
46
    >>> _parse_revision_str('234....789') #Error ?
47
 
    [<RevisionSpec_revno 234>, <RevisionSpec None>, <RevisionSpec_revno 789>]
 
47
    [<RevisionSpec_int 234>, <RevisionSpec None>, <RevisionSpec_int 789>]
48
48
    >>> _parse_revision_str('revid:test@other.com-234234')
49
49
    [<RevisionSpec_revid revid:test@other.com-234234>]
50
50
    >>> _parse_revision_str('revid:test@other.com-234234..revid:test@other.com-234235')
51
51
    [<RevisionSpec_revid revid:test@other.com-234234>, <RevisionSpec_revid revid:test@other.com-234235>]
52
52
    >>> _parse_revision_str('revid:test@other.com-234234..23')
53
 
    [<RevisionSpec_revid revid:test@other.com-234234>, <RevisionSpec_revno 23>]
 
53
    [<RevisionSpec_revid revid:test@other.com-234234>, <RevisionSpec_int 23>]
54
54
    >>> _parse_revision_str('date:2005-04-12')
55
55
    [<RevisionSpec_date date:2005-04-12>]
56
56
    >>> _parse_revision_str('date:2005-04-12 12:24:33')
60
60
    >>> _parse_revision_str('date:2005-04-12,12:24:33')
61
61
    [<RevisionSpec_date date:2005-04-12,12:24:33>]
62
62
    >>> _parse_revision_str('-5..23')
63
 
    [<RevisionSpec_revno -5>, <RevisionSpec_revno 23>]
 
63
    [<RevisionSpec_int -5>, <RevisionSpec_int 23>]
64
64
    >>> _parse_revision_str('-5')
65
 
    [<RevisionSpec_revno -5>]
 
65
    [<RevisionSpec_int -5>]
66
66
    >>> _parse_revision_str('123a')
67
67
    Traceback (most recent call last):
68
68
      ...
69
 
    NoSuchRevisionSpec: No namespace registered for string: '123a'
 
69
    BzrError: No namespace registered for string: '123a'
70
70
    >>> _parse_revision_str('abc')
71
71
    Traceback (most recent call last):
72
72
      ...
73
 
    NoSuchRevisionSpec: No namespace registered for string: 'abc'
 
73
    BzrError: No namespace registered for string: 'abc'
74
74
    >>> _parse_revision_str('branch:../branch2')
75
75
    [<RevisionSpec_branch branch:../branch2>]
76
76
    >>> _parse_revision_str('branch:../../branch2')
77
77
    [<RevisionSpec_branch branch:../../branch2>]
78
78
    >>> _parse_revision_str('branch:../../branch2..23')
79
 
    [<RevisionSpec_branch branch:../../branch2>, <RevisionSpec_revno 23>]
 
79
    [<RevisionSpec_branch branch:../../branch2>, <RevisionSpec_int 23>]
80
80
    """
81
81
    # TODO: Maybe move this into revisionspec.py
 
82
    old_format_re = re.compile('\d*:\d*')
 
83
    m = old_format_re.match(revstr)
82
84
    revs = []
83
 
    sep = re.compile("\\.\\.(?!/)")
84
 
    for x in sep.split(revstr):
85
 
        revs.append(RevisionSpec.from_string(x or None))
 
85
    if m:
 
86
        warning('Colon separator for revision numbers is deprecated.'
 
87
                ' Use .. instead')
 
88
        for rev in revstr.split(':'):
 
89
            if rev:
 
90
                revs.append(RevisionSpec(int(rev)))
 
91
            else:
 
92
                revs.append(RevisionSpec(None))
 
93
    else:
 
94
        sep = re.compile("\\.\\.(?!/)")
 
95
        for x in sep.split(revstr):
 
96
            revs.append(RevisionSpec(x or None))
86
97
    return revs
87
98
 
88
99
 
124
135
        argname -- name of option argument, if any
125
136
        """
126
137
        # TODO: perhaps a subclass that automatically does 
127
 
        # --option, --no-option for reversible booleans
 
138
        # --option, --no-option for reversable booleans
128
139
        self.name = name
129
140
        self.help = help
130
141
        self.type = type
143
154
            if option is self:
144
155
                return short
145
156
 
146
 
    def get_negation_name(self):
147
 
        if self.name.startswith('no-'):
148
 
            return self.name[3:]
149
 
        else:
150
 
            return 'no-' + self.name
151
 
 
152
 
    def add_option(self, parser, short_name):
153
 
        """Add this option to an Optparse parser"""
154
 
        option_strings = ['--%s' % self.name]
155
 
        if short_name is not None:
156
 
            option_strings.append('-%s' % short_name)
157
 
        optargfn = self.type
158
 
        if optargfn is None:
159
 
            parser.add_option(action='store_true', dest=self.name, 
160
 
                              help=self.help,
161
 
                              default=OptionParser.DEFAULT_VALUE,
162
 
                              *option_strings)
163
 
            negation_strings = ['--%s' % self.get_negation_name()]
164
 
            parser.add_option(action='store_false', dest=self.name, 
165
 
                              help=optparse.SUPPRESS_HELP, *negation_strings)
166
 
        else:
167
 
            parser.add_option(action='callback', 
168
 
                              callback=self._optparse_callback, 
169
 
                              type='string', metavar=self.argname.upper(),
170
 
                              help=self.help,
171
 
                              default=OptionParser.DEFAULT_VALUE, 
172
 
                              *option_strings)
173
 
 
174
 
    def _optparse_callback(self, option, opt, value, parser):
175
 
        setattr(parser.values, self.name, self.type(value))
176
 
 
177
 
    def iter_switches(self):
178
 
        """Iterate through the list of switches provided by the option
179
 
        
180
 
        :return: an iterator of (name, short_name, argname, help)
181
 
        """
182
 
        argname =  self.argname
183
 
        if argname is not None:
184
 
            argname = argname.upper()
185
 
        yield self.name, self.short_name(), argname, self.help
186
 
 
187
 
 
188
 
class OptionParser(optparse.OptionParser):
189
 
    """OptionParser that raises exceptions instead of exiting"""
190
 
 
191
 
    DEFAULT_VALUE = object()
192
 
 
193
 
    def error(self, message):
194
 
        raise BzrCommandError(message)
195
 
 
196
 
 
197
 
def get_optparser(options):
198
 
    """Generate an optparse parser for bzrlib-style options"""
199
 
 
200
 
    parser = OptionParser()
201
 
    parser.remove_option('--help')
202
 
    short_options = dict((k.name, v) for v, k in 
203
 
                         Option.SHORT_OPTIONS.iteritems())
204
 
    for option in options.itervalues():
205
 
        option.add_option(parser, short_options.get(option.name))
206
 
    return parser
207
 
 
208
157
 
209
158
def _global_option(name, **kwargs):
210
159
    """Register o as a global option."""
247
196
_global_option('line', help='Use log format with one line per revision. Same as --log-format line')
248
197
_global_option('root', type=str)
249
198
_global_option('no-backup')
250
 
_global_option('merge-type', type=_parse_merge_type, 
251
 
               help='Select a particular merge algorithm')
 
199
_global_option('merge-type', type=_parse_merge_type)
252
200
_global_option('pattern', type=str)
253
201
_global_option('quiet')
254
202
_global_option('remember', help='Remember the specified location as a'