13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
17
# TODO: For things like --diff-prefix, we want a way to customize the display
18
18
# of the option argument.
23
22
from bzrlib.lazy_import import lazy_import
24
23
lazy_import(globals(), """
25
26
from bzrlib import (
32
registry as _mod_registry,
34
from bzrlib.trace import warning
36
37
def _parse_revision_str(revstr):
152
153
def __init__(self, name, help='', type=None, argname=None,
153
short_name=None, param_name=None, custom_callback=None,
154
short_name=None, param_name=None, custom_callback=None):
155
155
"""Make a new command option.
157
157
:param name: regular name of the command, used in the double-dash
158
form and also as the parameter to the command's run()
158
form and also as the parameter to the command's run()
159
159
method (unless param_name is specified).
161
161
:param help: help message displayed in command help
163
:param type: function called to parse the option argument, or
163
:param type: function called to parse the option argument, or
164
164
None (default) if this option doesn't take an argument.
166
166
:param argname: name of option argument, if any
174
174
:param custom_callback: a callback routine to be called after normal
175
175
processing. The signature of the callback routine is
176
176
(option, name, new_value, parser).
177
:param hidden: If True, the option should be hidden in help and
183
181
self._short_name = short_name
186
raise ValueError('argname not valid for booleans')
183
assert argname is None
187
184
elif argname is None:
189
186
self.argname = argname
190
187
if param_name is None:
191
self._param_name = self.name.replace('-', '_')
188
self._param_name = self.name
193
190
self._param_name = param_name
194
191
self.custom_callback = custom_callback
197
193
def short_name(self):
198
194
if self._short_name:
212
208
option_strings = ['--%s' % self.name]
213
209
if short_name is not None:
214
210
option_strings.append('-%s' % short_name)
216
help = optparse.SUPPRESS_HELP
219
211
optargfn = self.type
220
212
if optargfn is None:
221
parser.add_option(action='callback',
222
callback=self._optparse_bool_callback,
213
parser.add_option(action='callback',
214
callback=self._optparse_bool_callback,
223
215
callback_args=(True,),
226
218
negation_strings = ['--%s' % self.get_negation_name()]
227
parser.add_option(action='callback',
228
callback=self._optparse_bool_callback,
219
parser.add_option(action='callback',
220
callback=self._optparse_bool_callback,
229
221
callback_args=(False,),
230
222
help=optparse.SUPPRESS_HELP, *negation_strings)
232
parser.add_option(action='callback',
233
callback=self._optparse_callback,
224
parser.add_option(action='callback',
225
callback=self._optparse_callback,
234
226
type='string', metavar=self.argname.upper(),
236
default=OptionParser.DEFAULT_VALUE,
228
default=OptionParser.DEFAULT_VALUE,
239
231
def _optparse_bool_callback(self, option, opt_str, value, parser, bool_v):
258
250
yield self.name, self.short_name(), argname, self.help
260
252
def is_hidden(self, name):
264
256
class ListOption(Option):
315
307
return self.converter(value)
317
def __init__(self, name, help, registry=None, converter=None,
318
value_switches=False, title=None, enum_switch=True,
309
def __init__(self, name, help, registry, converter=None,
310
value_switches=False, title=None, enum_switch=True):
330
321
'--knit' can be used interchangeably.
331
322
:param enum_switch: If true, a switch is provided with the option name,
332
323
which takes a value.
333
:param lazy_registry: A tuple of (module name, attribute name) for a
334
registry to be lazily loaded.
336
325
Option.__init__(self, name, help, type=self.convert)
337
self._registry = registry
339
if lazy_registry is None:
340
raise AssertionError(
341
'One of registry or lazy_registry must be given.')
342
self._lazy_registry = _mod_registry._LazyObjectGetter(
344
if registry is not None and lazy_registry is not None:
345
raise AssertionError(
346
'registry and lazy_registry are mutually exclusive')
326
self.registry = registry
348
328
self.converter = converter
349
329
self.value_switches = value_switches
352
332
if self.title is None:
353
333
self.title = name
357
if self._registry is None:
358
self._registry = self._lazy_registry.get_obj()
359
return self._registry
362
336
def from_kwargs(name_, help=None, title=None, value_switches=False,
363
337
enum_switch=True, **kwargs):
367
341
RegistryOption constructor. Any other keyword arguments are treated
368
342
as values for the option, and they value is treated as the help.
370
reg = _mod_registry.Registry()
344
reg = registry.Registry()
371
345
for name, switch_help in kwargs.iteritems():
372
346
name = name.replace('_', '-')
373
347
reg.register(name, name, help=switch_help)
374
if not value_switches:
375
help = help + ' "' + name + '": ' + switch_help
376
if not help.endswith("."):
378
348
return RegistryOption(name_, help, reg, title=title,
379
349
value_switches=value_switches, enum_switch=enum_switch)
460
430
Option.OPTIONS[name] = Option(name, **kwargs)
463
def _global_registry_option(name, help, registry=None, **kwargs):
433
def _global_registry_option(name, help, registry, **kwargs):
464
434
Option.OPTIONS[name] = RegistryOption(name, help, registry, **kwargs)
437
class MergeTypeRegistry(registry.Registry):
467
442
# This is the verbosity level detected during command line parsing.
468
443
# Note that the final value is dependent on the order in which the
469
444
# various flags (verbose, quiet, no-verbose, no-quiet) are given.
492
467
_verbosity_level = -1
495
class MergeTypeRegistry(_mod_registry.Registry):
500
470
_merge_type_registry = MergeTypeRegistry()
501
471
_merge_type_registry.register_lazy('merge3', 'bzrlib.merge', 'Merge3Merger',
502
472
"Native diff3-style merge")
504
474
"Merge using external diff3")
505
475
_merge_type_registry.register_lazy('weave', 'bzrlib.merge', 'WeaveMerger',
506
476
"Weave-based merge")
507
_merge_type_registry.register_lazy('lca', 'bzrlib.merge', 'LCAMerger',
510
478
# Declare the standard options
511
479
_standard_option('help', short_name='h',
512
480
help='Show help message.')
513
_standard_option('usage',
514
help='Show usage message and options.')
515
481
_standard_option('verbose', short_name='v',
516
482
help='Display more information.',
517
483
custom_callback=_verbosity_level_callback)
547
513
help='Select changes introduced by the specified revision. See also "help revisionspec".')
548
514
_global_option('show-ids',
549
515
help='Show internal object ids.')
550
_global_option('timezone',
516
_global_option('timezone',
552
help='Display timezone as local, original, or utc.')
518
help='display timezone as local, original, or utc')
553
519
_global_option('unbound')
554
520
_global_option('version')
555
521
_global_option('email')
556
522
_global_option('update')
557
523
_global_registry_option('log-format', "Use specified log format.",
558
lazy_registry=('bzrlib.log', 'log_formatter_registry'),
559
value_switches=True, title='Log format')
524
log.log_formatter_registry, value_switches=True,
560
526
_global_option('long', help='Use detailed log format. Same as --log-format long',
562
528
_global_option('short', help='Use moderately short log format. Same as --log-format short')
574
540
_global_option('dry-run',
575
541
help="Show what would be done, but don't actually do anything.")
576
542
_global_option('name-from-revision', help='The path name in the old tree.')
578
diff_writer_registry = _mod_registry.Registry()
579
diff_writer_registry.register('plain', lambda x: x, 'Plaintext diff output.')
580
diff_writer_registry.default_key = 'plain'