~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/option.py

(jelmer) Use the absolute_import feature everywhere in bzrlib,
 and add a source test to make sure it's used everywhere. (Jelmer Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2004, 2005, 2006, 2007 Canonical Ltd
 
1
# Copyright (C) 2005-2010 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
17
17
# TODO: For things like --diff-prefix, we want a way to customize the display
18
18
# of the option argument.
19
19
 
 
20
from __future__ import absolute_import
 
21
 
20
22
import optparse
21
23
import re
22
24
 
25
27
from bzrlib import (
26
28
    errors,
27
29
    revisionspec,
 
30
    i18n,
28
31
    )
29
32
""")
30
33
 
276
279
        parser.add_option(action='callback',
277
280
                          callback=self._optparse_callback,
278
281
                          type='string', metavar=self.argname.upper(),
279
 
                          help=self.help, default=[],
 
282
                          help=self.help, dest=self._param_name, default=[],
280
283
                          *option_strings)
281
284
 
282
285
    def _optparse_callback(self, option, opt, value, parser):
312
315
 
313
316
    def __init__(self, name, help, registry=None, converter=None,
314
317
        value_switches=False, title=None, enum_switch=True,
315
 
        lazy_registry=None):
 
318
        lazy_registry=None, short_name=None, short_value_switches=None):
316
319
        """
317
320
        Constructor.
318
321
 
328
331
            which takes a value.
329
332
        :param lazy_registry: A tuple of (module name, attribute name) for a
330
333
            registry to be lazily loaded.
 
334
        :param short_name: The short name for the enum switch, if any
 
335
        :param short_value_switches: A dict mapping values to short names
331
336
        """
332
 
        Option.__init__(self, name, help, type=self.convert)
 
337
        Option.__init__(self, name, help, type=self.convert,
 
338
                        short_name=short_name)
333
339
        self._registry = registry
334
340
        if registry is None:
335
341
            if lazy_registry is None:
344
350
        self.converter = converter
345
351
        self.value_switches = value_switches
346
352
        self.enum_switch = enum_switch
 
353
        self.short_value_switches = short_value_switches
347
354
        self.title = title
348
355
        if self.title is None:
349
356
            self.title = name
361
368
 
362
369
        name, help, value_switches and enum_switch are passed to the
363
370
        RegistryOption constructor.  Any other keyword arguments are treated
364
 
        as values for the option, and they value is treated as the help.
 
371
        as values for the option, and their value is treated as the help.
365
372
        """
366
373
        reg = _mod_registry.Registry()
367
 
        for name, switch_help in kwargs.iteritems():
 
374
        for name, switch_help in sorted(kwargs.items()):
368
375
            name = name.replace('_', '-')
369
376
            reg.register(name, name, help=switch_help)
370
377
            if not value_switches:
387
394
                    help = optparse.SUPPRESS_HELP
388
395
                else:
389
396
                    help = self.registry.get_help(key)
 
397
                if (self.short_value_switches and
 
398
                    key in self.short_value_switches):
 
399
                    option_strings.append('-%s' %
 
400
                                          self.short_value_switches[key])
