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
38
from warnings import warn
38
import bzrlib.errors as errors
39
from bzrlib.errors import (BzrError,
49
from bzrlib.symbol_versioning import (
43
56
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
64
73
k_unsquished = _unsquish_command_name(k)
67
if not plugin_cmds.has_key(k_unsquished):
76
if k_unsquished not in plugin_cmds:
68
77
plugin_cmds[k_unsquished] = cmd
69
mutter('registered plugin command %s', k_unsquished)
78
trace.mutter('registered plugin command %s', k_unsquished)
70
79
if decorate and k_unsquished in builtin_command_names():
71
80
return _builtin_commands()[k_unsquished]
130
139
from bzrlib.externalcommand import ExternalCommand
132
cmd_name = str(cmd_name) # not unicode
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.
134
146
# first look up this command under the specified name
135
147
cmds = _get_cmd_dict(plugins_override=plugins_override)
257
269
def run_argv_aliases(self, argv, alias_argv=None):
258
270
"""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)
259
275
args, opts = parse_args(self, argv, alias_argv)
260
276
if 'help' in opts: # e.g. bzr add --help
261
277
from bzrlib.help import help_on_command
262
278
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()))
270
280
# mix arguments and options into one dictionary
271
281
cmdargs = _match_argform(self.name(), self.takes_args, args)
354
367
lookup table, something about the available options, what optargs
355
368
they take, and which commands will accept them.
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
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])
457
380
return args, opts
474
397
argdict[argname + '_list'] = None
475
398
elif ap[-1] == '+':
477
raise BzrCommandError("command %r needs one or more %s"
478
% (cmd, argname.upper()))
400
raise errors.BzrCommandError("command %r needs one or more %s"
401
% (cmd, argname.upper()))
480
403
argdict[argname + '_list'] = args[:]
482
405
elif ap[-1] == '$': # all but one
483
406
if len(args) < 2:
484
raise BzrCommandError("command %r needs one or more %s"
485
% (cmd, argname.upper()))
407
raise errors.BzrCommandError("command %r needs one or more %s"
408
% (cmd, argname.upper()))
486
409
argdict[argname + '_list'] = args[:-1]
489
412
# just a plain arg
492
raise BzrCommandError("command %r requires argument %s"
493
% (cmd, argname.upper()))
415
raise errors.BzrCommandError("command %r requires argument %s"
416
% (cmd, argname.upper()))
495
418
argdict[argname] = args.pop(0)
498
raise BzrCommandError("extra argument to command %s: %s"
421
raise errors.BzrCommandError("extra argument to command %s: %s"
640
563
alias_argv = [a.decode(bzrlib.user_encoding) for a in alias_argv]
641
564
argv[0] = alias_argv.pop(0)
643
cmd = str(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.
645
571
cmd_obj = get_cmd_object(cmd, plugins_override=not opt_builtin)
646
572
if not getattr(cmd_obj.run_argv, 'is_deprecated', False):
697
623
return run_bzr(argv)
698
624
# do this here inside the exception wrappers to catch EPIPE
699
625
sys.stdout.flush()
626
except (KeyboardInterrupt, Exception), e:
701
627
# used to handle AssertionError and KeyboardInterrupt
702
628
# specially here, but hopefully they're handled ok by the logger now
703
bzrlib.trace.report_exception(sys.exc_info(), sys.stderr)
629
trace.report_exception(sys.exc_info(), sys.stderr)
704
630
if os.environ.get('BZR_PDB'):
705
631
print '**** entering debugger'