1
# Copyright (C) 2004, 2005, 2006 by 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.
23
from bzrlib.trace import warning
24
from bzrlib.revisionspec import RevisionSpec
25
from bzrlib.errors import BzrCommandError
28
def _parse_revision_str(revstr):
29
"""This handles a revision string -> revno.
31
This always returns a list. The list will have one element for
32
each revision specifier supplied.
34
>>> _parse_revision_str('234')
35
[<RevisionSpec_revno 234>]
36
>>> _parse_revision_str('234..567')
37
[<RevisionSpec_revno 234>, <RevisionSpec_revno 567>]
38
>>> _parse_revision_str('..')
39
[<RevisionSpec None>, <RevisionSpec None>]
40
>>> _parse_revision_str('..234')
41
[<RevisionSpec None>, <RevisionSpec_revno 234>]
42
>>> _parse_revision_str('234..')
43
[<RevisionSpec_revno 234>, <RevisionSpec None>]
44
>>> _parse_revision_str('234..456..789') # Maybe this should be an error
45
[<RevisionSpec_revno 234>, <RevisionSpec_revno 456>, <RevisionSpec_revno 789>]
46
>>> _parse_revision_str('234....789') #Error ?
47
[<RevisionSpec_revno 234>, <RevisionSpec None>, <RevisionSpec_revno 789>]
48
>>> _parse_revision_str('revid:test@other.com-234234')
49
[<RevisionSpec_revid revid:test@other.com-234234>]
50
>>> _parse_revision_str('revid:test@other.com-234234..revid:test@other.com-234235')
51
[<RevisionSpec_revid revid:test@other.com-234234>, <RevisionSpec_revid revid:test@other.com-234235>]
52
>>> _parse_revision_str('revid:test@other.com-234234..23')
53
[<RevisionSpec_revid revid:test@other.com-234234>, <RevisionSpec_revno 23>]
54
>>> _parse_revision_str('date:2005-04-12')
55
[<RevisionSpec_date date:2005-04-12>]
56
>>> _parse_revision_str('date:2005-04-12 12:24:33')
57
[<RevisionSpec_date date:2005-04-12 12:24:33>]
58
>>> _parse_revision_str('date:2005-04-12T12:24:33')
59
[<RevisionSpec_date date:2005-04-12T12:24:33>]
60
>>> _parse_revision_str('date:2005-04-12,12:24:33')
61
[<RevisionSpec_date date:2005-04-12,12:24:33>]
62
>>> _parse_revision_str('-5..23')
63
[<RevisionSpec_revno -5>, <RevisionSpec_revno 23>]
64
>>> _parse_revision_str('-5')
65
[<RevisionSpec_revno -5>]
66
>>> _parse_revision_str('123a')
67
Traceback (most recent call last):
69
NoSuchRevisionSpec: No namespace registered for string: '123a'
70
>>> _parse_revision_str('abc')
71
Traceback (most recent call last):
73
NoSuchRevisionSpec: No namespace registered for string: 'abc'
74
>>> _parse_revision_str('branch:../branch2')
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>]
81
# TODO: Maybe move this into revisionspec.py
83
# split on the first .. that is not followed by a / ?
84
sep = re.compile("\\.\\.(?!/)")
85
for x in sep.split(revstr):
86
revs.append(RevisionSpec.from_string(x or None))
90
def _parse_merge_type(typestring):
91
return get_merge_type(typestring)
93
def get_merge_type(typestring):
94
"""Attempt to find the merge class/factory associated with a string."""
95
from merge import merge_types
97
return merge_types[typestring][0]
99
templ = '%s%%7s: %%s' % (' '*12)
100
lines = [templ % (f[0], f[1][1]) for f in merge_types.iteritems()]
101
type_list = '\n'.join(lines)
102
msg = "No known merge type %s. Supported types are:\n%s" %\
103
(typestring, type_list)
104
raise BzrCommandError(msg)
106
class Option(object):
107
"""Description of a command line option"""
108
# TODO: Some way to show in help a description of the option argument
113
def __init__(self, name, help='', type=None, argname=None):
114
"""Make a new command option.
116
name -- regular name of the command, used in the double-dash
117
form and also as the parameter to the command's run()
120
help -- help message displayed in command help
122
type -- function called to parse the option argument, or
123
None (default) if this option doesn't take an argument.
125
argname -- name of option argument, if any
127
# TODO: perhaps a subclass that automatically does
128
# --option, --no-option for reversible booleans
133
assert argname is None
134
elif argname is None:
136
self.argname = argname
138
def short_name(self):
139
"""Return the single character option for this command, if any.
141
Short options are globally registered.
143
for short, option in Option.SHORT_OPTIONS.iteritems():
147
def get_negation_name(self):
148
if self.name.startswith('no-'):
151
return 'no-' + self.name
153
def add_option(self, parser, short_name):
154
"""Add this option to an Optparse parser"""
155
option_strings = ['--%s' % self.name]
156
if short_name is not None:
157
option_strings.append('-%s' % short_name)
160
parser.add_option(action='store_true', dest=self.name,
162
default=OptionParser.DEFAULT_VALUE,
164
negation_strings = ['--%s' % self.get_negation_name()]
165
parser.add_option(action='store_false', dest=self.name,
166
help=optparse.SUPPRESS_HELP, *negation_strings)
168
parser.add_option(action='callback',
169
callback=self._optparse_callback,
170
type='string', metavar=self.argname.upper(),
172
default=OptionParser.DEFAULT_VALUE,
175
def _optparse_callback(self, option, opt, value, parser):
176
setattr(parser.values, self.name, self.type(value))
178
def iter_switches(self):
179
"""Iterate through the list of switches provided by the option
181
:return: an iterator of (name, short_name, argname, help)
183
argname = self.argname
184
if argname is not None:
185
argname = argname.upper()
186
yield self.name, self.short_name(), argname, self.help
189
class OptionParser(optparse.OptionParser):
190
"""OptionParser that raises exceptions instead of exiting"""
192
DEFAULT_VALUE = object()
194
def error(self, message):
195
raise BzrCommandError(message)
198
def get_optparser(options):
199
"""Generate an optparse parser for bzrlib-style options"""
201
parser = OptionParser()
202
parser.remove_option('--help')
203
short_options = dict((k.name, v) for v, k in
204
Option.SHORT_OPTIONS.iteritems())
205
for option in options.itervalues():
206
option.add_option(parser, short_options.get(option.name))
210
def _global_option(name, **kwargs):
211
"""Register o as a global option."""
212
Option.OPTIONS[name] = Option(name, **kwargs)
214
_global_option('all')
215
_global_option('overwrite', help='Ignore differences between branches and '
216
'overwrite unconditionally')
217
_global_option('basis', type=str)
218
_global_option('bound')
219
_global_option('diff-options', type=str)
220
_global_option('help',
221
help='show help message')
222
_global_option('file', type=unicode)
223
_global_option('force')
224
_global_option('format', type=unicode)
225
_global_option('forward')
226
_global_option('message', type=unicode)
227
_global_option('no-recurse')
228
_global_option('prefix', type=str,
229
help='Set prefixes to added to old and new filenames, as '
230
'two values separated by a colon.')
231
_global_option('profile',
232
help='show performance profiling information')
233
_global_option('revision', type=_parse_revision_str)
234
_global_option('show-ids',
235
help='show internal object ids')
236
_global_option('timezone',
238
help='display timezone as local, original, or utc')
239
_global_option('unbound')
240
_global_option('verbose',
241
help='display more information')
242
_global_option('version')
243
_global_option('email')
244
_global_option('update')
245
_global_option('log-format', type=str, help="Use this log format")
246
_global_option('long', help='Use detailed log format. Same as --log-format long')
247
_global_option('short', help='Use moderately short log format. Same as --log-format short')
248
_global_option('line', help='Use log format with one line per revision. Same as --log-format line')
249
_global_option('root', type=str)
250
_global_option('no-backup')
251
_global_option('merge-type', type=_parse_merge_type,
252
help='Select a particular merge algorithm')
253
_global_option('pattern', type=str)
254
_global_option('quiet')
255
_global_option('remember', help='Remember the specified location as a'
257
_global_option('reprocess', help='Reprocess to reduce spurious conflicts')
258
_global_option('kind', type=str)
259
_global_option('dry-run',
260
help="show what would be done, but don't actually do anything")
263
def _global_short(short_name, long_name):
264
assert short_name not in Option.SHORT_OPTIONS
265
Option.SHORT_OPTIONS[short_name] = Option.OPTIONS[long_name]
268
Option.SHORT_OPTIONS['F'] = Option.OPTIONS['file']
269
Option.SHORT_OPTIONS['h'] = Option.OPTIONS['help']
270
Option.SHORT_OPTIONS['m'] = Option.OPTIONS['message']
271
Option.SHORT_OPTIONS['r'] = Option.OPTIONS['revision']
272
Option.SHORT_OPTIONS['v'] = Option.OPTIONS['verbose']
273
Option.SHORT_OPTIONS['l'] = Option.OPTIONS['long']
274
Option.SHORT_OPTIONS['q'] = Option.OPTIONS['quiet']
275
Option.SHORT_OPTIONS['p'] = Option.OPTIONS['prefix']