~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/commands.py

  • Committer: John Arbash Meinel
  • Date: 2006-08-14 16:16:53 UTC
  • mto: (1946.2.6 reduce-knit-churn)
  • mto: This revision was merged to the branch mainline in revision 1919.
  • Revision ID: john@arbash-meinel.com-20060814161653-54cdcdadcd4e9003
Remove bogus entry from BRANCH.TODO

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
# TODO: "--profile=cum", to change sort order.  Is there any value in leaving
29
29
# the profile output behind so it can be interactively examined?
30
30
 
31
 
import codecs
32
 
import errno
 
31
import sys
33
32
import os
34
33
from warnings import warn
35
 
import sys
 
34
import errno
 
35
import codecs
36
36
 
37
37
import bzrlib
38
38
import bzrlib.errors as errors
40
40
                           BzrCommandError,
41
41
                           BzrCheckError,
42
42
                           NotBranchError)
43
 
from bzrlib import option
44
43
from bzrlib.option import Option
45
44
import bzrlib.osutils
46
45
from bzrlib.revisionspec import RevisionSpec
222
221
        r = dict()
223
222
        r['help'] = Option.OPTIONS['help']
224
223
        for o in self.takes_options:
225
 
            if isinstance(o, basestring):
 
224
            if not isinstance(o, Option):
226
225
                o = Option.OPTIONS[o]
227
226
            r[o.name] = o
228
227
        return r
257
256
 
258
257
    def run_argv_aliases(self, argv, alias_argv=None):
259
258
        """Parse the command line and run with extra aliases in alias_argv."""
260
 
        if argv is None:
261
 
            warn("Passing None for [] is deprecated from bzrlib 0.10", 
262
 
                 DeprecationWarning, stacklevel=2)
263
 
            argv = []
264
259
        args, opts = parse_args(self, argv, alias_argv)
265
260
        if 'help' in opts:  # e.g. bzr add --help
266
261
            from bzrlib.help import help_on_command
267
262
            help_on_command(self.name())
268
263
            return 0
 
264
        # XXX: This should be handled by the parser
 
265
        allowed_names = self.options().keys()
 
266
        for oname in opts:
 
267
            if oname not in allowed_names:
 
268
                raise BzrOptionError("option '--%s' is not allowed for"
 
269
                                " command %r" % (oname, self.name()))
269
270
        # mix arguments and options into one dictionary
270
271
        cmdargs = _match_argform(self.name(), self.takes_args, args)
271
272
        cmdopts = {}
353
354
    lookup table, something about the available options, what optargs
354
355
    they take, and which commands will accept them.
355
356
    """
356
 
    # TODO: make it a method of the Command?
357
 
    parser = option.get_optparser(command.options())
358
 
    if alias_argv is not None:
359
 
        args = alias_argv + argv
360
 
    else:
361
 
        args = argv
362
 
 
363
 
    options, args = parser.parse_args(args)
364
 
    opts = dict([(k, v) for k, v in options.__dict__.iteritems() if 
365
 
                 v is not option.OptionParser.DEFAULT_VALUE])
 
357
    # TODO: chop up this beast; make it a method of the Command
 
358
    args = []
 
359
    opts = {}
 
360
    alias_opts = {}
 
361
 
 
362
    cmd_options = command.options()
 
363
    argsover = False
 
364
    proc_aliasarg = True # Are we processing alias_argv now?
 
365
    for proc_argv in alias_argv, argv:
 
366
        while proc_argv:
 
367
            a = proc_argv.pop(0)
 
368
            if argsover:
 
369
                args.append(a)
 
370
                continue
 
371
            elif a == '-':
 
372
                args.append(a)
 
373
                continue
 
374
            elif a == '--':
 
375
                # We've received a standalone -- No more flags
 
376
                argsover = True
 
377
                continue
 
378
            if a[0] == '-':
 
379
                # option names must not be unicode
 
380
                a = str(a)
 
381
                optarg = None
 
382
                if a[1] == '-':
 
383
                    mutter("  got option %r", a)
 
384
                    if '=' in a:
 
385
                        optname, optarg = a[2:].split('=', 1)
 
386
                    else:
 
387
                        optname = a[2:]
 
388
                    if optname not in cmd_options:
 
389
                        raise BzrCommandError('unknown option "%s"' % a)
 
390
                else:
 
391
                    shortopt = a[1:]
 
392
                    if shortopt in Option.SHORT_OPTIONS:
 
393
                        # Multi-character options must have a space to delimit
 
394
                        # their value
 
395
                        # ^^^ what does this mean? mbp 20051014
 
396
                        optname = Option.SHORT_OPTIONS[shortopt].name
 
397
                    else:
 
398
                        # Single character short options, can be chained,
 
399
                        # and have their value appended to their name
 
400
                        shortopt = a[1:2]
 
401
                        if shortopt not in Option.SHORT_OPTIONS:
 
402
                            # We didn't find the multi-character name, and we
 
403
                            # didn't find the single char name
 
404
                            raise BzrCommandError('unknown option "%s"' % a)
 
405
                        optname = Option.SHORT_OPTIONS[shortopt].name
 
406
 
 
407
                        if a[2:]:
 
408
                            # There are extra things on this option
 
409
                            # see if it is the value, or if it is another
 
410
                            # short option
 
411
                            optargfn = Option.OPTIONS[optname].type
 
412
                            if optargfn is None:
 
413
                                # This option does not take an argument, so the
 
414
                                # next entry is another short option, pack it
 
415
                                # back into the list
 
416
                                proc_argv.insert(0, '-' + a[2:])
 
417
                            else:
 
418
                                # This option takes an argument, so pack it
 
419
                                # into the array
 
420
                                optarg = a[2:]
 
421
                    if optname not in cmd_options:
 
422
                        raise BzrCommandError('unknown option "%s"' % shortopt)
 
423
                if optname in opts:
 
424
                    # XXX: Do we ever want to support this, e.g. for -r?
 
425
                    if proc_aliasarg:
 
426
                        raise BzrCommandError('repeated option %r' % a)
 
427
                    elif optname in alias_opts:
 
428
                        # Replace what's in the alias with what's in the real
 
429
                        # argument
 
430
                        del alias_opts[optname]
 
431
                        del opts[optname]
 
432
                        proc_argv.insert(0, a)
 
433
                        continue
 
434
                    else:
 
435
                        raise BzrCommandError('repeated option %r' % a)
 
436
                    
 
437
                option_obj = cmd_options[optname]
 
438
                optargfn = option_obj.type
 
439
                if optargfn:
 
440
                    if optarg == None:
 
441
                        if not proc_argv:
 
442
                            raise BzrCommandError('option %r needs an argument' % a)
 
443
                        else:
 
444
                            optarg = proc_argv.pop(0)
 
445
                    opts[optname] = optargfn(optarg)
 
446
                    if proc_aliasarg:
 
447
                        alias_opts[optname] = optargfn(optarg)
 
448
                else:
 
449
                    if optarg != None:
 
450
                        raise BzrCommandError('option %r takes no argument' % optname)
 
451
                    opts[optname] = True
 
452
                    if proc_aliasarg:
 
453
                        alias_opts[optname] = True
 
454
            else:
 
455
                args.append(a)
 
456
        proc_aliasarg = False # Done with alias argv
366
457
    return args, opts
367
458
 
368
459
 
530
621
        return 0
531
622
 
532
623
    if argv[0] == '--version':
533
 
        from bzrlib.version import show_version
 
624
        from bzrlib.builtins import show_version
534
625
        show_version()
535
626
        return 0
536
627