1
# Copyright (C) 2004, 2005, 2006 Canonical Ltd
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.
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.
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
17
# TODO: For things like --diff-prefix, we want a way to customize the display
18
# of the option argument.
22
from bzrlib.lazy_import import lazy_import
23
lazy_import(globals(), """
31
from bzrlib.trace import warning
34
def _parse_revision_str(revstr):
35
"""This handles a revision string -> revno.
37
This always returns a list. The list will have one element for
38
each revision specifier supplied.
40
>>> _parse_revision_str('234')
41
[<RevisionSpec_revno 234>]
42
>>> _parse_revision_str('234..567')
43
[<RevisionSpec_revno 234>, <RevisionSpec_revno 567>]
44
>>> _parse_revision_str('..')
45
[<RevisionSpec None>, <RevisionSpec None>]
46
>>> _parse_revision_str('..234')
47
[<RevisionSpec None>, <RevisionSpec_revno 234>]
48
>>> _parse_revision_str('234..')
49
[<RevisionSpec_revno 234>, <RevisionSpec None>]
50
>>> _parse_revision_str('234..456..789') # Maybe this should be an error
51
[<RevisionSpec_revno 234>, <RevisionSpec_revno 456>, <RevisionSpec_revno 789>]
52
>>> _parse_revision_str('234....789') #Error ?
53
[<RevisionSpec_revno 234>, <RevisionSpec None>, <RevisionSpec_revno 789>]
54
>>> _parse_revision_str('revid:test@other.com-234234')
55
[<RevisionSpec_revid revid:test@other.com-234234>]
56
>>> _parse_revision_str('revid:test@other.com-234234..revid:test@other.com-234235')
57
[<RevisionSpec_revid revid:test@other.com-234234>, <RevisionSpec_revid revid:test@other.com-234235>]
58
>>> _parse_revision_str('revid:test@other.com-234234..23')
59
[<RevisionSpec_revid revid:test@other.com-234234>, <RevisionSpec_revno 23>]
60
>>> _parse_revision_str('date:2005-04-12')
61
[<RevisionSpec_date date:2005-04-12>]
62
>>> _parse_revision_str('date:2005-04-12 12:24:33')
63
[<RevisionSpec_date date:2005-04-12 12:24:33>]
64
>>> _parse_revision_str('date:2005-04-12T12:24:33')
65
[<RevisionSpec_date date:2005-04-12T12:24:33>]
66
>>> _parse_revision_str('date:2005-04-12,12:24:33')
67
[<RevisionSpec_date date:2005-04-12,12:24:33>]
68
>>> _parse_revision_str('-5..23')
69
[<RevisionSpec_revno -5>, <RevisionSpec_revno 23>]
70
>>> _parse_revision_str('-5')
71
[<RevisionSpec_revno -5>]
72
>>> _parse_revision_str('123a')
73
Traceback (most recent call last):
75
NoSuchRevisionSpec: No namespace registered for string: '123a'
76
>>> _parse_revision_str('abc')
77
Traceback (most recent call last):
79
NoSuchRevisionSpec: No namespace registered for string: 'abc'
80
>>> _parse_revision_str('branch:../branch2')
81
[<RevisionSpec_branch branch:../branch2>]
82
>>> _parse_revision_str('branch:../../branch2')
83
[<RevisionSpec_branch branch:../../branch2>]
84
>>> _parse_revision_str('branch:../../branch2..23')
85
[<RevisionSpec_branch branch:../../branch2>, <RevisionSpec_revno 23>]
87
# TODO: Maybe move this into revisionspec.py
89
# split on the first .. that is not followed by a / ?
90
sep = re.compile("\\.\\.(?!/)")
91
for x in sep.split(revstr):
92
revs.append(revisionspec.RevisionSpec.from_string(x or None))
96
def _parse_merge_type(typestring):
97
return get_merge_type(typestring)
99
def get_merge_type(typestring):
100
"""Attempt to find the merge class/factory associated with a string."""
101
from merge import merge_types
103
return merge_types[typestring][0]
105
templ = '%s%%7s: %%s' % (' '*12)
106
lines = [templ % (f[0], f[1][1]) for f in merge_types.iteritems()]
107
type_list = '\n'.join(lines)
108
msg = "No known merge type %s. Supported types are:\n%s" %\
109
(typestring, type_list)
110
raise errors.BzrCommandError(msg)
112
class Option(object):
113
"""Description of a command line option
115
:ivar short_name: If this option has a single-letter name, this is it.
119
# TODO: Some way to show in help a description of the option argument
123
def __init__(self, name, help='', type=None, argname=None,
125
"""Make a new command option.
127
name -- regular name of the command, used in the double-dash
128
form and also as the parameter to the command's run()
131
help -- help message displayed in command help
133
type -- function called to parse the option argument, or
134
None (default) if this option doesn't take an argument.
136
argname -- name of option argument, if any
141
self.short_name = short_name
143
assert argname is None
144
elif argname is None:
146
self.argname = argname
148
def get_negation_name(self):
149
if self.name.startswith('no-'):
152
return 'no-' + self.name
154
def add_option(self, parser, short_name):
155
"""Add this option to an Optparse parser"""
156
option_strings = ['--%s' % self.name]
157
if short_name is not None:
158
option_strings.append('-%s' % short_name)
161
parser.add_option(action='store_true', dest=self.name,
163
default=OptionParser.DEFAULT_VALUE,
165
negation_strings = ['--%s' % self.get_negation_name()]
166
parser.add_option(action='store_false', dest=self.name,
167
help=optparse.SUPPRESS_HELP, *negation_strings)
169
parser.add_option(action='callback',
170
callback=self._optparse_callback,
171
type='string', metavar=self.argname.upper(),
173
default=OptionParser.DEFAULT_VALUE,
176
def _optparse_callback(self, option, opt, value, parser):
177
setattr(parser.values, self.name, self.type(value))
179
def iter_switches(self):
180
"""Iterate through the list of switches provided by the option
182
:return: an iterator of (name, short_name, argname, help)
184
argname = self.argname
185
if argname is not None:
186
argname = argname.upper()
187
yield self.name, self.short_name, argname, self.help
190
class OptionParser(optparse.OptionParser):
191
"""OptionParser that raises exceptions instead of exiting"""
193
DEFAULT_VALUE = object()
195
def error(self, message):
196
raise errors.BzrCommandError(message)
199
def get_optparser(options):
200
"""Generate an optparse parser for bzrlib-style options"""
202
parser = OptionParser()
203
parser.remove_option('--help')
204
for option in options.itervalues():
205
option.add_option(parser, option.short_name)
209
def _global_option(name, **kwargs):
210
"""Register o as a global option."""
211
Option.OPTIONS[name] = Option(name, **kwargs)
213
_global_option('all')
214
_global_option('overwrite', help='Ignore differences between branches and '
215
'overwrite unconditionally')
216
_global_option('basis', type=str)
217
_global_option('bound')
218
_global_option('diff-options', type=str)
219
_global_option('help',
220
help='show help message',
222
_global_option('file', type=unicode, short_name='F')
223
_global_option('force')
224
_global_option('format', type=unicode)
225
_global_option('forward')
226
_global_option('message', type=unicode,
228
_global_option('no-recurse')
229
_global_option('profile',
230
help='show performance profiling information')
231
_global_option('revision',
232
type=_parse_revision_str,
234
help='See \'help revisionspec\' for details')
235
_global_option('show-ids',
236
help='show internal object ids')
237
_global_option('timezone',
239
help='display timezone as local, original, or utc')
240
_global_option('unbound')
241
_global_option('verbose',
242
help='display more information',
244
_global_option('version')
245
_global_option('email')
246
_global_option('update')
247
_global_option('log-format', type=str, help="Use this log format")
248
_global_option('long', help='Use detailed log format. Same as --log-format long',
250
_global_option('short', help='Use moderately short log format. Same as --log-format short')
251
_global_option('line', help='Use log format with one line per revision. Same as --log-format line')
252
_global_option('root', type=str)
253
_global_option('no-backup')
254
_global_option('merge-type', type=_parse_merge_type,
255
help='Select a particular merge algorithm')
256
_global_option('pattern', type=str)
257
_global_option('quiet', short_name='q')
258
_global_option('remember', help='Remember the specified location as a'
260
_global_option('reprocess', help='Reprocess to reduce spurious conflicts')
261
_global_option('kind', type=str)
262
_global_option('dry-run',
263
help="show what would be done, but don't actually do anything")
264
_global_option('name-from-revision', help='The path name in the old tree.')