~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-10-02 00:43:10 UTC
  • mfrom: (2057.1.1 bzr.dev)
  • Revision ID: pqm@pqm.ubuntu.com-20061002004310-6e09ddd7fd28f71c
Merge in 0.11 NEWS entry.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2004, 2005 by Canonical Ltd
2
 
 
 
1
# Copyright (C) 2004, 2005, 2006 by Canonical Ltd
 
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
16
16
 
 
17
# TODO: For things like --diff-prefix, we want a way to customize the display
 
18
# of the option argument.
17
19
 
 
20
import optparse
18
21
import re
19
22
 
20
 
import bzrlib.commands
21
 
from bzrlib.trace import warning, mutter
 
23
from bzrlib.trace import warning
22
24
from bzrlib.revisionspec import RevisionSpec
 
25
from bzrlib.errors import BzrCommandError
23
26
 
24
27
 
25
28
def _parse_revision_str(revstr):
29
32
    each revision specifier supplied.
30
33
 
31
34
    >>> _parse_revision_str('234')
32
 
    [<RevisionSpec_int 234>]
 
35
    [<RevisionSpec_revno 234>]
33
36
    >>> _parse_revision_str('234..567')
34
 
    [<RevisionSpec_int 234>, <RevisionSpec_int 567>]
 
37
    [<RevisionSpec_revno 234>, <RevisionSpec_revno 567>]
35
38
    >>> _parse_revision_str('..')
36
39
    [<RevisionSpec None>, <RevisionSpec None>]
37
40
    >>> _parse_revision_str('..234')
38
 
    [<RevisionSpec None>, <RevisionSpec_int 234>]
 
41
    [<RevisionSpec None>, <RevisionSpec_revno 234>]
39
42
    >>> _parse_revision_str('234..')
40
 
    [<RevisionSpec_int 234>, <RevisionSpec None>]
 
43
    [<RevisionSpec_revno 234>, <RevisionSpec None>]
41
44
    >>> _parse_revision_str('234..456..789') # Maybe this should be an error
42
 
    [<RevisionSpec_int 234>, <RevisionSpec_int 456>, <RevisionSpec_int 789>]
 
45
    [<RevisionSpec_revno 234>, <RevisionSpec_revno 456>, <RevisionSpec_revno 789>]
43
46
    >>> _parse_revision_str('234....789') #Error ?
44
 
    [<RevisionSpec_int 234>, <RevisionSpec None>, <RevisionSpec_int 789>]
 
47
    [<RevisionSpec_revno 234>, <RevisionSpec None>, <RevisionSpec_revno 789>]
45
48
    >>> _parse_revision_str('revid:test@other.com-234234')
46
49
    [<RevisionSpec_revid revid:test@other.com-234234>]
47
50
    >>> _parse_revision_str('revid:test@other.com-234234..revid:test@other.com-234235')
48
51
    [<RevisionSpec_revid revid:test@other.com-234234>, <RevisionSpec_revid revid:test@other.com-234235>]
49
52
    >>> _parse_revision_str('revid:test@other.com-234234..23')
50
 
    [<RevisionSpec_revid revid:test@other.com-234234>, <RevisionSpec_int 23>]
 
53
    [<RevisionSpec_revid revid:test@other.com-234234>, <RevisionSpec_revno 23>]
51
54
    >>> _parse_revision_str('date:2005-04-12')
52
55
    [<RevisionSpec_date date:2005-04-12>]
53
56
    >>> _parse_revision_str('date:2005-04-12 12:24:33')
57
60
    >>> _parse_revision_str('date:2005-04-12,12:24:33')
58
61
    [<RevisionSpec_date date:2005-04-12,12:24:33>]
59
62
    >>> _parse_revision_str('-5..23')
60
 
    [<RevisionSpec_int -5>, <RevisionSpec_int 23>]
 
63
    [<RevisionSpec_revno -5>, <RevisionSpec_revno 23>]
61
64
    >>> _parse_revision_str('-5')
62
 
    [<RevisionSpec_int -5>]
 
65
    [<RevisionSpec_revno -5>]
63
66
    >>> _parse_revision_str('123a')
64
67
    Traceback (most recent call last):
65
68
      ...
66
 
    BzrError: No namespace registered for string: '123a'
 
69
    NoSuchRevisionSpec: No namespace registered for string: '123a'
67
70
    >>> _parse_revision_str('abc')
68
71
    Traceback (most recent call last):
69
72
      ...
70
 
    BzrError: No namespace registered for string: 'abc'
 
73
    NoSuchRevisionSpec: No namespace registered for string: 'abc'
71
74
    >>> _parse_revision_str('branch:../branch2')
72
75
    [<RevisionSpec_branch branch:../branch2>]
 
76
    >>> _parse_revision_str('branch:../../branch2')
 
77
    [<RevisionSpec_branch branch:../../branch2>]
 
78
    >>> _parse_revision_str('branch:../../branch2..23')
 
79
    [<RevisionSpec_branch branch:../../branch2>, <RevisionSpec_revno 23>]
