127
# TODO: Some way to show in help a description of the option argument
127
# The dictionary of standard options. These are always legal.
130
# The dictionary of commonly used options. these are only legal
131
# if a command explicitly references them by name in the list
132
# of supported options.
131
135
def __init__(self, name, help='', type=None, argname=None,
136
short_name=None, custom_callback=None):
133
137
"""Make a new command option.
135
name -- regular name of the command, used in the double-dash
139
:param name: regular name of the command, used in the double-dash
136
140
form and also as the parameter to the command's run()
139
help -- help message displayed in command help
143
:param help: help message displayed in command help
141
type -- function called to parse the option argument, or
145
:param type: function called to parse the option argument, or
142
146
None (default) if this option doesn't take an argument.
144
argname -- name of option argument, if any
148
:param argname: name of option argument, if any
150
:param short_name: short option code for use with a single -, e.g.
151
short_name="v" to enable parsing of -v.
153
:param custom_callback: a callback routine to be called after normal
154
processing. The signature of the callback routine is the same
155
as that for normal option callbacks. See optparse in the standard
156
Python library for details.
173
186
option_strings.append('-%s' % short_name)
174
187
optargfn = self.type
175
188
if optargfn is None:
176
parser.add_option(action='store_true', dest=self.name,
189
parser.add_option(action='callback',
190
callback=self._optparse_bool_callback,
191
callback_args=(True,),
178
default=OptionParser.DEFAULT_VALUE,
180
194
negation_strings = ['--%s' % self.get_negation_name()]
181
parser.add_option(action='store_false', dest=self.name,
195
parser.add_option(action='callback',
196
callback=self._optparse_bool_callback,
197
callback_args=(False,),
182
198
help=optparse.SUPPRESS_HELP, *negation_strings)
184
200
parser.add_option(action='callback',
188
204
default=OptionParser.DEFAULT_VALUE,
207
def _optparse_bool_callback(self, option, opt_str, value, parser, bool_v):
208
setattr(parser.values, self.name, bool_v)
209
if self.custom_callback is not None:
210
self.custom_callback(option, self.name, bool_v, parser)
191
212
def _optparse_callback(self, option, opt, value, parser):
192
213
setattr(parser.values, self.name, self.type(value))
214
if self.custom_callback is not None:
215
self.custom_callback(option, opt, value, parser)
194
217
def iter_switches(self):
195
218
"""Iterate through the list of switches provided by the option
235
258
values.append(self.type(value))
259
if self.custom_callback is not None:
260
self.custom_callback(option, opt, value, parser)
238
263
class RegistryOption(Option):
319
344
def _optparse_value_callback(self, cb_value):
320
345
def cb(option, opt, value, parser):
321
346
setattr(parser.values, self.name, self.type(cb_value))
347
if self.custom_callback is not None:
348
self.custom_callback(option, opt, value, parser)
324
351
def iter_switches(self):
387
def custom_help(name, help):
388
"""Clone a common option overriding the help."""
390
o = copy.copy(Option.OPTIONS[name])
395
def _standard_option(name, **kwargs):
396
"""Register a standard option."""
397
# All standard options are implicitly 'global' ones
398
Option.STD_OPTIONS[name] = Option(name, **kwargs)
399
Option.OPTIONS[name] = Option.STD_OPTIONS[name]
360
402
def _global_option(name, **kwargs):
361
"""Register o as a global option."""
403
"""Register a global option."""
362
404
Option.OPTIONS[name] = Option(name, **kwargs)
416
# This is the verbosity level detected during command line parsing.
417
# Note that the final value is dependent on the order in which the
418
# various flags (verbose, quiet, no-verbose, no-quiet) are given.
419
# The final value will be one of the following:
427
def _verbosity_level_callback(option, opt_str, value, parser):
428
global _verbosity_level
430
# Either --no-verbose or --no-quiet was specified
432
elif opt_str == "verbose":
433
if _verbosity_level > 0:
434
_verbosity_level += 1
438
if _verbosity_level < 0:
439
_verbosity_level -= 1
441
_verbosity_level = -1
374
444
_merge_type_registry = MergeTypeRegistry()
375
445
_merge_type_registry.register_lazy('merge3', 'bzrlib.merge', 'Merge3Merger',
376
446
"Native diff3-style merge")
379
449
_merge_type_registry.register_lazy('weave', 'bzrlib.merge', 'WeaveMerger',
380
450
"Weave-based merge")
452
# Declare the standard options
453
_standard_option('help', short_name='h',
454
help='Show help message.')
455
_standard_option('verbose', short_name='v',
456
help='Display more information.',
457
custom_callback=_verbosity_level_callback)
458
_standard_option('quiet', short_name='q',
459
help="Only display errors and warnings.",
460
custom_callback=_verbosity_level_callback)
462
# Declare commonly used options
382
463
_global_option('all')
383
464
_global_option('overwrite', help='Ignore differences between branches and '
384
465
'overwrite unconditionally.')
406
487
help='display timezone as local, original, or utc')
407
488
_global_option('unbound')
408
_global_option('verbose',
409
help='Display more information.',
411
489
_global_option('version')
412
490
_global_option('email')
413
491
_global_option('update')
424
502
_merge_type_registry, value_switches=True,
425
503
title='Merge algorithm')
426
504
_global_option('pattern', type=str)
427
_global_option('quiet', short_name='q')
428
505
_global_option('remember', help='Remember the specified location as a'
430
507
_global_option('reprocess', help='Reprocess to reduce spurious conflicts.')
432
509
_global_option('dry-run',
433
510
help="Show what would be done, but don't actually do anything.")
434
511
_global_option('name-from-revision', help='The path name in the old tree.')
436
_help_option = Option('help',
437
help='Show help message.',