/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 bzrlib/option.py

  • Committer: Martin Pool
  • Date: 2005-08-04 22:04:40 UTC
  • Revision ID: mbp@sourcefrog.net-20050804220440-99562df8151d1ac5
- add pending merge from aaron

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2004, 2005, 2006 Canonical Ltd
2
 
#
3
 
# This program is free software; you can redistribute it and/or modify
4
 
# it under the terms of the GNU General Public License as published by
5
 
# the Free Software Foundation; either version 2 of the License, or
6
 
# (at your option) any later version.
7
 
#
8
 
# This program is distributed in the hope that it will be useful,
9
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
# GNU General Public License for more details.
12
 
#
13
 
# You should have received a copy of the GNU General Public License
14
 
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
 
 
17
 
# TODO: For things like --diff-prefix, we want a way to customize the display
18
 
# of the option argument.
19
 
 
20
 
import re
21
 
 
22
 
from bzrlib.lazy_import import lazy_import
23
 
lazy_import(globals(), """
24
 
import optparse
25
 
 
26
 
from bzrlib import (
27
 
    errors,
28
 
    revisionspec,
29
 
    symbol_versioning,
30
 
    )
31
 
""")
32
 
from bzrlib.trace import warning
33
 
 
34
 
 
35
 
def _parse_revision_str(revstr):
36
 
    """This handles a revision string -> revno.
37
 
 
38
 
    This always returns a list.  The list will have one element for
39
 
    each revision specifier supplied.
40
 
 
41
 
    >>> _parse_revision_str('234')
42
 
    [<RevisionSpec_revno 234>]
43
 
    >>> _parse_revision_str('234..567')
44
 
    [<RevisionSpec_revno 234>, <RevisionSpec_revno 567>]
45
 
    >>> _parse_revision_str('..')
46
 
    [<RevisionSpec None>, <RevisionSpec None>]
47
 
    >>> _parse_revision_str('..234')
48
 
    [<RevisionSpec None>, <RevisionSpec_revno 234>]
49
 
    >>> _parse_revision_str('234..')
50
 
    [<RevisionSpec_revno 234>, <RevisionSpec None>]
51
 
    >>> _parse_revision_str('234..456..789') # Maybe this should be an error
52
 
    [<RevisionSpec_revno 234>, <RevisionSpec_revno 456>, <RevisionSpec_revno 789>]
53
 
    >>> _parse_revision_str('234....789') #Error ?
54
 
    [<RevisionSpec_revno 234>, <RevisionSpec None>, <RevisionSpec_revno 789>]
55
 
    >>> _parse_revision_str('revid:test@other.com-234234')
56
 
    [<RevisionSpec_revid revid:test@other.com-234234>]
57
 
    >>> _parse_revision_str('revid:test@other.com-234234..revid:test@other.com-234235')
58
 
    [<RevisionSpec_revid revid:test@other.com-234234>, <RevisionSpec_revid revid:test@other.com-234235>]
59
 
    >>> _parse_revision_str('revid:test@other.com-234234..23')
60
 
    [<RevisionSpec_revid revid:test@other.com-234234>, <RevisionSpec_revno 23>]
61
 
    >>> _parse_revision_str('date:2005-04-12')
62
 
    [<RevisionSpec_date date:2005-04-12>]
63
 
    >>> _parse_revision_str('date:2005-04-12 12:24:33')
64
 
    [<RevisionSpec_date date:2005-04-12 12:24:33>]
65
 
    >>> _parse_revision_str('date:2005-04-12T12:24:33')
66
 
    [<RevisionSpec_date date:2005-04-12T12:24:33>]
67
 
    >>> _parse_revision_str('date:2005-04-12,12:24:33')
68
 
    [<RevisionSpec_date date:2005-04-12,12:24:33>]
69
 
    >>> _parse_revision_str('-5..23')
70
 
    [<RevisionSpec_revno -5>, <RevisionSpec_revno 23>]
71
 
    >>> _parse_revision_str('-5')
72
 
    [<RevisionSpec_revno -5>]
73
 
    >>> _parse_revision_str('123a')
74
 
    Traceback (most recent call last):
75
 
      ...
76
 
    NoSuchRevisionSpec: No namespace registered for string: '123a'
77
 
    >>> _parse_revision_str('abc')
78
 
    Traceback (most recent call last):
79
 
      ...
80
 
    NoSuchRevisionSpec: No namespace registered for string: 'abc'
81
 
    >>> _parse_revision_str('branch:../branch2')
82
 
    [<RevisionSpec_branch branch:../branch2>]
83
 
    >>> _parse_revision_str('branch:../../branch2')
84
 
    [<RevisionSpec_branch branch:../../branch2>]
85
 
    >>> _parse_revision_str('branch:../../branch2..23')
86
 
    [<RevisionSpec_branch branch:../../branch2>, <RevisionSpec_revno 23>]
87
 
    """
