1
# Copyright (C) 2004, 2005 by Canonical Ltd
1
# Copyright (C) 2006 by Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
11
# GNU General Public License for more details.
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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32
33
from warnings import warn
33
from inspect import getdoc
38
from bzrlib.trace import mutter, note, log_error, warning, be_quiet
39
from bzrlib.errors import (BzrError,
37
from bzrlib.errors import (BzrError,
42
from bzrlib.option import Option
44
43
from bzrlib.revisionspec import RevisionSpec
45
from bzrlib import BZRDIR
46
from bzrlib.option import Option
44
from bzrlib.symbol_versioning import *
46
from bzrlib.trace import mutter, note, log_error, warning, be_quiet
51
51
def register_command(cmd, decorate=False):
52
"Utility function to help register a command"
52
"""Utility function to help register a command
54
:param cmd: Command subclass to register
55
:param decorate: If true, allow overriding an existing command
56
of the same name; the old command is returned by this function.
57
Otherwise it is an error to try to override an existing command.
55
61
if k.startswith("cmd_"):
216
@deprecated_method(zero_eight)
211
217
def run_argv(self, argv):
212
"""Parse command line and run."""
213
args, opts = parse_args(self, argv)
218
"""Parse command line and run.
220
See run_argv_aliases for the 0.8 and beyond api.
222
return self.run_argv_aliases(argv)
224
def run_argv_aliases(self, argv, alias_argv=None):
225
"""Parse the command line and run with extra aliases in alias_argv."""
226
args, opts = parse_args(self, argv, alias_argv)
214
227
if 'help' in opts: # e.g. bzr add --help
215
228
from bzrlib.help import help_on_command
216
229
help_on_command(self.name())
297
311
# TODO: chop up this beast; make it a method of the Command
301
316
cmd_options = command.options()
309
# We've received a standalone -- No more flags
313
# option names must not be unicode
317
mutter(" got option %r", a)
319
optname, optarg = a[2:].split('=', 1)
322
if optname not in cmd_options:
323
raise BzrOptionError('unknown long option %r for command %s'
324
% (a, command.name()))
327
if shortopt in Option.SHORT_OPTIONS:
328
# Multi-character options must have a space to delimit
330
# ^^^ what does this mean? mbp 20051014
331
optname = Option.SHORT_OPTIONS[shortopt].name
333
# Single character short options, can be chained,
334
# and have their value appended to their name
336
if shortopt not in Option.SHORT_OPTIONS:
337
# We didn't find the multi-character name, and we
338
# didn't find the single char name
339
raise BzrError('unknown short option %r' % a)
340
optname = Option.SHORT_OPTIONS[shortopt].name
318
proc_aliasarg = True # Are we processing alias_argv now?
319
for proc_argv in alias_argv, argv:
326
# We've received a standalone -- No more flags
330
# option names must not be unicode
334
mutter(" got option %r", a)
336
optname, optarg = a[2:].split('=', 1)
339
if optname not in cmd_options:
340
raise BzrOptionError('unknown long option %r for'
345
if shortopt in Option.SHORT_OPTIONS:
346
# Multi-character options must have a space to delimit
348
# ^^^ what does this mean? mbp 20051014
349
optname = Option.SHORT_OPTIONS[shortopt].name
351
# Single character short options, can be chained,
352
# and have their value appended to their name
354
if shortopt not in Option.SHORT_OPTIONS:
355
# We didn't find the multi-character name, and we
356
# didn't find the single char name
357
raise BzrError('unknown short option %r' % a)
358
optname = Option.SHORT_OPTIONS[shortopt].name
343
# There are extra things on this option
344
# see if it is the value, or if it is another
346
optargfn = Option.OPTIONS[optname].type
348
# This option does not take an argument, so the
349
# next entry is another short option, pack it back
351
argv.insert(0, '-' + a[2:])
361
# There are extra things on this option
362
# see if it is the value, or if it is another
364
optargfn = Option.OPTIONS[optname].type
366
# This option does not take an argument, so the
367
# next entry is another short option, pack it
369
proc_argv.insert(0, '-' + a[2:])
371
# This option takes an argument, so pack it
375
if optname not in cmd_options:
376
raise BzrOptionError('unknown short option %r for'
378
(shortopt, command.name()))
380
# XXX: Do we ever want to support this, e.g. for -r?
382
raise BzrError('repeated option %r' % a)
383
elif optname in alias_opts:
384
# Replace what's in the alias with what's in the real
386
del alias_opts[optname]
388
proc_argv.insert(0, a)
391
raise BzrError('repeated option %r' % a)
393
option_obj = cmd_options[optname]
394
optargfn = option_obj.type
398
raise BzrError('option %r needs an argument' % a)
353
# This option takes an argument, so pack it
357
if optname not in cmd_options:
358
raise BzrOptionError('unknown short option %r for command'
359
' %s' % (shortopt, command.name()))
361
# XXX: Do we ever want to support this, e.g. for -r?
362
raise BzrError('repeated option %r' % a)
364
option_obj = cmd_options[optname]
365
optargfn = option_obj.type
369
raise BzrError('option %r needs an argument' % a)
372
opts[optname] = optargfn(optarg)
400
optarg = proc_argv.pop(0)
401
opts[optname] = optargfn(optarg)
403
alias_opts[optname] = optargfn(optarg)
406
raise BzrError('option %r takes no argument' % optname)
409
alias_opts[optname] = True
375
raise BzrError('option %r takes no argument' % optname)
412
proc_aliasarg = False # Done with alias argv
379
413
return args, opts
465
522
Do not load plugin modules at all
468
528
Only use builtin commands. (Plugins are still allowed to change
469
529
other behaviour.)
472
Run under the Python profiler.
532
Run under the Python hotshot profiler.
535
Run under the Python lsprof profiler.
474
537
argv = [a.decode(bzrlib.user_encoding) for a in argv]
476
opt_profile = opt_no_plugins = opt_builtin = False
539
opt_lsprof = opt_profile = opt_no_plugins = opt_builtin = \
540
opt_no_aliases = False
541
opt_lsprof_file = None
478
543
# --no-plugins is handled specially at a very early stage. We need
479
544
# to load plugins before doing other command parsing so that they
480
545
# can override commands, but this needs to happen first.
483
551
if a == '--profile':
484
552
opt_profile = True
553
elif a == '--lsprof':
555
elif a == '--lsprof-file':
556
opt_lsprof_file = argv[i + 1]
485
558
elif a == '--no-plugins':
486
559
opt_no_plugins = True
560
elif a == '--no-aliases':
561
opt_no_aliases = True
487
562
elif a == '--builtin':
488
563
opt_builtin = True
489
564
elif a in ('--quiet', '-q'):
495
if (not argv) or (argv[0] == '--help'):
496
from bzrlib.help import help
572
from bzrlib.builtins import cmd_help
573
cmd_help().run_argv_aliases([])
503
576
if argv[0] == '--version':
508
581
if not opt_no_plugins:
509
582
from bzrlib.plugin import load_plugins
585
from bzrlib.plugin import disable_plugins
590
if not opt_no_aliases:
591
alias_argv = get_alias(argv[0])
593
alias_argv = [a.decode(bzrlib.user_encoding) for a in alias_argv]
594
argv[0] = alias_argv.pop(0)
512
596
cmd = str(argv.pop(0))
514
598
cmd_obj = get_cmd_object(cmd, plugins_override=not opt_builtin)
599
if not getattr(cmd_obj.run_argv, 'is_deprecated', False):
600
run = cmd_obj.run_argv
603
run = cmd_obj.run_argv_aliases
604
run_argv = [argv, alias_argv]
518
ret = apply_profiled(cmd_obj.run_argv, argv)
608
ret = apply_lsprofiled(opt_lsprof_file, run, *run_argv)
610
ret = apply_profiled(run, *run_argv)
520
ret = cmd_obj.run_argv(argv)
523
615
# reset, in case we may do other commands later within the same process