192
205
yield self.name, self.short_name(), argname, self.help
208
class ListOption(Option):
209
"""Option used to provide a list of values.
211
On the command line, arguments are specified by a repeated use of the
212
option. '-' is a special argument that resets the list. For example,
214
sets the value of the 'foo' option to ['a', 'b'], and
215
--foo=a --foo=b --foo=- --foo=c
216
sets the value of the 'foo' option to ['c'].
219
def add_option(self, parser, short_name):
220
"""Add this option to an Optparse parser."""
221
option_strings = ['--%s' % self.name]
222
if short_name is not None:
223
option_strings.append('-%s' % short_name)
224
parser.add_option(action='callback',
225
callback=self._optparse_callback,
226
type='string', metavar=self.argname.upper(),
227
help=self.help, default=[],
230
def _optparse_callback(self, option, opt, value, parser):
231
values = getattr(parser.values, self.name)
235
values.append(self.type(value))
238
class RegistryOption(Option):
239
"""Option based on a registry
241
The values for the options correspond to entries in the registry. Input
242
must be a registry key. After validation, it is converted into an object
243
using Registry.get or a caller-provided converter.
246
def validate_value(self, value):
247
"""Validate a value name"""
248
if value not in self.registry:
249
raise errors.BadOptionValue(self.name, value)
251
def convert(self, value):
252
"""Convert a value name into an output type"""
253
self.validate_value(value)
254
if self.converter is None:
255
return self.registry.get(value)
257
return self.converter(value)
259
def __init__(self, name, help, registry, converter=None,
260
value_switches=False, title=None, enum_switch=True):
264
:param name: The option name.
265
:param help: Help for the option.
266
:param registry: A Registry containing the values
267
:param converter: Callable to invoke with the value name to produce
268
the value. If not supplied, self.registry.get is used.
269
:param value_switches: If true, each possible value is assigned its
270
own switch. For example, instead of '--format knit',
271
'--knit' can be used interchangeably.
272
:param enum_switch: If true, a switch is provided with the option name,
275
Option.__init__(self, name, help, type=self.convert)
276
self.registry = registry
278
self.converter = converter
279
self.value_switches = value_switches
280
self.enum_switch = enum_switch
282
if self.title is None:
286
def from_kwargs(name_, help=None, title=None, value_switches=False,
287
enum_switch=True, **kwargs):
288
"""Convenience method to generate string-map registry options
290
name, help, value_switches and enum_switch are passed to the
291
RegistryOption constructor. Any other keyword arguments are treated
292
as values for the option, and they value is treated as the help.
294
reg = registry.Registry()
295
for name, help in kwargs.iteritems():
296
name = name.replace('_', '-')
297
reg.register(name, name, help=help)
298
return RegistryOption(name_, help, reg, title=title,
299
value_switches=value_switches, enum_switch=enum_switch)
301
def add_option(self, parser, short_name):
302
"""Add this option to an Optparse parser"""
303
if self.value_switches:
304
parser = parser.add_option_group(self.title)
306
Option.add_option(self, parser, short_name)
307
if self.value_switches:
308
for key in self.registry.keys():
309
option_strings = ['--%s' % key]
310
if getattr(self.registry.get_info(key), 'hidden', False):
311
help = optparse.SUPPRESS_HELP
313
help = self.registry.get_help(key)
314
parser.add_option(action='callback',
315
callback=self._optparse_value_callback(key),
319
def _optparse_value_callback(self, cb_value):
320
def cb(option, opt, value, parser):
321
setattr(parser.values, self.name, self.type(cb_value))
324
def iter_switches(self):
325
"""Iterate through the list of switches provided by the option
327
:return: an iterator of (name, short_name, argname, help)
329
for value in Option.iter_switches(self):
331
if self.value_switches:
332
for key in sorted(self.registry.keys()):
333
yield key, None, None, self.registry.get_help(key)
195
336
class OptionParser(optparse.OptionParser):
196
337
"""OptionParser that raises exceptions instead of exiting"""
217
356
"""Register o as a global option."""
218
357
Option.OPTIONS[name] = Option(name, **kwargs)
360
def _global_registry_option(name, help, registry, **kwargs):
361
Option.OPTIONS[name] = RegistryOption(name, help, registry, **kwargs)
364
class MergeTypeRegistry(registry.Registry):
369
_merge_type_registry = MergeTypeRegistry()
370
_merge_type_registry.register_lazy('merge3', 'bzrlib.merge', 'Merge3Merger',
371
"Native diff3-style merge")
372
_merge_type_registry.register_lazy('diff3', 'bzrlib.merge', 'Diff3Merger',
373
"Merge using external diff3")
374
_merge_type_registry.register_lazy('weave', 'bzrlib.merge', 'WeaveMerger',
220
377
_global_option('all')
221
378
_global_option('overwrite', help='Ignore differences between branches and '
222
379
'overwrite unconditionally')
224
381
_global_option('bound')
225
382
_global_option('diff-options', type=str)
226
383
_global_option('help',
227
help='show help message')
228
_global_option('file', type=unicode)
384
help='show help message',
386
_global_option('file', type=unicode, short_name='F')
229
387
_global_option('force')
230
388
_global_option('format', type=unicode)
231
389
_global_option('forward')
232
_global_option('message', type=unicode)
390
_global_option('message', type=unicode,
233
392
_global_option('no-recurse')
234
_global_option('prefix', type=str,
235
help='Set prefixes to added to old and new filenames, as '
236
'two values separated by a colon.')
237
393
_global_option('profile',
238
394
help='show performance profiling information')
239
_global_option('revision', type=_parse_revision_str)
395
_global_option('revision',
396
type=_parse_revision_str,
398
help='See \'help revisionspec\' for details')
240
399
_global_option('show-ids',
241
400
help='show internal object ids')
242
401
_global_option('timezone',
244
403
help='display timezone as local, original, or utc')
245
404
_global_option('unbound')
246
405
_global_option('verbose',
247
help='display more information')
406
help='display more information',
248
408
_global_option('version')
249
409
_global_option('email')
250
410
_global_option('update')
251
_global_option('log-format', type=str, help="Use this log format")
252
_global_option('long', help='Use detailed log format. Same as --log-format long')
411
_global_registry_option('log-format', "Use this log format",
412
log.log_formatter_registry, value_switches=True,
414
_global_option('long', help='Use detailed log format. Same as --log-format long',
253
416
_global_option('short', help='Use moderately short log format. Same as --log-format short')
254
417
_global_option('line', help='Use log format with one line per revision. Same as --log-format line')
255
418
_global_option('root', type=str)
256
419
_global_option('no-backup')
257
_global_option('merge-type', type=_parse_merge_type,
258
help='Select a particular merge algorithm')
420
_global_registry_option('merge-type', 'Select a particular merge algorithm',
421
_merge_type_registry, value_switches=True,
422
title='Merge algorithm')
259
423
_global_option('pattern', type=str)
260
_global_option('quiet')
424
_global_option('quiet', short_name='q')
261
425
_global_option('remember', help='Remember the specified location as a'
263
427
_global_option('reprocess', help='Reprocess to reduce spurious conflicts')
267
431
_global_option('name-from-revision', help='The path name in the old tree.')
270
def _global_short(short_name, long_name):
271
assert short_name not in Option.SHORT_OPTIONS
272
Option.SHORT_OPTIONS[short_name] = Option.OPTIONS[long_name]
275
Option.SHORT_OPTIONS['F'] = Option.OPTIONS['file']
276
Option.SHORT_OPTIONS['h'] = Option.OPTIONS['help']
277
Option.SHORT_OPTIONS['m'] = Option.OPTIONS['message']
278
Option.SHORT_OPTIONS['r'] = Option.OPTIONS['revision']
279
Option.SHORT_OPTIONS['v'] = Option.OPTIONS['verbose']
280
Option.SHORT_OPTIONS['l'] = Option.OPTIONS['long']
281
Option.SHORT_OPTIONS['q'] = Option.OPTIONS['quiet']
282
Option.SHORT_OPTIONS['p'] = Option.OPTIONS['prefix']
434
# prior to 0.14 these were always globally registered; the old dict is
435
# available for plugins that use it but it should not be used.
436
Option.SHORT_OPTIONS = symbol_versioning.DeprecatedDict(
437
symbol_versioning.zero_fourteen,
440
'F': Option.OPTIONS['file'],
441
'h': Option.OPTIONS['help'],
442
'm': Option.OPTIONS['message'],
443
'r': Option.OPTIONS['revision'],
444
'v': Option.OPTIONS['verbose'],
445
'l': Option.OPTIONS['long'],
446
'q': Option.OPTIONS['quiet'],
448
'Set the short option name when constructing the Option.',