85
86
[<RevisionSpec_branch branch:../../branch2>]
86
87
>>> _parse_revision_str('branch:../../branch2..23')
87
88
[<RevisionSpec_branch branch:../../branch2>, <RevisionSpec_revno 23>]
88
>>> _parse_revision_str('branch:..\\\\branch2')
89
[<RevisionSpec_branch branch:..\\branch2>]
90
>>> _parse_revision_str('branch:..\\\\..\\\\branch2..23')
91
[<RevisionSpec_branch branch:..\\..\\branch2>, <RevisionSpec_revno 23>]
93
90
# TODO: Maybe move this into revisionspec.py
95
# split on .. that is not followed by a / or \
96
sep = re.compile(r'\.\.(?![\\/])')
92
# split on the first .. that is not followed by a / ?
93
sep = re.compile("\\.\\.(?!/)")
97
94
for x in sep.split(revstr):
98
95
revs.append(revisionspec.RevisionSpec.from_string(x or None))
102
def _parse_change_str(revstr):
103
"""Parse the revision string and return a tuple with left-most
104
parent of the revision.
106
>>> _parse_change_str('123')
107
(<RevisionSpec_before before:123>, <RevisionSpec_revno 123>)
108
>>> _parse_change_str('123..124')
109
Traceback (most recent call last):
111
RangeInChangeOption: Option --change does not accept revision ranges
113
revs = _parse_revision_str(revstr)
115
raise errors.RangeInChangeOption()
116
return (revisionspec.RevisionSpec.from_string('before:' + revstr),
120
99
def _parse_merge_type(typestring):
121
100
return get_merge_type(typestring)
144
# The dictionary of standard options. These are always legal.
123
# TODO: Some way to show in help a description of the option argument
147
# The dictionary of commonly used options. these are only legal
148
# if a command explicitly references them by name in the list
149
# of supported options.
152
127
def __init__(self, name, help='', type=None, argname=None,
153
short_name=None, param_name=None, custom_callback=None):
154
129
"""Make a new command option.
156
:param name: regular name of the command, used in the double-dash
131
name -- regular name of the command, used in the double-dash
157
132
form and also as the parameter to the command's run()
158
method (unless param_name is specified).
160
:param help: help message displayed in command help
162
:param type: function called to parse the option argument, or
135
help -- help message displayed in command help
137
type -- function called to parse the option argument, or
163
138
None (default) if this option doesn't take an argument.
165
:param argname: name of option argument, if any
167
:param short_name: short option code for use with a single -, e.g.
168
short_name="v" to enable parsing of -v.
170
:param param_name: name of the parameter which will be passed to
171
the command's run() method.
173
:param custom_callback: a callback routine to be called after normal
174
processing. The signature of the callback routine is
175
(option, name, new_value, parser).
140
argname -- name of option argument, if any
183
148
elif argname is None:
185
150
self.argname = argname
186
if param_name is None:
187
self._param_name = self.name
189
self._param_name = param_name
190
self.custom_callback = custom_callback
192
152
def short_name(self):
193
153
if self._short_name:
194
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):
196
163
def set_short_name(self, short_name):
197
164
self._short_name = short_name
209
176
option_strings.append('-%s' % short_name)
210
177
optargfn = self.type
211
178
if optargfn is None:
212
parser.add_option(action='callback',
213
callback=self._optparse_bool_callback,
214
callback_args=(True,),
179
parser.add_option(action='store_true', dest=self.name,
181
default=OptionParser.DEFAULT_VALUE,
217
183
negation_strings = ['--%s' % self.get_negation_name()]
218
parser.add_option(action='callback',
219
callback=self._optparse_bool_callback,
220
callback_args=(False,),
184
parser.add_option(action='store_false', dest=self.name,
221
185
help=optparse.SUPPRESS_HELP, *negation_strings)
223
187
parser.add_option(action='callback',
227
191
default=OptionParser.DEFAULT_VALUE,
230
def _optparse_bool_callback(self, option, opt_str, value, parser, bool_v):
231
setattr(parser.values, self._param_name, bool_v)
232
if self.custom_callback is not None:
233
self.custom_callback(option, self._param_name, bool_v, parser)
235
194
def _optparse_callback(self, option, opt, value, parser):
237
setattr(parser.values, self._param_name, v)
238
if self.custom_callback is not None:
239
self.custom_callback(option, self.name, v, parser)
195
setattr(parser.values, self.name, self.type(value))
241
197
def iter_switches(self):
242
198
"""Iterate through the list of switches provided by the option
277
230
def _optparse_callback(self, option, opt, value, parser):
278
values = getattr(parser.values, self._param_name)
231
values = getattr(parser.values, self.name)
282
235
values.append(self.type(value))
283
if self.custom_callback is not None:
284
self.custom_callback(option, self._param_name, values, parser)
287
238
class RegistryOption(Option):
341
292
as values for the option, and they value is treated as the help.
343
294
reg = registry.Registry()
344
for name, switch_help in kwargs.iteritems():
295
for name, help in kwargs.iteritems():
345
296
name = name.replace('_', '-')
346
reg.register(name, name, help=switch_help)
297
reg.register(name, name, help=help)
347
298
return RegistryOption(name_, help, reg, title=title,
348
299
value_switches=value_switches, enum_switch=enum_switch)
368
319
def _optparse_value_callback(self, cb_value):
369
320
def cb(option, opt, value, parser):
370
v = self.type(cb_value)
371
setattr(parser.values, self._param_name, v)
372
if self.custom_callback is not None:
373
self.custom_callback(option, self._param_name, v, parser)
321
setattr(parser.values, self.name, self.type(cb_value))
376
324
def iter_switches(self):
384
332
for key in sorted(self.registry.keys()):
385
333
yield key, None, None, self.registry.get_help(key)
387
def is_hidden(self, name):
388
if name == self.name:
389
return Option.is_hidden(self, name)
390
return getattr(self.registry.get_info(name), 'hidden', False)
393
336
class OptionParser(optparse.OptionParser):
394
337
"""OptionParser that raises exceptions instead of exiting"""
412
def custom_help(name, help):
413
"""Clone a common option overriding the help."""
415
o = copy.copy(Option.OPTIONS[name])
420
def _standard_option(name, **kwargs):
421
"""Register a standard option."""
422
# All standard options are implicitly 'global' ones
423
Option.STD_OPTIONS[name] = Option(name, **kwargs)
424
Option.OPTIONS[name] = Option.STD_OPTIONS[name]
427
355
def _global_option(name, **kwargs):
428
"""Register a global option."""
356
"""Register o as a global option."""
429
357
Option.OPTIONS[name] = Option(name, **kwargs)
441
# This is the verbosity level detected during command line parsing.
442
# Note that the final value is dependent on the order in which the
443
# various flags (verbose, quiet, no-verbose, no-quiet) are given.
444
# The final value will be one of the following:
452
def _verbosity_level_callback(option, opt_str, value, parser):
453
global _verbosity_level
455
# Either --no-verbose or --no-quiet was specified
457
elif opt_str == "verbose":
458
if _verbosity_level > 0:
459
_verbosity_level += 1
463
if _verbosity_level < 0:
464
_verbosity_level -= 1
466
_verbosity_level = -1
469
369
_merge_type_registry = MergeTypeRegistry()
470
370
_merge_type_registry.register_lazy('merge3', 'bzrlib.merge', 'Merge3Merger',
471
371
"Native diff3-style merge")
473
373
"Merge using external diff3")
474
374
_merge_type_registry.register_lazy('weave', 'bzrlib.merge', 'WeaveMerger',
475
375
"Weave-based merge")
476
_merge_type_registry.register_lazy('lca', 'bzrlib.merge', 'LCAMerger',
479
# Declare the standard options
480
_standard_option('help', short_name='h',
481
help='Show help message.')
482
_standard_option('verbose', short_name='v',
483
help='Display more information.',
484
custom_callback=_verbosity_level_callback)
485
_standard_option('quiet', short_name='q',
486
help="Only display errors and warnings.",
487
custom_callback=_verbosity_level_callback)
489
# Declare commonly used options
490
377
_global_option('all')
491
378
_global_option('overwrite', help='Ignore differences between branches and '
492
'overwrite unconditionally.')
379
'overwrite unconditionally')
493
380
_global_option('basis', type=str)
494
381
_global_option('bound')
495
382
_global_option('diff-options', type=str)
383
_global_option('help',
384
help='show help message',
496
386
_global_option('file', type=unicode, short_name='F')
497
387
_global_option('force')
498
388
_global_option('format', type=unicode)
499
389
_global_option('forward')
500
390
_global_option('message', type=unicode,
502
help='Message string.')
503
392
_global_option('no-recurse')
504
393
_global_option('profile',
505
help='Show performance profiling information.')
394
help='show performance profiling information')
506
395
_global_option('revision',
507
396
type=_parse_revision_str,
509
help='See "help revisionspec" for details.')
510
_global_option('change',
511
type=_parse_change_str,
513
param_name='revision',
514
help='Select changes introduced by the specified revision. See also "help revisionspec".')
515
_global_option('show-ids',
516
help='Show internal object ids.')
398
help='See \'help revisionspec\' for details')
399
_global_option('show-ids',
400
help='show internal object ids')
517
401
_global_option('timezone',
519
403
help='display timezone as local, original, or utc')
520
404
_global_option('unbound')
405
_global_option('verbose',
406
help='display more information',
521
408
_global_option('version')
522
409
_global_option('email')
523
410
_global_option('update')
524
_global_registry_option('log-format', "Use specified log format.",
411
_global_registry_option('log-format', "Use this log format",
525
412
log.log_formatter_registry, value_switches=True,
526
413
title='Log format')
527
414
_global_option('long', help='Use detailed log format. Same as --log-format long',
530
417
_global_option('line', help='Use log format with one line per revision. Same as --log-format line')
531
418
_global_option('root', type=str)
532
419
_global_option('no-backup')
533
_global_registry_option('merge-type', 'Select a particular merge algorithm.',
420
_global_registry_option('merge-type', 'Select a particular merge algorithm',
534
421
_merge_type_registry, value_switches=True,
535
422
title='Merge algorithm')
536
423
_global_option('pattern', type=str)
424
_global_option('quiet', short_name='q')
537
425
_global_option('remember', help='Remember the specified location as a'
539
_global_option('reprocess', help='Reprocess to reduce spurious conflicts.')
427
_global_option('reprocess', help='Reprocess to reduce spurious conflicts')
540
428
_global_option('kind', type=str)
541
429
_global_option('dry-run',
542
help="Show what would be done, but don't actually do anything.")
430
help="show what would be done, but don't actually do anything")
543
431
_global_option('name-from-revision', help='The path name in the old tree.')
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.',