88
 
    # TODO: Maybe move this into revisionspec.py
89
 
    revs = []
90
 
    # split on the first .. that is not followed by a / ?
91
 
    sep = re.compile("\\.\\.(?!/)")
92
 
    for x in sep.split(revstr):
93
 
        revs.append(revisionspec.RevisionSpec.from_string(x or None))
94
 
    return revs
95
 
 
96
 
 
97
 
def _parse_merge_type(typestring):
98
 
    return get_merge_type(typestring)
99
 
 
100
 
def get_merge_type(typestring):
101
 
    """Attempt to find the merge class/factory associated with a string."""
102
 
    from merge import merge_types
103
 
    try:
104
 
        return merge_types[typestring][0]
105
 
    except KeyError:
106
 
        templ = '%s%%7s: %%s' % (' '*12)
107
 
        lines = [templ % (f[0], f[1][1]) for f in merge_types.iteritems()]
108
 
        type_list = '\n'.join(lines)
109
 
        msg = "No known merge type %s. Supported types are:\n%s" %\
110
 
            (typestring, type_list)
111
 
        raise errors.BzrCommandError(msg)
112
 
 
113
 
 
114
 
class Option(object):
115
 
    """Description of a command line option
116
 
    
117
 
    :ivar _short_name: If this option has a single-letter name, this is it.
118
 
    Otherwise None.
119
 
    """
120
 
 
121
 
    # TODO: Some way to show in help a description of the option argument
122
 
 
123
 
    OPTIONS = {}
124
 
 
125
 
    def __init__(self, name, help='', type=None, argname=None,
126
 
                 short_name=None):
127
 
        """Make a new command option.
128
 
 
129
 
        name -- regular name of the command, used in the double-dash
130
 
            form and also as the parameter to the command's run() 
131
 
            method.
132
 
 
133
 
        help -- help message displayed in command help
134
 
 
135
 
        type -- function called to parse the option argument, or 
136
 
            None (default) if this option doesn't take an argument.
137
 
 
138
 
        argname -- name of option argument, if any
139
 
        """
140
 
        self.name = name
141
 
        self.help = help
142
 
        self.type = type
143
 
        self._short_name = short_name
144
 
        if type is None:
145
 
            assert argname is None
146
 
        elif argname is None:
147
 
            argname = 'ARG'
148
 
        self.argname = argname
149
 
 
150
 
    def short_name(self):
151
 
        if self._short_name:
152
 
            return self._short_name
153
 
        else:
154
 
            # remove this when SHORT_OPTIONS is removed
155
 
            # XXX: This is accessing a DeprecatedDict, so we call the super 
156
 
            # method to avoid warnings
157
 
            for (k, v) in dict.iteritems(Option.SHORT_OPTIONS):
158
 
                if v == self:
159
 
                    return k
160
 
 
161
 
    def set_short_name(self, short_name):
162
 
        self._short_name = short_name
163
 
 
164
 
    def get_negation_name(self):
165
 
        if self.name.startswith('no-'):
166
 
            return self.name[3:]
167
 
        else:
168
 
            return 'no-' + self.name
169
 
 
170
 
    def add_option(self, parser, short_name):
171
 
        """Add this option to an Optparse parser"""
172
 
        option_strings = ['--%s' % self.name]
173
 
        if short_name is not None:
174
 
            option_strings.append('-%s' % short_name)
175
 
        optargfn = self.type
176
 
        if optargfn is None:
177
 
            parser.add_option(action='store_true', dest=self.name, 
178
 
                              help=self.help,
179
 
                              default=OptionParser.DEFAULT_VALUE,
180
 
                              *option_strings)
181
 
            negation_strings = ['--%s' % self.get_negation_name()]
182
 
            parser.add_option(action='store_false', dest=self.name, 
183
 
                              help=optparse.SUPPRESS_HELP, *negation_strings)
184
 
        else:
185
 
            parser.add_option(action='callback', 
186
 
                              callback=self._optparse_callback, 
187
 
                              type='string', metavar=self.argname.upper(),
188
 
                              help=self.help,
189
 
                              default=OptionParser.DEFAULT_VALUE, 
190
 
                              *option_strings)
