~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/option.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-08-24 21:25:26 UTC
  • mfrom: (4641.1.1 integration)
  • Revision ID: pqm@pqm.ubuntu.com-20090824212526-5j41jw5zsciji66e
(robertc) Back out draft documentation change landed as 'selftest
        improvements'. (Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
# TODO: For things like --diff-prefix, we want a way to customize the display
18
18
# of the option argument.
29
29
""")
30
30
 
31
31
from bzrlib import (
32
 
    log,
33
 
    registry,
 
32
    registry as _mod_registry,
34
33
    )
35
34
 
 
35
 
36
36
def _parse_revision_str(revstr):
37
37
    """This handles a revision string -> revno.
38
38
 
136
136
 
137
137
class Option(object):
138
138
    """Description of a command line option
139
 
    
 
139
 
140
140
    :ivar _short_name: If this option has a single-letter name, this is it.
141
141
    Otherwise None.
142
142
    """
150
150
    OPTIONS = {}
151
151
 
152
152
    def __init__(self, name, help='', type=None, argname=None,
153
 
                 short_name=None, param_name=None, custom_callback=None):
 
153
                 short_name=None, param_name=None, custom_callback=None,
 
154
                 hidden=False):
154
155
        """Make a new command option.
155
156
 
156
157
        :param name: regular name of the command, used in the double-dash
157
 
            form and also as the parameter to the command's run() 
 
158
            form and also as the parameter to the command's run()
158
159
            method (unless param_name is specified).
159
160
 
160
161
        :param help: help message displayed in command help
161
162
 
162
 
        :param type: function called to parse the option argument, or 
 
163
        :param type: function called to parse the option argument, or
163
164
            None (default) if this option doesn't take an argument.
164
165
 
165
166
        :param argname: name of option argument, if any
173
174
        :param custom_callback: a callback routine to be called after normal
174
175
            processing. The signature of the callback routine is
175
176
            (option, name, new_value, parser).
 
177
        :param hidden: If True, the option should be hidden in help and
 
178
            documentation.
176
179
        """
177
180
        self.name = name
178
181
        self.help = help
189
192
        else:
190
193
            self._param_name = param_name
191
194
        self.custom_callback = custom_callback
 
195
        self.hidden = hidden
192
196
 
193
197
    def short_name(self):
194
198
        if self._short_name:
208
212
        option_strings = ['--%s' % self.name]
209
213
        if short_name is not None:
210
214
            option_strings.append('-%s' % short_name)
 
215
        if self.hidden:
 
216
            help = optparse.SUPPRESS_HELP
 
217
        else:
 
218
            help = self.help
211
219
        optargfn = self.type
212
220
        if optargfn is None:
213
221
            parser.add_option(action='callback',
214
222
                              callback=self._optparse_bool_callback,
215
223
                              callback_args=(True,),
216
 
                              help=self.help,
 
224
                              help=help,
217
225
                              *option_strings)
218
226
            negation_strings = ['--%s' % self.get_negation_name()]
