184
202
argname = self.argname
185
203
if argname is not None:
186
204
argname = argname.upper()
187
yield self.name, self.short_name, argname, self.help
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)
190
336
class OptionParser(optparse.OptionParser):
210
356
"""Register o as a global option."""
211
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',
213
377
_global_option('all')
214
378
_global_option('overwrite', help='Ignore differences between branches and '
215
379
'overwrite unconditionally')
244
408
_global_option('version')
245
409
_global_option('email')
246
410
_global_option('update')
247
_global_option('log-format', type=str, help="Use this log format")
411
_global_registry_option('log-format', "Use this log format",
412
log.log_formatter_registry, value_switches=True,
248
414
_global_option('long', help='Use detailed log format. Same as --log-format long',
250
416
_global_option('short', help='Use moderately short log format. Same as --log-format short')
251
417
_global_option('line', help='Use log format with one line per revision. Same as --log-format line')
252
418
_global_option('root', type=str)
253
419
_global_option('no-backup')
254
_global_option('merge-type', type=_parse_merge_type,
255
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')
256
423
_global_option('pattern', type=str)
257
424
_global_option('quiet', short_name='q')
258
425
_global_option('remember', help='Remember the specified location as a'