191
 
 
192
 
    def _optparse_callback(self, option, opt, value, parser):
193
 
        setattr(parser.values, self.name, self.type(value))
194
 
 
195
 
    def iter_switches(self):
196
 
        """Iterate through the list of switches provided by the option
197
 
        
198
 
        :return: an iterator of (name, short_name, argname, help)
199
 
        """
200
 
        argname =  self.argname
201
 
        if argname is not None:
202
 
            argname = argname.upper()
203
 
        yield self.name, self.short_name(), argname, self.help
204
 
 
205
 
 
206
 
class OptionParser(optparse.OptionParser):
207
 
    """OptionParser that raises exceptions instead of exiting"""
208
 
 
209
 
    DEFAULT_VALUE = object()
210
 
 
211
 
    def error(self, message):
212
 
        raise errors.BzrCommandError(message)
213
 
 
214
 
 
215
 
def get_optparser(options):
216
 
    """Generate an optparse parser for bzrlib-style options"""
217
 
 
218
 
    parser = OptionParser()
219
 
    parser.remove_option('--help')
220
 
    for option in options.itervalues():
221
 
        option.add_option(parser, option.short_name())
222
 
    return parser
223
 
 
224
 
 
225
 
def _global_option(name, **kwargs):
226
 
    """Register o as a global option."""
227
 
    Option.OPTIONS[name] = Option(name, **kwargs)
228
 
 
229
 
_global_option('all')
230
 
_global_option('overwrite', help='Ignore differences between branches and '
231
 
               'overwrite unconditionally')
232
 
_global_option('basis', type=str)
233
 
_global_option('bound')
234
 
_global_option('diff-options', type=str)
235
 
_global_option('help',
236
 
               help='show help message',
237
 
               short_name='h')
238
 
_global_option('file', type=unicode, short_name='F')
239
 
_global_option('force')
240
 
_global_option('format', type=unicode)
241
 
_global_option('forward')
242
 
_global_option('message', type=unicode,
243
 
               short_name='m')
244
 
_global_option('no-recurse')
245
 
_global_option('profile',
246
 
               help='show performance profiling information')
247
 
_global_option('revision',
248
 
               type=_parse_revision_str,
249
 
               short_name='r',
250
 
               help='See \'help revisionspec\' for details')
251
 
_global_option('show-ids', 
252
 
               help='show internal object ids')
253
 
_global_option('timezone', 
254
 
               type=str,
255
 
               help='display timezone as local, original, or utc')
256
 
_global_option('unbound')
257
 
_global_option('verbose',
258
 
               help='display more information',
259
 
               short_name='v')
260
 
_global_option('version')
261
 
_global_option('email')
262
 
_global_option('update')
263
 
_global_option('log-format', type=str, help="Use this log format")
264
 
_global_option('long', help='Use detailed log format. Same as --log-format long',
265
 
               short_name='l')
266
 
_global_option('short', help='Use moderately short log format. Same as --log-format short')
267
 
_global_option('line', help='Use log format with one line per revision. Same as --log-format line')
268
 
_global_option('root', type=str)
269
 
_global_option('no-backup')
270
 
_global_option('merge-type', type=_parse_merge_type, 
271
 
               help='Select a particular merge algorithm')
272
 
_global_option('pattern', type=str)
273
 
_global_option('quiet', short_name='q')
274
 
_global_option('remember', help='Remember the specified location as a'
275
 
               ' default.')
276
 
_global_option('reprocess', help='Reprocess to reduce spurious conflicts')
277
 
_global_option('kind', type=str)
278
 
_global_option('dry-run',
279
 
               help="show what would be done, but don't actually do anything")
280
 
_global_option('name-from-revision', help='The path name in the old tree.')
281
 
 
282
 
 
283
 
# prior to 0.14 these were always globally registered; the old dict is
284
 
# available for plugins that use it but it should not be used.
285
 
Option.SHORT_OPTIONS = symbol_versioning.DeprecatedDict(
286
 
    symbol_versioning.zero_fourteen,
287
 
    'SHORT_OPTIONS',
288
 
    {
289
 
        'F': Option.OPTIONS['file'],
290
 
        'h': Option.OPTIONS['help'],
291
 
        'm': Option.OPTIONS['message'],
292
 
        'r': Option.OPTIONS['revision'],
293
 
        'v': Option.OPTIONS['verbose'],
294
 
        'l': Option.OPTIONS['long'],
295
 
        'q': Option.OPTIONS['quiet'],
296
 
    },
297
 
    'Set the short option name when constructing the Option.',
298
 
    )