390
401
                parser.add_option(action='callback',
391
402
                              callback=self._optparse_value_callback(key),
392
403
                                  help=help,
422
433
 
423
434
    DEFAULT_VALUE = object()
424
435
 
 
436
    def __init__(self):
 
437
        optparse.OptionParser.__init__(self)
 
438
        self.formatter = GettextIndentedHelpFormatter()
 
439
 
425
440
    def error(self, message):
426
441
        raise errors.BzrCommandError(message)
427
442
 
428
443
 
 
444
class GettextIndentedHelpFormatter(optparse.IndentedHelpFormatter):
 
445
    """Adds gettext() call to format_option()"""
 
446
    def __init__(self):
 
447
        optparse.IndentedHelpFormatter.__init__(self)
 
448
 
 
449
    def format_option(self, option):
 
450
        """code taken from Python's optparse.py"""
 
451
        if option.help:
 
452
            option.help = i18n.gettext(option.help)
 
453
        return optparse.IndentedHelpFormatter.format_option(self, option)
 
454
 
 
455
 
429
456
def get_optparser(options):
430
457
    """Generate an optparse parser for bzrlib-style options"""
431
458
 
450
477
    Option.STD_OPTIONS[name] = Option(name, **kwargs)
451
478
    Option.OPTIONS[name] = Option.STD_OPTIONS[name]
452
479
 
 
480
def _standard_list_option(name, **kwargs):
 
481
    """Register a standard option."""
 
482
    # All standard options are implicitly 'global' ones
 
483
    Option.STD_OPTIONS[name] = ListOption(name, **kwargs)
 
484
    Option.OPTIONS[name] = Option.STD_OPTIONS[name]
 
485
 
453
486
 
454
487
def _global_option(name, **kwargs):
455
488
    """Register a global option."""
488
521
            _verbosity_level = -1
489
522
 
490
523
 
491
 
class MergeTypeRegistry(_mod_registry.Registry):
492
 
 
493
 
    pass
494
 
 
495
 
 
496
 
_merge_type_registry = MergeTypeRegistry()
497
 
_merge_type_registry.register_lazy('merge3', 'bzrlib.merge', 'Merge3Merger',
498
 
                                   "Native diff3-style merge")
499
 
_merge_type_registry.register_lazy('diff3', 'bzrlib.merge', 'Diff3Merger',
500
 
                                   "Merge using external diff3")
501
 
_merge_type_registry.register_lazy('weave', 'bzrlib.merge', 'WeaveMerger',
502
 
                                   "Weave-based merge")
503
 
_merge_type_registry.register_lazy('lca', 'bzrlib.merge', 'LCAMerger',
504
 
                                   "LCA-newness merge")
505
 
 
506
524
# Declare the standard options
507
525
_standard_option('help', short_name='h',
508
526
                 help='Show help message.')
 
527
_standard_option('quiet', short_name='q',
 
528
                 help="Only display errors and warnings.",
 
529
                 custom_callback=_verbosity_level_callback)
509
530
_standard_option('usage',
510
531
                 help='Show usage message and options.')
511
532
_standard_option('verbose', short_name='v',
512
533
                 help='Display more information.',
513
534
                 custom_callback=_verbosity_level_callback)
514
 
_standard_option('quiet', short_name='q',
515
 
                 help="Only display errors and warnings.",
516
 
                 custom_callback=_verbosity_level_callback)
517
535
 
518
536
# Declare commonly used options
519
 
_global_option('all')
520
 
_global_option('overwrite', help='Ignore differences between branches and '
521
 
               'overwrite unconditionally.')
522
 
_global_option('basis', type=str)
523
 
_global_option('bound')
524
 
_global_option('diff-options', type=str)
 
537
_global_option('change',
 
538
               type=_parse_change_str,
 
539
               short_name='c',
 
540
               param_name='revision',
 
541
               help='Select changes introduced by the specified revision. See also "help revisionspec".')
 
542
_global_option('directory', short_name='d', type=unicode,
 
543
               help='Branch to operate on, instead of working directory.')
525
544
_global_option('file', type=unicode, short_name='F')
526
 
_global_option('force')
527
 
_global_option('format', type=unicode)
528
 
_global_option('forward')
 
545
_global_registry_option('log-format', "Use specified log format.",
 
546
                        lazy_registry=('bzrlib.log', 'log_formatter_registry'),
 
547
                        value_switches=True, title='Log format',
 
548
                        short_value_switches={'short': 'S'})
 
549
_global_registry_option('merge-type', 'Select a particular merge algorithm.',
 
550
                        lazy_registry=('bzrlib.merge', 'merge_type_registry'),
 
551
                        value_switches=True, title='Merge algorithm')
529
552
_global_option('message', type=unicode,
530
553
               short_name='m',
531
554
               help='Message string.')
532
 
_global_option('no-recurse')
533
 
_global_option('profile',
534
 
               help='Show performance profiling information.')
 
555
_global_option('null', short_name='0',
 
556
                 help='Use an ASCII NUL (\\0) separator rather than '
 
557
                      'a newline.')
 
558
_global_option('overwrite', help='Ignore differences between branches and '
 
559
               'overwrite unconditionally.')
 
560
_global_option('remember', help='Remember the specified location as a'
 
561
               ' default.')
 
562
_global_option('reprocess', help='Reprocess to reduce spurious conflicts.')
535
563
_global_option('revision',
536
564
               type=_parse_revision_str,
537
565
               short_name='r',
538
566
               help='See "help revisionspec" for details.')
539
 
_global_option('change',
540
 
               type=_parse_change_str,
541
 
               short_name='c',
542
 
               param_name='revision',
543
 
               help='Select changes introduced by the specified revision. See also "help revisionspec".')
544
567
_global_option('show-ids',
545
568
               help='Show internal object ids.')
546
569
_global_option('timezone',
547
570
               type=str,
548
571
               help='Display timezone as local, original, or utc.')
549
 
_global_option('unbound')
550
 
_global_option('version')
551
 
_global_option('email')
552
 
_global_option('update')
553
 
_global_registry_option('log-format', "Use specified log format.",
554
 
                        lazy_registry=('bzrlib.log', 'log_formatter_registry'),
555
 
                        value_switches=True, title='Log format')
556
 
_global_option('long', help='Use detailed log format. Same as --log-format long',
557
 
               short_name='l')
558
 
_global_option('short', help='Use moderately short log format. Same as --log-format short')
559
 
_global_option('line', help='Use log format with one line per revision. Same as --log-format line')
560
 
_global_option('root', type=str)
561
 
_global_option('no-backup')
562
 
_global_registry_option('merge-type', 'Select a particular merge algorithm.',
563
 
                        _merge_type_registry, value_switches=True,
564
 
                        title='Merge algorithm')
565
 
_global_option('pattern', type=str)
566
 
_global_option('remember', help='Remember the specified location as a'
567
 
               ' default.')
568
 
_global_option('reprocess', help='Reprocess to reduce spurious conflicts.')
569
 
_global_option('kind', type=str)
570
 
_global_option('dry-run',
571
 
               help="Show what would be done, but don't actually do anything.")
572
 
_global_option('name-from-revision', help='The path name in the old tree.')
573
572
 
574
573
diff_writer_registry = _mod_registry.Registry()
575
574
diff_writer_registry.register('plain', lambda x: x, 'Plaintext diff output.')