145
# TODO: Some way to show in help a description of the option argument
145
# The dictionary of standard options. These are always legal.
148
# The dictionary of commonly used options. these are only legal
149
# if a command explicitly references them by name in the list
150
# of supported options.
149
153
def __init__(self, name, help='', type=None, argname=None,
150
short_name=None, param_name=None):
154
short_name=None, param_name=None, custom_callback=None):
151
155
"""Make a new command option.
153
name -- regular name of the command, used in the double-dash
157
:param name: regular name of the command, used in the double-dash
154
158
form and also as the parameter to the command's run()
155
159
method (unless param_name is specified).
157
help -- help message displayed in command help
161
:param help: help message displayed in command help
159
type -- function called to parse the option argument, or
163
:param type: function called to parse the option argument, or
160
164
None (default) if this option doesn't take an argument.
162
argname -- name of option argument, if any
164
param_name -- name of the parameter which will be passed to
166
:param argname: name of option argument, if any
168
:param short_name: short option code for use with a single -, e.g.
169
short_name="v" to enable parsing of -v.
171
:param param_name: name of the parameter which will be passed to
165
172
the command's run() method.
174
:param custom_callback: a callback routine to be called after normal
175
processing. The signature of the callback routine is
176
(option, name, new_value, parser).
198
210
option_strings.append('-%s' % short_name)
199
211
optargfn = self.type
200
212
if optargfn is None:
201
parser.add_option(action='store_true', dest=self.name,
213
parser.add_option(action='callback',
214
callback=self._optparse_bool_callback,
215
callback_args=(True,),
203
default=OptionParser.DEFAULT_VALUE,
205
218
negation_strings = ['--%s' % self.get_negation_name()]
206
parser.add_option(action='store_false', dest=self.name,
219
parser.add_option(action='callback',
220
callback=self._optparse_bool_callback,
221
callback_args=(False,),
207
222
help=optparse.SUPPRESS_HELP, *negation_strings)
209
224
parser.add_option(action='callback',
213
228
default=OptionParser.DEFAULT_VALUE,
231
def _optparse_bool_callback(self, option, opt_str, value, parser, bool_v):
232
setattr(parser.values, self._param_name, bool_v)
233
if self.custom_callback is not None:
234
self.custom_callback(option, self._param_name, bool_v, parser)
216
236
def _optparse_callback(self, option, opt, value, parser):
217
setattr(parser.values, self._param_name, self.type(value))
238
setattr(parser.values, self._param_name, v)
239
if self.custom_callback is not None:
240
self.custom_callback(option, self.name, v, parser)
219
242
def iter_switches(self):
220
243
"""Iterate through the list of switches provided by the option
255
278
def _optparse_callback(self, option, opt, value, parser):
256
values = getattr(parser.values, self.name)
279
values = getattr(parser.values, self._param_name)
260
283
values.append(self.type(value))
284
if self.custom_callback is not None:
285
self.custom_callback(option, self._param_name, values, parser)
263
288
class RegistryOption(Option):
344
369
def _optparse_value_callback(self, cb_value):
345
370
def cb(option, opt, value, parser):
346
setattr(parser.values, self.name, self.type(cb_value))
371
v = self.type(cb_value)
372
setattr(parser.values, self._param_name, v)
373
if self.custom_callback is not None:
374
self.custom_callback(option, self._param_name, v, parser)
349
377
def iter_switches(self):
413
def custom_help(name, help):
414
"""Clone a common option overriding the help."""
416
o = copy.copy(Option.OPTIONS[name])
421
def _standard_option(name, **kwargs):
422
"""Register a standard option."""
423
# All standard options are implicitly 'global' ones
424
Option.STD_OPTIONS[name] = Option(name, **kwargs)
425
Option.OPTIONS[name] = Option.STD_OPTIONS[name]
385
428
def _global_option(name, **kwargs):
386
"""Register o as a global option."""
429
"""Register a global option."""
387
430
Option.OPTIONS[name] = Option(name, **kwargs)
442
# This is the verbosity level detected during command line parsing.
443
# Note that the final value is dependent on the order in which the
444
# various flags (verbose, quiet, no-verbose, no-quiet) are given.
445
# The final value will be one of the following:
453
def _verbosity_level_callback(option, opt_str, value, parser):
454
global _verbosity_level
456
# Either --no-verbose or --no-quiet was specified
458
elif opt_str == "verbose":
459
if _verbosity_level > 0:
460
_verbosity_level += 1
464
if _verbosity_level < 0:
465
_verbosity_level -= 1
467
_verbosity_level = -1
399
470
_merge_type_registry = MergeTypeRegistry()
400
471
_merge_type_registry.register_lazy('merge3', 'bzrlib.merge', 'Merge3Merger',
401
472
"Native diff3-style merge")
404
475
_merge_type_registry.register_lazy('weave', 'bzrlib.merge', 'WeaveMerger',
405
476
"Weave-based merge")
478
# Declare the standard options
479
_standard_option('help', short_name='h',
480
help='Show help message.')
481
_standard_option('verbose', short_name='v',
482
help='Display more information.',
483
custom_callback=_verbosity_level_callback)
484
_standard_option('quiet', short_name='q',
485
help="Only display errors and warnings.",
486
custom_callback=_verbosity_level_callback)
488
# Declare commonly used options
407
489
_global_option('all')
408
490
_global_option('overwrite', help='Ignore differences between branches and '
409
491
'overwrite unconditionally.')
436
518
help='display timezone as local, original, or utc')
437
519
_global_option('unbound')
438
_global_option('verbose',
439
help='Display more information.',
441
520
_global_option('version')
442
521
_global_option('email')
443
522
_global_option('update')
454
533
_merge_type_registry, value_switches=True,
455
534
title='Merge algorithm')
456
535
_global_option('pattern', type=str)
457
_global_option('quiet', short_name='q')
458
536
_global_option('remember', help='Remember the specified location as a'
460
538
_global_option('reprocess', help='Reprocess to reduce spurious conflicts.')
462
540
_global_option('dry-run',
463
541
help="Show what would be done, but don't actually do anything.")
464
542
_global_option('name-from-revision', help='The path name in the old tree.')
466
_help_option = Option('help',
467
help='Show help message.',