49
from bzrlib.hooks import HookPoint, Hooks
46
from bzrlib.hooks import Hooks
50
47
# Compatibility - Option used to be in commands.
51
48
from bzrlib.option import Option
52
49
from bzrlib.plugin import disable_plugins, load_plugins
396
396
sys.stdout is forced to be a binary stream, and line-endings
397
397
will not mangled.
400
A string indicating the real name under which this command was
401
invoked, before expansion of aliases.
402
(This may be None if the command was constructed and run in-process.)
399
404
:cvar hooks: An instance of CommandHooks.
406
:ivar __doc__: The help shown by 'bzr help command' for this command.
407
This is set by assigning explicitly to __doc__ so that -OO can
411
__doc__ = "My help goes here"
403
415
takes_options = []
404
416
encoding_type = 'strict'
408
421
def __init__(self):
409
422
"""Construct an instance of this command."""
410
if self.__doc__ == Command.__doc__:
411
warn("No help message set for %r" % self)
412
423
# List of standard options directly supported
413
424
self.supported_std_options = []
414
425
self._setup_run()
481
492
usage help (e.g. Purpose, Usage, Options) with a
482
493
message explaining how to obtain full help.
495
from bzrlib.i18n import gettext # gettext() for fixed string
496
cmd_gettext = self.get_gettext() # gettext() for command help
484
497
doc = self.help()
486
raise NotImplementedError("sorry, no detailed help yet for %r" % self.name())
499
# NOTE: If cmd_gettext translates ':Usage:\n', the section will
500
# be shown after "Description" section.
501
# Additionally, ZzzTranslation translates ':Label...' into
502
# 'zz{{:Label...}}'. So all sections are broken and shown in
503
# the "Description" section.
504
doc = cmd_gettext(doc)
506
doc = gettext("No help for this command.")
488
508
# Extract the summary (purpose) and sections out from the text
489
509
purpose,sections,order = self._get_help_parts(doc)
497
517
# The header is the purpose and usage
499
result += ':Purpose: %s\n' % purpose
519
result += ':%s: %s\n' % (gettext('Purpose'), purpose)
500
520
if usage.find('\n') >= 0:
501
result += ':Usage:\n%s\n' % usage
521
result += ':%s:\n%s\n' % (gettext('Usage'), usage)
503
result += ':Usage: %s\n' % usage
523
result += ':%s: %s\n' % (gettext('Usage'), usage)
506
526
# Add the options
508
528
# XXX: optparse implicitly rewraps the help, and not always perfectly,
509
529
# so we get <https://bugs.launchpad.net/bzr/+bug/249908>. -- mbp
511
options = option.get_optparser(self.options()).format_option_help()
512
# XXX: According to the spec, ReST option lists actually don't support
513
# options like --1.9 so that causes syntax errors (in Sphinx at least).
514
# As that pattern always appears in the commands that break, we trap
515
# on that and then format that block of 'format' options as a literal
517
if not plain and options.find(' --1.9 ') != -1:
531
options = option.get_optparser(self.options(), True)
532
options = options.format_option_help()
533
# FIXME: According to the spec, ReST option lists actually don't
534
# support options like --1.14 so that causes syntax errors (in Sphinx
535
# at least). As that pattern always appears in the commands that
536
# break, we trap on that and then format that block of 'format' options
537
# as a literal block. We use the most recent format still listed so we
538
# don't have to do that too often -- vila 20110514
539
if not plain and options.find(' --1.14 ') != -1:
518
540
options = options.replace(' format:\n', ' format::\n\n', 1)
519
if options.startswith('Options:'):
520
result += ':' + options
521
elif options.startswith('options:'):
522
# Python 2.4 version of optparse
523
result += ':Options:' + options[len('options:'):]
541
if options.startswith('Options:') or options.startswith('options:'):
542
# Python 2.4 version of optparse uses 'options'.
543
result += ':%s:%s' % (gettext('Options'), options[len('options:'):])
525
545
result += options
531
551
if sections.has_key(None):
532
552
text = sections.pop(None)
533
553
text = '\n '.join(text.splitlines())
534
result += ':%s:\n %s\n\n' % ('Description',text)
554
result += ':%s:\n %s\n\n' % (gettext('Description'),text)
536
556
# Add the custom sections (e.g. Examples). Note that there's no need
537
557
# to indent these as they must be indented already in the source.
539
559
for label in order:
540
if sections.has_key(label):
541
result += ':%s:\n%s\n' % (label,sections[label])
560
if label in sections:
561
result += ':%s:\n%s\n' % (label, sections[label])
544
result += ("See bzr help %s for more details and examples.\n\n"
564
result += (gettext("See bzr help %s for more details and examples.\n\n")
547
567
# Add the aliases, source (plug-in) and see also links, if any
549
result += ':Aliases: '
569
result += ':%s: ' % gettext('Aliases')
550
570
result += ', '.join(self.aliases) + '\n'
551
571
plugin_name = self.plugin_name()
552
572
if plugin_name is not None:
565
585
link_text = ":doc:`%s <%s-help>`" % (item, item)
566
586
see_also_links.append(link_text)
567
587
see_also = see_also_links
568
result += ':See also: '
588
result += ':%s: ' % gettext('See also')
569
589
result += ', '.join(see_also) + '\n'
571
591
# If this will be rendered as plain text, convert it
657
677
# Process the standard options
658
678
if 'help' in opts: # e.g. bzr add --help
659
sys.stdout.write(self.get_help_text())
680
self.outf.write(self.get_help_text())
661
682
if 'usage' in opts: # e.g. bzr add --usage
662
683
sys.stdout.write(self.get_help_text(verbose=False))
683
704
self._setup_outf()
685
return self.run(**all_cmd_args)
707
return self.run(**all_cmd_args)
709
# reset it, so that other commands run in the same process won't
710
# inherit state. Before we reset it, log any activity, so that it
711
# gets properly tracked.
712
ui.ui_factory.log_transport_activity(
713
display=('bytes' in debug.debug_flags))
714
trace.set_verbosity_level(0)
687
716
def _setup_run(self):
688
717
"""Wrap the defined run method on self with a cleanup.
739
768
return getdoc(self)
772
"""Returns the gettext function used to translate this command's help.
774
NOTE: Commands provided by plugins should override this to use own
777
from bzrlib.i18n import gettext
781
"""Return the canonical name for this command.
783
The name under which it was actually invoked is available in invoked_as.
742
785
return _unsquish_command_name(self.__class__.__name__)
744
787
def plugin_name(self):
762
805
These are all empty initially, because by default nothing should get
766
self.create_hook(HookPoint('extend_command',
808
Hooks.__init__(self, "bzrlib.commands", "Command.hooks")
809
self.add_hook('extend_command',
767
810
"Called after creating a command object to allow modifications "
768
811
"such as adding or removing options, docs etc. Called with the "
769
"new bzrlib.commands.Command object.", (1, 13), None))
770
self.create_hook(HookPoint('get_command',
812
"new bzrlib.commands.Command object.", (1, 13))
813
self.add_hook('get_command',
771
814
"Called when creating a single command. Called with "
772
815
"(cmd_or_None, command_name). get_command should either return "
773
816
"the cmd_or_None parameter, or a replacement Command object that "
774
817
"should be used for the command. Note that the Command.hooks "
775
818
"hooks are core infrastructure. Many users will prefer to use "
776
819
"bzrlib.commands.register_command or plugin_cmds.register_lazy.",
778
self.create_hook(HookPoint('get_missing_command',
821
self.add_hook('get_missing_command',
779
822
"Called when creating a single command if no command could be "
780
823
"found. Called with (command_name). get_missing_command should "
781
824
"either return None, or a Command object to be used for the "
782
"command.", (1, 17), None))
783
self.create_hook(HookPoint('list_commands',
826
self.add_hook('list_commands',
784
827
"Called when enumerating commands. Called with a set of "
785
828
"cmd_name strings for all the commands found so far. This set "
786
829
" is safe to mutate - e.g. to remove a command. "
787
830
"list_commands should return the updated set of command names.",
790
833
Command.hooks = CommandHooks()
808
options, args = parser.parse_args(args)
851
# for python 2.5 and later, optparse raises this exception if a non-ascii
852
# option name is given. See http://bugs.python.org/issue2931
854
options, args = parser.parse_args(args)
855
except UnicodeEncodeError,e:
856
raise errors.BzrCommandError('Only ASCII permitted in option names')
809
858
opts = dict([(k, v) for k, v in options.__dict__.iteritems() if
810
859
v is not option.OptionParser.DEFAULT_VALUE])
811
860
return args, opts
1015
1064
Specify the number of processes that can be run concurrently (selftest).
1017
1066
trace.mutter("bazaar version: " + bzrlib.__version__)
1067
argv = _specified_or_unicode_argv(argv)
1019
1068
trace.mutter("bzr arguments: %r", argv)
1021
opt_lsprof = opt_profile = opt_no_plugins = opt_builtin = \
1022
opt_no_aliases = False
1070
opt_lsprof = opt_profile = opt_no_plugins = opt_builtin = \
1071
opt_no_i18n = opt_no_aliases = False
1023
1072
opt_lsprof_file = opt_coverage_dir = None
1025
1074
# --no-plugins is handled specially at a very early stage. We need
1077
1136
if not opt_no_aliases:
1078
1137
alias_argv = get_alias(argv[0])
1080
user_encoding = osutils.get_user_encoding()
1081
alias_argv = [a.decode(user_encoding) for a in alias_argv]
1082
1139
argv[0] = alias_argv.pop(0)
1084
1141
cmd = argv.pop(0)
1085
# We want only 'ascii' command names, but the user may have typed
1086
# in a Unicode name. In that case, they should just get a
1087
# 'command not found' error later.
1089
1142
cmd_obj = get_cmd_object(cmd, plugins_override=not opt_builtin)
1090
1143
run = cmd_obj.run_argv_aliases
1091
1144
run_argv = [argv, alias_argv]
1188
1241
:return: exit code of bzr command.
1190
argv = _specified_or_unicode_argv(argv)
1243
if argv is not None:
1191
1245
_register_builtin_commands()
1192
1246
ret = run_bzr_catch_errors(argv)
1193
bzrlib.ui.ui_factory.log_transport_activity(
1194
display=('bytes' in debug.debug_flags))
1195
1247
trace.mutter("return code %d", ret)