~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/commands.py

[merge] bzr.dev

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 sys
 
31
import codecs
 
32
import errno
32
33
import os
33
34
from warnings import warn
34
 
import errno
35
 
import codecs
 
35
import sys
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
43
44
from bzrlib.option import Option
44
45
import bzrlib.osutils
45
 
from bzrlib.revisionspec import RevisionSpec
46
46
from bzrlib.symbol_versioning import (deprecated_method, zero_eight)
47
47
import bzrlib.trace
48
48
from bzrlib.trace import mutter, note, log_error, warning, be_quiet
64
64
        k_unsquished = _unsquish_command_name(k)
65
65
    else:
66
66
        k_unsquished = k
67
 
    if not plugin_cmds.has_key(k_unsquished):
 
67
    if k_unsquished not in plugin_cmds:
68
68
        plugin_cmds[k_unsquished] = cmd
69
69
        mutter('registered plugin command %s', k_unsquished)
70
70
        if decorate and k_unsquished in builtin_command_names():
129
129
    """
130
130
    from bzrlib.externalcommand import ExternalCommand
131
131
 
132
 
    cmd_name = str(cmd_name)            # not unicode
 
132
    # We want only 'ascii' command names, but the user may have typed
 
133
    # in a Unicode name. In that case, they should just get a
 
134
    # 'command not found' error later.
 
135
    # In the future, we may actually support Unicode command names.
133
136
 
134
137
    # first look up this command under the specified name
135
138
    cmds = _get_cmd_dict(plugins_override=plugins_override)
221
224
        r = dict()
222
225
        r['help'] = Option.OPTIONS['help']
223
226
        for o in self.takes_options:
224
 
            if not isinstance(o, Option):
 
227
            if isinstance(o, basestring):
225
228
                o = Option.OPTIONS[o]
226
229
            r[o.name] = o
227
230
        return r
256
259
 
257
260
    def run_argv_aliases(self, argv, alias_argv=None):
258
261
        """Parse the command line and run with extra aliases in alias_argv."""
 
262
        if argv is None:
 
263
            warn("Passing None for [] is deprecated from bzrlib 0.10", 
 
264
                 DeprecationWarning, stacklevel=2)
 
265
            argv = []
259
266
        args, opts = parse_args(self, argv, alias_argv)
260
267
        if 'help' in opts:  # e.g. bzr add --help
261
268
            from bzrlib.help import help_on_command
262
269
            help_on_command(self.name())
263
270
            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()))
270
271
        # mix arguments and options into one dictionary
271
272
        cmdargs = _match_argform(self.name(), self.takes_args, args)
272
273
        cmdopts = {}
354
355
    lookup table, something about the available options, what optargs
355
356
    they take, and which commands will accept them.
356
357
    """
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
 
358
    # TODO: make it a method of the Command?
 
359
    parser = option.get_optparser(command.options())
 
360
    if alias_argv is not None:
 
361
        args = alias_argv + argv
 
362
    else:
 
363
        args = argv
 
364
 
 
365
    options, args = parser.parse_args(args)
 
366
    opts = dict([(k, v) for k, v in options.__dict__.iteritems() if 
 
367
                 v is not option.OptionParser.DEFAULT_VALUE])
457
368
    return args, opts
458
369
 
459
370
 
621
532
        return 0
622
533
 
623
534
    if argv[0] == '--version':
624
 
        from bzrlib.builtins import show_version
 
535
        from bzrlib.version import show_version
625
536
        show_version()
626
537
        return 0
627
538
        
640
551
            alias_argv = [a.decode(bzrlib.user_encoding) for a in alias_argv]
641
552
            argv[0] = alias_argv.pop(0)
642
553
 
643
 
    cmd = str(argv.pop(0))
 
554
    cmd = argv.pop(0)
 
555
    # We want only 'ascii' command names, but the user may have typed
 
556
    # in a Unicode name. In that case, they should just get a
 
557
    # 'command not found' error later.
644
558
 
645
559
    cmd_obj = get_cmd_object(cmd, plugins_override=not opt_builtin)
646
560
    if not getattr(cmd_obj.run_argv, 'is_deprecated', False):