~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/option.py

  • Committer: John Arbash Meinel
  • Date: 2008-09-09 15:09:12 UTC
  • mto: This revision was merged to the branch mainline in revision 3699.
  • Revision ID: john@arbash-meinel.com-20080909150912-wyttm8he1zsls2ck
Use the right timing function on win32

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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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
 
    registry as _mod_registry,
 
32
    log,
 
33
    registry,
33
34
    )
34
35
 
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,
154
 
                 hidden=False):
 
153
                 short_name=None, param_name=None, custom_callback=None):
155
154
        """Make a new command option.
156
155
 
157
156
        :param name: regular name of the command, used in the double-dash
158
 
            form and also as the parameter to the command's run()
 
157
            form and also as the parameter to the command's run() 
159
158
            method (unless param_name is specified).
160
159
 
161
160
        :param help: help message displayed in command help
162
161
 
163
 
        :param type: function called to parse the option argument, or
 
162
        :param type: function called to parse the option argument, or 
164
163
            None (default) if this option doesn't take an argument.
165
164
 
166
165
        :param argname: name of option argument, if any
174
173
        :param custom_callback: a callback routine to be called after normal
175
174
            processing. The signature of the callback routine is
176
175
            (option, name, new_value, parser).
177
 
        :param hidden: If True, the option should be hidden in help and
178
 
            documentation.
179
176
        """
180
177
        self.name = name
181
178
        self.help = help
192
189
        else:
193
190
            self._param_name = param_name
194
191
        self.custom_callback = custom_callback
195
 
        self.hidden = hidden
196
192
 
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)
215
 
        if self.hidden:
216
 
            help = optparse.SUPPRESS_HELP
217
 
        else:
218
 
            help = self.help
219
211
        optargfn = self.type
220
212
        if optargfn is None:
221
213
            parser.add_option(action='callback',
222
214
                              callback=self._optparse_bool_callback,
223
215
                              callback_args=(True,),
224
 
                              help=help,
 
216
                              help=self.help,
225
217
                              *option_strings)
226
218
            negation_strings = ['--%s' % self.get_negation_name()]
227
219
            parser.add_option(action='callback',
232
224
            parser.add_option(action='callback',
233
225
                              callback=self._optparse_callback,
234
226
                              type='string', metavar=self.argname.upper(),
235
 
                              help=help,
 
227
                              help=self.help,
236
228
                              default=OptionParser.DEFAULT_VALUE,
237
229
                              *option_strings)
238
230
 
249
241
 
250
242
    def iter_switches(self):
251
243
        """Iterate through the list of switches provided by the option
252
 
 
 
244
        
253
245
        :return: an iterator of (name, short_name, argname, help)
254
246
        """
255
247
        argname =  self.argname
258
250
        yield self.name, self.short_name(), argname, self.help
259
251
 
260
252
    def is_hidden(self, name):
261
 
        return self.hidden
 
253
        return False
262
254
 
263
255
 
264
256
class ListOption(Option):
314
306
        else:
315
307
            return self.converter(value)
316
308
 
317
 
    def __init__(self, name, help, registry=None, converter=None,
318
 
        value_switches=False, title=None, enum_switch=True,
319
 
        lazy_registry=None):
 
309
    def __init__(self, name, help, registry, converter=None,
 
310
        value_switches=False, title=None, enum_switch=True):
320
311
        """
321
312
        Constructor.
322
313
 
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.
335
324
        """
336
325
        Option.__init__(self, name, help, type=self.convert)
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')
 
326
        self.registry = registry
347
327
        self.name = name
348
328
        self.converter = converter
349
329
        self.value_switches = value_switches
352
332
        if self.title is None:
353
333
            self.title = name
354
334
 
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
 
 
361
335
    @staticmethod
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.
369
343
        """
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)
460
434
    Option.OPTIONS[name] = Option(name, **kwargs)
461
435
 
462
436
 
463
 
def _global_registry_option(name, help, registry=None, **kwargs):
 
437
def _global_registry_option(name, help, registry, **kwargs):
464
438
    Option.OPTIONS[name] = RegistryOption(name, help, registry, **kwargs)
465
439
 
466
440
 
 
441
class MergeTypeRegistry(registry.Registry):
 
442
 
 
443
    pass
 
444
 
 
445
 
467
446
# This is the verbosity level detected during command line parsing.
468
447
# Note that the final value is dependent on the order in which the
469
448
# various flags (verbose, quiet, no-verbose, no-quiet) are given.
492
471
            _verbosity_level = -1
493
472
 
494
473
 
495
 
class MergeTypeRegistry(_mod_registry.Registry):
496
 
 
497
 
    pass
498
 
 
499
 
 
500
474
_merge_type_registry = MergeTypeRegistry()
501
475
_merge_type_registry.register_lazy('merge3', 'bzrlib.merge', 'Merge3Merger',
502
476
                                   "Native diff3-style merge")
510
484
# Declare the standard options
511
485
_standard_option('help', short_name='h',
512
486
                 help='Show help message.')
513
 
_standard_option('usage',
514
 
                 help='Show usage message and options.')
515
487
_standard_option('verbose', short_name='v',
516
488
                 help='Display more information.',
517
489
                 custom_callback=_verbosity_level_callback)
547
519
               help='Select changes introduced by the specified revision. See also "help revisionspec".')
548
520
_global_option('show-ids',
549
521
               help='Show internal object ids.')
550
 
_global_option('timezone',
 
522
_global_option('timezone', 
551
523
               type=str,
552
 
               help='Display timezone as local, original, or utc.')
 
524
               help='display timezone as local, original, or utc')
553
525
_global_option('unbound')
554
526
_global_option('version')
555
527
_global_option('email')
556
528
_global_option('update')
557
529
_global_registry_option('log-format', "Use specified log format.",
558
 
                        lazy_registry=('bzrlib.log', 'log_formatter_registry'),
559
 
                        value_switches=True, title='Log format')
 
530
                        log.log_formatter_registry, value_switches=True,
 
531
                        title='Log format')
560
532
_global_option('long', help='Use detailed log format. Same as --log-format long',
561
533
               short_name='l')
562
534
_global_option('short', help='Use moderately short log format. Same as --log-format short')
574
546
_global_option('dry-run',
575
547
               help="Show what would be done, but don't actually do anything.")
576
548
_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'