112
108
(typestring, type_list)
113
109
raise errors.BzrCommandError(msg)
116
111
class Option(object):
117
"""Description of a command line option
119
:ivar _short_name: If this option has a single-letter name, this is it.
112
"""Description of a command line option"""
123
113
# TODO: Some way to show in help a description of the option argument
127
def __init__(self, name, help='', type=None, argname=None,
118
def __init__(self, name, help='', type=None, argname=None):
129
119
"""Make a new command option.
131
121
name -- regular name of the command, used in the double-dash
150
141
self.argname = argname
152
143
def short_name(self):
154
return self._short_name
156
# remove this when SHORT_OPTIONS is removed
157
# XXX: This is accessing a DeprecatedDict, so we call the super
158
# method to avoid warnings
159
for (k, v) in dict.iteritems(Option.SHORT_OPTIONS):
144
"""Return the single character option for this command, if any.
163
def set_short_name(self, short_name):
164
self._short_name = short_name
146
Short options are globally registered.
148
for short, option in Option.SHORT_OPTIONS.iteritems():
166
152
def get_negation_name(self):
167
153
if self.name.startswith('no-'):
205
191
yield self.name, self.short_name(), argname, self.help
208
class RegistryOption(Option):
209
"""Option based on a registry
211
The values for the options correspond to entries in the registry. Input
212
must be a registry key. After validation, it is converted into an object
213
using Registry.get or a caller-provided converter.
216
def validate_value(self, value):
217
"""Validate a value name"""
218
if value not in self.registry:
219
raise errors.BadOptionValue(self.name, value)
221
def convert(self, value):
222
"""Convert a value name into an output type"""
223
self.validate_value(value)
224
if self.converter is None:
225
return self.registry.get(value)
227
return self.converter(value)
229
def __init__(self, name, help, registry, converter=None,
230
value_switches=False, title=None):
234
:param name: The option name.
235
:param help: Help for the option.
236
:param registry: A Registry containing the values
237
:param converter: Callable to invoke with the value name to produce
238
the value. If not supplied, self.registry.get is used.
239
:param value_switches: If true, each possible value is assigned its
240
own switch. For example, instead of '--format knit',
241
'--knit' can be used interchangeably.
243
Option.__init__(self, name, help, type=self.convert)
244
self.registry = registry
246
self.converter = converter
247
self.value_switches = value_switches
249
if self.title is None:
252
def add_option(self, parser, short_name):
253
"""Add this option to an Optparse parser"""
254
if self.value_switches:
255
parser = parser.add_option_group(self.title)
256
Option.add_option(self, parser, short_name)
257
if self.value_switches:
258
for key in self.registry.keys():
259
option_strings = ['--%s' % key]
260
parser.add_option(action='callback',
261
callback=self._optparse_value_callback(key),
262
help=self.registry.get_help(key),
265
def _optparse_value_callback(self, cb_value):
266
def cb(option, opt, value, parser):
267
setattr(parser.values, self.name, self.type(cb_value))
270
def iter_switches(self):
271
"""Iterate through the list of switches provided by the option
273
:return: an iterator of (name, short_name, argname, help)
275
for value in Option.iter_switches(self):
277
if self.value_switches:
278
for key in sorted(self.registry.keys()):
279
yield key, None, None, self.registry.get_help(key)
282
194
class OptionParser(optparse.OptionParser):
283
195
"""OptionParser that raises exceptions instead of exiting"""
302
216
"""Register o as a global option."""
303
217
Option.OPTIONS[name] = Option(name, **kwargs)
306
def _global_registry_option(name, help, registry, **kwargs):
307
Option.OPTIONS[name] = RegistryOption(name, help, registry, **kwargs)
310
class MergeTypeRegistry(registry.Registry):
315
_merge_type_registry = MergeTypeRegistry()
316
_merge_type_registry.register_lazy('merge3', 'bzrlib.merge', 'Merge3Merger',
317
"Native diff3-style merge")
318
_merge_type_registry.register_lazy('diff3', 'bzrlib.merge', 'Diff3Merger',
319
"Merge using external diff3")
320
_merge_type_registry.register_lazy('weave', 'bzrlib.merge', 'WeaveMerger',
323
219
_global_option('all')
324
220
_global_option('overwrite', help='Ignore differences between branches and '
325
221
'overwrite unconditionally')
327
223
_global_option('bound')
328
224
_global_option('diff-options', type=str)
329
225
_global_option('help',
330
help='show help message',
332
_global_option('file', type=unicode, short_name='F')
226
help='show help message')
227
_global_option('file', type=unicode)
333
228
_global_option('force')
334
229
_global_option('format', type=unicode)
335
230
_global_option('forward')
336
_global_option('message', type=unicode,
231
_global_option('message', type=unicode)
338
232
_global_option('no-recurse')
233
_global_option('prefix', type=str,
234
help='Set prefixes to added to old and new filenames, as '
235
'two values separated by a colon.')
339
236
_global_option('profile',
340
237
help='show performance profiling information')
341
_global_option('revision',
342
type=_parse_revision_str,
344
help='See \'help revisionspec\' for details')
238
_global_option('revision', type=_parse_revision_str)
345
239
_global_option('show-ids',
346
240
help='show internal object ids')
347
241
_global_option('timezone',
349
243
help='display timezone as local, original, or utc')
350
244
_global_option('unbound')
351
245
_global_option('verbose',
352
help='display more information',
246
help='display more information')
354
247
_global_option('version')
355
248
_global_option('email')
356
249
_global_option('update')
357
_global_registry_option('log-format', "Use this log format",
358
log.log_formatter_registry, value_switches=True,
360
_global_option('long', help='Use detailed log format. Same as --log-format long',
250
_global_option('log-format', type=str, help="Use this log format")
251
_global_option('long', help='Use detailed log format. Same as --log-format long')
362
252
_global_option('short', help='Use moderately short log format. Same as --log-format short')
363
253
_global_option('line', help='Use log format with one line per revision. Same as --log-format line')
364
254
_global_option('root', type=str)
365
255
_global_option('no-backup')
366
_global_registry_option('merge-type', 'Select a particular merge algorithm',
367
_merge_type_registry, value_switches=True,
368
title='Merge algorithm')
256
_global_option('merge-type', type=_parse_merge_type,
257
help='Select a particular merge algorithm')
369
258
_global_option('pattern', type=str)
370
_global_option('quiet', short_name='q')
259
_global_option('quiet')
371
260
_global_option('remember', help='Remember the specified location as a'
373
262
_global_option('reprocess', help='Reprocess to reduce spurious conflicts')
374
263
_global_option('kind', type=str)
375
264
_global_option('dry-run',
376
265
help="show what would be done, but don't actually do anything")
377
_global_option('name-from-revision', help='The path name in the old tree.')
380
# prior to 0.14 these were always globally registered; the old dict is
381
# available for plugins that use it but it should not be used.
382
Option.SHORT_OPTIONS = symbol_versioning.DeprecatedDict(
383
symbol_versioning.zero_fourteen,
386
'F': Option.OPTIONS['file'],
387
'h': Option.OPTIONS['help'],
388
'm': Option.OPTIONS['message'],
389
'r': Option.OPTIONS['revision'],
390
'v': Option.OPTIONS['verbose'],
391
'l': Option.OPTIONS['long'],
392
'q': Option.OPTIONS['quiet'],
394
'Set the short option name when constructing the Option.',
268
def _global_short(short_name, long_name):
269
assert short_name not in Option.SHORT_OPTIONS
270
Option.SHORT_OPTIONS[short_name] = Option.OPTIONS[long_name]
273
Option.SHORT_OPTIONS['F'] = Option.OPTIONS['file']
274
Option.SHORT_OPTIONS['h'] = Option.OPTIONS['help']
275
Option.SHORT_OPTIONS['m'] = Option.OPTIONS['message']
276
Option.SHORT_OPTIONS['r'] = Option.OPTIONS['revision']
277
Option.SHORT_OPTIONS['v'] = Option.OPTIONS['verbose']
278
Option.SHORT_OPTIONS['l'] = Option.OPTIONS['long']
279
Option.SHORT_OPTIONS['q'] = Option.OPTIONS['quiet']
280
Option.SHORT_OPTIONS['p'] = Option.OPTIONS['prefix']