~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/option.py

  • Committer: Robert Collins
  • Date: 2007-09-03 23:19:00 UTC
  • mfrom: (2791 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2792.
  • Revision ID: robertc@robertcollins.net-20070903231900-j3or8vkiixxpskzm
Fix some inconsistent NEWS indents.

Show diffs side-by-side

added added

removed removed

Lines of Context:
142
142
    Otherwise None.
143
143
    """
144
144
 
145
 
    # TODO: Some way to show in help a description of the option argument
 
145
    # The dictionary of standard options. These are always legal.
 
146
    STD_OPTIONS = {}
146
147
 
 
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.
147
151
    OPTIONS = {}
148
152
 
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.
152
156
 
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).
156
160
 
157
 
        help -- help message displayed in command help
 
161
        :param help: help message displayed in command help
158
162
 
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.
161
165
 
162
 
        argname -- name of option argument, if any
163
 
 
164
 
        param_name -- name of the parameter which will be passed to
 
166
        :param argname: name of option argument, if any
 
167
 
 
168
        :param short_name: short option code for use with a single -, e.g.
 
169
            short_name="v" to enable parsing of -v.
 
170
 
 
171
        :param param_name: name of the parameter which will be passed to
165
172
            the command's run() method.
 
173
 
 
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).
166
177
        """
167
178
        self.name = name
168
179
        self.help = help
177
188
            self._param_name = self.name
178
189
        else:
179
190
            self._param_name = param_name
 
191
        self.custom_callback = custom_callback
180
192
 
181
193
    def short_name(self):
182
194
        if self._short_name:
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,),
202
216
                              help=self.help,
203
 
                              default=OptionParser.DEFAULT_VALUE,
204
217
                              *option_strings)
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)
208
223
        else:
209
224
            parser.add_option(action='callback', 
213
228
                              default=OptionParser.DEFAULT_VALUE, 
214
229
                              *option_strings)
215
230
 
 
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)
 
235
 
216
236
    def _optparse_callback(self, option, opt, value, parser):
217
 
        setattr(parser.values, self._param_name, self.type(value))
 
237
        v = 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)
218
241
 
219
242
    def iter_switches(self):
220
243
        """Iterate through the list of switches provided by the option
253
276
                          *option_strings)
254
277
 
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)
257
280
        if value == '-':
258
281
            del values[:]
259
282
        else:
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)
261
286
 
262
287
 
263
288
class RegistryOption(Option):
343
368
 
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)
347
375
        return cb
348
376
 
349
377
    def iter_switches(self):
382
410
    return parser
383
411
 
384
412
 
 
413
def custom_help(name, help):
 
414
    """Clone a common option overriding the help."""
 
415
    import copy
 
416
    o = copy.copy(Option.OPTIONS[name])
 
417
    o.help = help
 
418
    return o
 
419
 
 
420
 
 
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]
 
426
 
 
427
 
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)
388
431
 
389
432
 
396
439
    pass
397
440
 
398
441
 
 
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:
 
446
#
 
447
# * -ve for quiet
 
448
# * 0 for normal
 
449
# * +ve for verbose
 
450
_verbosity_level = 0
 
451
 
 
452
 
 
453
def _verbosity_level_callback(option, opt_str, value, parser):
 
454
    global _verbosity_level
 
455
    if not value:
 
456
        # Either --no-verbose or --no-quiet was specified
 
457
        _verbosity_level = 0
 
458
    elif opt_str == "verbose":
 
459
        if _verbosity_level > 0:
 
460
            _verbosity_level += 1
 
461
        else:
 
462
            _verbosity_level = 1
 
463
    else:
 
464
        if _verbosity_level < 0:
 
465
            _verbosity_level -= 1
 
466
        else:
 
467
            _verbosity_level = -1
 
468
 
 
469
 
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")
406
477
 
 
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)
 
487
 
 
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.')
435
517
               type=str,
436
518
               help='display timezone as local, original, or utc')
437
519
_global_option('unbound')
438
 
_global_option('verbose',
439
 
               help='Display more information.',
440
 
               short_name='v')
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'
459
537
               ' default.')
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.')
465
 
 
466
 
_help_option = Option('help',
467
 
                      help='Show help message.',
468
 
                      short_name='h')