357
358
summary, then a complete description of the command. A grammar
358
359
description will be inserted.
361
Other accepted names for this command.
364
List of argument forms, marked with whether they are optional,
369
['to_location', 'from_branch?', 'file*']
371
'to_location' is required
372
'from_branch' is optional
373
'file' can be specified 0 or more times
376
List of options that may be given for this command. These can
377
be either strings, referring to globally-defined options,
378
or option objects. Retrieve through options().
381
If true, this command isn't advertised. This is typically
361
:cvar aliases: Other accepted names for this command.
363
:cvar takes_args: List of argument forms, marked with whether they are
364
optional, repeated, etc. Examples::
366
['to_location', 'from_branch?', 'file*']
368
* 'to_location' is required
369
* 'from_branch' is optional
370
* 'file' can be specified 0 or more times
372
:cvar takes_options: List of options that may be given for this command.
373
These can be either strings, referring to globally-defined options, or
374
option objects. Retrieve through options().
376
:cvar hidden: If true, this command isn't advertised. This is typically
382
377
for commands intended for expert users.
385
Command objects will get a 'outf' attribute, which has been
386
setup to properly handle encoding of unicode strings.
387
encoding_type determines what will happen when characters cannot
389
strict - abort if we cannot decode
390
replace - put in a bogus character (typically '?')
391
exact - do not encode sys.stdout
393
NOTE: by default on Windows, sys.stdout is opened as a text
394
stream, therefore LF line-endings are converted to CRLF.
395
When a command uses encoding_type = 'exact', then
396
sys.stdout is forced to be a binary stream, and line-endings
379
:cvar encoding_type: Command objects will get a 'outf' attribute, which has
380
been setup to properly handle encoding of unicode strings.
381
encoding_type determines what will happen when characters cannot be
384
* strict - abort if we cannot decode
385
* replace - put in a bogus character (typically '?')
386
* exact - do not encode sys.stdout
388
NOTE: by default on Windows, sys.stdout is opened as a text stream,
389
therefore LF line-endings are converted to CRLF. When a command uses
390
encoding_type = 'exact', then sys.stdout is forced to be a binary
391
stream, and line-endings will not mangled.
394
A string indicating the real name under which this command was
395
invoked, before expansion of aliases.
396
(This may be None if the command was constructed and run in-process.)
399
398
:cvar hooks: An instance of CommandHooks.
400
:cvar __doc__: The help shown by 'bzr help command' for this command.
401
This is set by assigning explicitly to __doc__ so that -OO can
405
__doc__ = "My help goes here"
403
409
takes_options = []
404
410
encoding_type = 'strict'
408
415
def __init__(self):
409
416
"""Construct an instance of this command."""
410
if self.__doc__ == Command.__doc__:
411
warn("No help message set for %r" % self)
412
417
# List of standard options directly supported
413
418
self.supported_std_options = []
414
419
self._setup_run()
481
486
usage help (e.g. Purpose, Usage, Options) with a
482
487
message explaining how to obtain full help.
489
cmd_gettext = self.get_gettext() # gettext() for command help
484
490
doc = self.help()
486
raise NotImplementedError("sorry, no detailed help yet for %r" % self.name())
492
# NOTE: If cmd_gettext translates ':Usage:\n', the section will
493
# be shown after "Description" section.
494
doc = cmd_gettext(doc)
496
doc = gettext("No help for this command.")
488
498
# Extract the summary (purpose) and sections out from the text
489
499
purpose,sections,order = self._get_help_parts(doc)
508
518
# XXX: optparse implicitly rewraps the help, and not always perfectly,
509
519
# 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:
521
options = option.get_optparser(self.options(), True)
522
options = options.format_option_help()
523
# FIXME: According to the spec, ReST option lists actually don't
524
# support options like --1.14 so that causes syntax errors (in Sphinx
525
# at least). As that pattern always appears in the commands that
526
# break, we trap on that and then format that block of 'format' options
527
# as a literal block. We use the most recent format still listed so we
528
# don't have to do that too often -- vila 20110514
529
if not plain and options.find(' --1.14 ') != -1:
518
530
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:'):]
531
if options.startswith('Options:') or options.startswith('options:'):
532
# Python 2.4 version of optparse uses 'options'.
533
result += ':%s:%s' % (gettext('Options'), options[len('options:'):])
525
535
result += options
531
541
if sections.has_key(None):
532
542
text = sections.pop(None)
533
543
text = '\n '.join(text.splitlines())
534
result += ':%s:\n %s\n\n' % ('Description',text)
544
result += ':%s:\n %s\n\n' % (gettext('Description'),text)
536
546
# Add the custom sections (e.g. Examples). Note that there's no need
537
547
# to indent these as they must be indented already in the source.
539
549
for label in order:
540
if sections.has_key(label):
541
result += ':%s:\n%s\n' % (label,sections[label])
550
if label in sections:
551
result += ':%s:\n%s\n' % (label, sections[label])
544
result += ("See bzr help %s for more details and examples.\n\n"
554
result += (gettext("See bzr help %s for more details and examples.\n\n")
547
557
# Add the aliases, source (plug-in) and see also links, if any
549
result += ':Aliases: '
559
result += ':%s: ' % gettext('Aliases')
550
560
result += ', '.join(self.aliases) + '\n'
551
561
plugin_name = self.plugin_name()
552
562
if plugin_name is not None:
762
794
These are all empty initially, because by default nothing should get
766
self.create_hook(HookPoint('extend_command',
797
Hooks.__init__(self, "bzrlib.commands", "Command.hooks")
798
self.add_hook('extend_command',
767
799
"Called after creating a command object to allow modifications "
768
800
"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',
801
"new bzrlib.commands.Command object.", (1, 13))
802
self.add_hook('get_command',
771
803
"Called when creating a single command. Called with "
772
804
"(cmd_or_None, command_name). get_command should either return "
773
805
"the cmd_or_None parameter, or a replacement Command object that "
774
806
"should be used for the command. Note that the Command.hooks "
775
807
"hooks are core infrastructure. Many users will prefer to use "
776
808
"bzrlib.commands.register_command or plugin_cmds.register_lazy.",
778
self.create_hook(HookPoint('get_missing_command',
810
self.add_hook('get_missing_command',
779
811
"Called when creating a single command if no command could be "
780
812
"found. Called with (command_name). get_missing_command should "
781
813
"either return None, or a Command object to be used for the "
782
"command.", (1, 17), None))
783
self.create_hook(HookPoint('list_commands',
815
self.add_hook('list_commands',
784
816
"Called when enumerating commands. Called with a set of "
785
817
"cmd_name strings for all the commands found so far. This set "
786
818
" is safe to mutate - e.g. to remove a command. "
787
819
"list_commands should return the updated set of command names.",
790
822
Command.hooks = CommandHooks()
808
options, args = parser.parse_args(args)
840
# for python 2.5 and later, optparse raises this exception if a non-ascii
841
# option name is given. See http://bugs.python.org/issue2931
843
options, args = parser.parse_args(args)
844
except UnicodeEncodeError,e:
845
raise errors.BzrCommandError('Only ASCII permitted in option names')
809
847
opts = dict([(k, v) for k, v in options.__dict__.iteritems() if
810
848
v is not option.OptionParser.DEFAULT_VALUE])
811
849
return args, opts
1077
1122
if not opt_no_aliases:
1078
1123
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
1125
argv[0] = alias_argv.pop(0)
1084
1127
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
1128
cmd_obj = get_cmd_object(cmd, plugins_override=not opt_builtin)
1090
1129
run = cmd_obj.run_argv_aliases
1091
1130
run_argv = [argv, alias_argv]
1252
1290
class Provider(object):
1253
'''Generic class to be overriden by plugins'''
1291
"""Generic class to be overriden by plugins"""
1255
1293
def plugin_for_command(self, cmd_name):
1256
'''Takes a command and returns the information for that plugin
1294
"""Takes a command and returns the information for that plugin
1258
1296
:return: A dictionary with all the available information
1259
for the requested plugin
1297
for the requested plugin
1261
1299
raise NotImplementedError
1264
1302
class ProvidersRegistry(registry.Registry):
1265
'''This registry exists to allow other providers to exist'''
1303
"""This registry exists to allow other providers to exist"""
1267
1305
def __iter__(self):
1268
1306
for key, provider in self.iteritems():