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?
34
from bzrlib.lazy_import import lazy_import
35
lazy_import(globals(), """
33
from warnings import warn
38
from warnings import warn
49
from bzrlib.symbol_versioning import (
38
import bzrlib.errors as errors
39
from bzrlib.errors import (BzrError,
56
43
from bzrlib.option import Option
45
from bzrlib.revisionspec import RevisionSpec
46
from bzrlib.symbol_versioning import (deprecated_method, zero_eight)
48
from bzrlib.trace import mutter, note, log_error, warning, be_quiet
73
64
k_unsquished = _unsquish_command_name(k)
76
if k_unsquished not in plugin_cmds:
67
if not plugin_cmds.has_key(k_unsquished):
77
68
plugin_cmds[k_unsquished] = cmd
78
trace.mutter('registered plugin command %s', k_unsquished)
69
mutter('registered plugin command %s', k_unsquished)
79
70
if decorate and k_unsquished in builtin_command_names():
80
71
return _builtin_commands()[k_unsquished]
139
130
from bzrlib.externalcommand import ExternalCommand
141
# We want only 'ascii' command names, but the user may have typed
142
# in a Unicode name. In that case, they should just get a
143
# 'command not found' error later.
144
# In the future, we may actually support Unicode command names.
132
cmd_name = str(cmd_name) # not unicode
146
134
# first look up this command under the specified name
147
135
cmds = _get_cmd_dict(plugins_override=plugins_override)
269
257
def run_argv_aliases(self, argv, alias_argv=None):
270
258
"""Parse the command line and run with extra aliases in alias_argv."""
272
warn("Passing None for [] is deprecated from bzrlib 0.10",
273
DeprecationWarning, stacklevel=2)
275
259
args, opts = parse_args(self, argv, alias_argv)
276
260
if 'help' in opts: # e.g. bzr add --help
277
261
from bzrlib.help import help_on_command
278
262
help_on_command(self.name())
264
# XXX: This should be handled by the parser
265
allowed_names = self.options().keys()
267
if oname not in allowed_names:
268
raise BzrOptionError("option '--%s' is not allowed for"
269
" command %r" % (oname, self.name()))
280
270
# mix arguments and options into one dictionary
281
271
cmdargs = _match_argform(self.name(), self.takes_args, args)
367
354
lookup table, something about the available options, what optargs
368
355
they take, and which commands will accept them.
370
# TODO: make it a method of the Command?
371
parser = option.get_optparser(command.options())
372
if alias_argv is not None:
373
args = alias_argv + argv
377
options, args = parser.parse_args(args)
378
opts = dict([(k, v) for k, v in options.__dict__.iteritems() if
379
v is not option.OptionParser.DEFAULT_VALUE])
357
# TODO: chop up this beast; make it a method of the Command
362
cmd_options = command.options()
364
proc_aliasarg = True # Are we processing alias_argv now?
365
for proc_argv in alias_argv, argv:
375
# We've received a standalone -- No more flags
379
# option names must not be unicode
383
mutter(" got option %r", a)
385
optname, optarg = a[2:].split('=', 1)
388
if optname not in cmd_options:
389
raise BzrCommandError('unknown option "%s"' % a)
392
if shortopt in Option.SHORT_OPTIONS:
393
# Multi-character options must have a space to delimit
395
# ^^^ what does this mean? mbp 20051014
396
optname = Option.SHORT_OPTIONS[shortopt].name
398
# Single character short options, can be chained,
399
# and have their value appended to their name
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
408
# There are extra things on this option
409
# see if it is the value, or if it is another
411
optargfn = Option.OPTIONS[optname].type
413
# This option does not take an argument, so the
414
# next entry is another short option, pack it
416
proc_argv.insert(0, '-' + a[2:])
418
# This option takes an argument, so pack it
421
if optname not in cmd_options:
422
raise BzrCommandError('unknown option "%s"' % shortopt)
424
# XXX: Do we ever want to support this, e.g. for -r?
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
430
del alias_opts[optname]
432
proc_argv.insert(0, a)
435
raise BzrCommandError('repeated option %r' % a)
437
option_obj = cmd_options[optname]
438
optargfn = option_obj.type
442
raise BzrCommandError('option %r needs an argument' % a)
444
optarg = proc_argv.pop(0)
445
opts[optname] = optargfn(optarg)
447
alias_opts[optname] = optargfn(optarg)
450
raise BzrCommandError('option %r takes no argument' % optname)
453
alias_opts[optname] = True
456
proc_aliasarg = False # Done with alias argv
380
457
return args, opts
397
474
argdict[argname + '_list'] = None
398
475
elif ap[-1] == '+':
400
raise errors.BzrCommandError("command %r needs one or more %s"
401
% (cmd, argname.upper()))
477
raise BzrCommandError("command %r needs one or more %s"
478
% (cmd, argname.upper()))
403
480
argdict[argname + '_list'] = args[:]
405
482
elif ap[-1] == '$': # all but one
406
483
if len(args) < 2:
407
raise errors.BzrCommandError("command %r needs one or more %s"
408
% (cmd, argname.upper()))
484
raise BzrCommandError("command %r needs one or more %s"
485
% (cmd, argname.upper()))
409
486
argdict[argname + '_list'] = args[:-1]
412
489
# just a plain arg
415
raise errors.BzrCommandError("command %r requires argument %s"
416
% (cmd, argname.upper()))
492
raise BzrCommandError("command %r requires argument %s"
493
% (cmd, argname.upper()))
418
495
argdict[argname] = args.pop(0)
421
raise errors.BzrCommandError("extra argument to command %s: %s"
498
raise BzrCommandError("extra argument to command %s: %s"
563
640
alias_argv = [a.decode(bzrlib.user_encoding) for a in alias_argv]
564
641
argv[0] = alias_argv.pop(0)
567
# We want only 'ascii' command names, but the user may have typed
568
# in a Unicode name. In that case, they should just get a
569
# 'command not found' error later.
643
cmd = str(argv.pop(0))
571
645
cmd_obj = get_cmd_object(cmd, plugins_override=not opt_builtin)
572
646
if not getattr(cmd_obj.run_argv, 'is_deprecated', False):
623
697
return run_bzr(argv)
624
698
# do this here inside the exception wrappers to catch EPIPE
625
699
sys.stdout.flush()
626
except (KeyboardInterrupt, Exception), e:
627
701
# used to handle AssertionError and KeyboardInterrupt
628
702
# specially here, but hopefully they're handled ok by the logger now
629
trace.report_exception(sys.exc_info(), sys.stderr)
703
bzrlib.trace.report_exception(sys.exc_info(), sys.stderr)
630
704
if os.environ.get('BZR_PDB'):
631
705
print '**** entering debugger'