219
227
            parser.add_option(action='callback',
224
232
            parser.add_option(action='callback',
225
233
                              callback=self._optparse_callback,
226
234
                              type='string', metavar=self.argname.upper(),
227
 
                              help=self.help,
 
235
                              help=help,
228
236
                              default=OptionParser.DEFAULT_VALUE,
229
237
                              *option_strings)
230
238
 
241
249
 
242
250
    def iter_switches(self):
243
251
        """Iterate through the list of switches provided by the option
244
 
        
 
252
 
245
253
        :return: an iterator of (name, short_name, argname, help)
246
254
        """
247
255
        argname =  self.argname
250
258
        yield self.name, self.short_name(), argname, self.help
251
259
 
252
260
    def is_hidden(self, name):
253
 
        return False
 
261
        return self.hidden
254
262
 
255
263
 
256
264
class ListOption(Option):
306
314
        else:
307
315
            return self.converter(value)
308
316
 
309
 
    def __init__(self, name, help, registry, converter=None,
310
 
        value_switches=False, title=None, enum_switch=True):
 
317
    def __init__(self, name, help, registry=None, converter=None,
 
318
        value_switches=False, title=None, enum_switch=True,
 
319
        lazy_registry=None):
311
320
        """
312
321
        Constructor.
313
322
 
321
330
            '--knit' can be used interchangeably.
322
331
        :param enum_switch: If true, a switch is provided with the option name,
323
332
            which takes a value.
 
333
        :param lazy_registry: A tuple of (module name, attribute name) for a
 
334
            registry to be lazily loaded.
324
335
        """
325
336
        Option.__init__(self, name, help, type=self.convert)
326
 
        self.registry = registry
 
337
        self._registry = registry
 
338
        if registry is None:
 
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(
 
343
                *lazy_registry)
 
344
        if registry is not None and lazy_registry is not None:
 
345
            raise AssertionError(
 
346
                'registry and lazy_registry are mutually exclusive')
327
347
        self.name = name
328
348
        self.converter = converter
329
349
        self.value_switches = value_switches
332
352
        if self.title is None:
333
353
            self.title = name
334
354
 
 
355
    @property
 
356
    def registry(self):
 
357
        if self._registry is None:
 
358
            self._registry = self._lazy_registry.get_obj()
 
359
        return self._registry
 
360
 
335
361
    @staticmethod
336
362
    def from_kwargs(name_, help=None, title=None, value_switches=False,
337
363
                    enum_switch=True, **kwargs):
341
367
        RegistryOption constructor.  Any other keyword arguments are treated
342
368
        as values for the option, and they value is treated as the help.
343
369
        """
344
 
        reg = registry.Registry()
 
370
        reg = _mod_registry.Registry()
345
371
        for name, switch_help in kwargs.iteritems():
346
372
            name = name.replace('_', '-')
347
373
            reg.register(name, name, help=switch_help)
434
460
    Option.OPTIONS[name] = Option(name, **kwargs)
435
461
 
436
462
 
437
 
def _global_registry_option(name, help, registry, **kwargs):
 
463
def _global_registry_option(name, help, registry=None, **kwargs):
438
464
    Option.OPTIONS[name] = RegistryOption(name, help, registry, **kwargs)
439
465
 
440
466
 
441
 
class MergeTypeRegistry(registry.Registry):
442
 
 
443
 
    pass
444
 
 
445
 
 
446
467
# This is the verbosity level detected during command line parsing.
447
468
# Note that the final value is dependent on the order in which the
448
469
# various flags (verbose, quiet, no-verbose, no-quiet) are given.
471
492
            _verbosity_level = -1
472
493
 
473
494
 
 
495
class MergeTypeRegistry(_mod_registry.Registry):
 
496
 
 
497
    pass
 
498
 
 
499
 
474
500
_merge_type_registry = MergeTypeRegistry()
475
501
_merge_type_registry.register_lazy('merge3', 'bzrlib.merge', 'Merge3Merger',
476
502
                                   "Native diff3-style merge")
484
510
# Declare the standard options
485
511
_standard_option('help', short_name='h',
486
512
                 help='Show help message.')
 
513
_standard_option('usage',
 
514
                 help='Show usage message and options.')
487
515
_standard_option('verbose', short_name='v',
488
516
                 help='Display more information.',
489
517
                 custom_callback=_verbosity_level_callback)
519
547
               help='Select changes introduced by the specified revision. See also "help revisionspec".')
520
548
_global_option('show-ids',
521
549
               help='Show internal object ids.')
522
 
_global_option('timezone', 
 
550
_global_option('timezone',
523
551
               type=str,
524
 
               help='display timezone as local, original, or utc')
 
552
               help='Display timezone as local, original, or utc.')
525
553
_global_option('unbound')
526
554
_global_option('version')
527
555
_global_option('email')
528
556
_global_option('update')
529
557
_global_registry_option('log-format', "Use specified log format.",
530
 
                        log.log_formatter_registry, value_switches=True,
531
 
                        title='Log format')
 
558
                        lazy_registry=('bzrlib.log', 'log_formatter_registry'),
 
559
                        value_switches=True, title='Log format')
532
560
_global_option('long', help='Use detailed log format. Same as --log-format long',
533
561
               short_name='l')
534
562
_global_option('short', help='Use moderately short log format. Same as --log-format short')
546
574
_global_option('dry-run',
547
575
               help="Show what would be done, but don't actually do anything.")
548
576
_global_option('name-from-revision', help='The path name in the old tree.')
 
577
 
 
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'