73
80
    """
74
81
    # TODO: Maybe move this into revisionspec.py
75
 
    old_format_re = re.compile('\d*:\d*')
76
 
    m = old_format_re.match(revstr)
77
82
    revs = []
78
 
    if m:
79
 
        warning('Colon separator for revision numbers is deprecated.'
80
 
                ' Use .. instead')
81
 
        for rev in revstr.split(':'):
82
 
            if rev:
83
 
                revs.append(RevisionSpec(int(rev)))
84
 
            else:
85
 
                revs.append(RevisionSpec(None))
86
 
    else:
87
 
        next_prefix = None
88
 
        for x in revstr.split('..'):
89
 
            if not x:
90
 
                revs.append(RevisionSpec(None))
91
 
            elif x[-1] == ':':
92
 
                # looks like a namespace:.. has happened
93
 
                next_prefix = x + '..'
94
 
            else:
95
 
                if next_prefix is not None:
96
 
                    x = next_prefix + x
97
 
                revs.append(RevisionSpec(x))
98
 
                next_prefix = None
99
 
        if next_prefix is not None:
100
 
            revs.append(RevisionSpec(next_prefix))
 
83
    sep = re.compile("\\.\\.(?!/)")
 
84
    for x in sep.split(revstr):
 
85
        revs.append(RevisionSpec.from_string(x or None))
101
86
    return revs
102
87
 
103
88
 
104
89
def _parse_merge_type(typestring):
105
 
    return bzrlib.commands.get_merge_type(typestring)
 
90
    return get_merge_type(typestring)
106
91
 
 
92
def get_merge_type(typestring):
 
93
    """Attempt to find the merge class/factory associated with a string."""
 
94
    from merge import merge_types
 
95
    try:
 
96
        return merge_types[typestring][0]
 
97
    except KeyError:
 
98
        templ = '%s%%7s: %%s' % (' '*12)
 
99
        lines = [templ % (f[0], f[1][1]) for f in merge_types.iteritems()]
 
100
        type_list = '\n'.join(lines)
 
101
        msg = "No known merge type %s. Supported types are:\n%s" %\
 
102
            (typestring, type_list)
 
103
        raise BzrCommandError(msg)
107
104
 
108
105
class Option(object):
109
106
    """Description of a command line option"""
127
124
        argname -- name of option argument, if any
128
125
        """
129
126
        # TODO: perhaps a subclass that automatically does 
130
 
        # --option, --no-option for reversable booleans
 
127
        # --option, --no-option for reversible booleans
131
128
        self.name = name
132
129
        self.help = help
133
130
        self.type = type
142
139
 
143
140
        Short options are globally registered.
144
141
        """
145
 
        return Option.SHORT_OPTIONS.get(self.name)
 
142
        for short, option in Option.SHORT_OPTIONS.iteritems():
 
143
            if option is self:
 
144
                return short
 
145
 
 
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
146
207
 
147
208
 
148
209
def _global_option(name, **kwargs):
150
211
    Option.OPTIONS[name] = Option(name, **kwargs)
151
212
 
152
213
_global_option('all')
153
 
_global_option('clobber')
 
214
_global_option('overwrite', help='Ignore differences between branches and '
 
215
               'overwrite unconditionally')
154
216
_global_option('basis', type=str)
 
217
_global_option('bound')
155
218
_global_option('diff-options', type=str)
156
219
_global_option('help',
157
220
               help='show help message')
161
224
_global_option('forward')
162
225
_global_option('message', type=unicode)
163
226
_global_option('no-recurse')
 
227
_global_option('prefix', type=str, 
 
228
               help='Set prefixes to added to old and new filenames, as '
 
229
                    'two values separated by a colon.')
164
230
_global_option('profile',
165
231
               help='show performance profiling information')
166
232
_global_option('revision', type=_parse_revision_str)
167
 
_global_option('short')
168
233
_global_option('show-ids', 
169
234
               help='show internal object ids')
170
235
_global_option('timezone', 
171
236
               type=str,
172
237
               help='display timezone as local, original, or utc')
 
238
_global_option('unbound')
173
239
_global_option('verbose',
174
240
               help='display more information')
175
241
_global_option('version')
176
242
_global_option('email')
177
243
_global_option('update')
178
 
_global_option('long')
 
244
_global_option('log-format', type=str, help="Use this log format")
 
245
_global_option('long', help='Use detailed log format. Same as --log-format long')
 
246
_global_option('short', help='Use moderately short log format. Same as --log-format short')
 
247
_global_option('line', help='Use log format with one line per revision. Same as --log-format line')
179
248
_global_option('root', type=str)
180
249
_global_option('no-backup')
181
 
_global_option('merge-type', type=_parse_merge_type)
 
250
_global_option('merge-type', type=_parse_merge_type, 
 
251
               help='Select a particular merge algorithm')
182
252
_global_option('pattern', type=str)
183
253
_global_option('quiet')
184
 
_global_option('remember')
 
254
_global_option('remember', help='Remember the specified location as a'
 
255
               ' default.')
 
256
_global_option('reprocess', help='Reprocess to reduce spurious conflicts')
 
257
_global_option('kind', type=str)
 
258
_global_option('dry-run',
 
259
               help="show what would be done, but don't actually do anything")
185
260
 
186
261
 
187
262
def _global_short(short_name, long_name):
196
271
Option.SHORT_OPTIONS['v'] = Option.OPTIONS['verbose']
197
272
Option.SHORT_OPTIONS['l'] = Option.OPTIONS['long']
198
273
Option.SHORT_OPTIONS['q'] = Option.OPTIONS['quiet']
 
274
Option.SHORT_OPTIONS['p'] = Option.OPTIONS['prefix']