138
138
If true, plugin commands can override builtins.
141
return _get_cmd_object(cmd_name, plugins_override)
143
raise errors.BzrCommandError('unknown command "%s"' % cmd_name)
146
def _get_cmd_object(cmd_name, plugins_override=True):
147
"""Worker for get_cmd_object which raises KeyError rather than BzrCommandError."""
140
148
from bzrlib.externalcommand import ExternalCommand
142
150
# We want only 'ascii' command names, but the user may have typed
233
240
if self.__doc__ == Command.__doc__:
234
241
warn("No help message set for %r" % self)
236
def get_see_also(self):
244
"""Return single-line grammar for this command.
246
Only describes arguments, not options.
248
s = 'bzr ' + self.name() + ' '
249
for aname in self.takes_args:
250
aname = aname.upper()
251
if aname[-1] in ['$', '+']:
252
aname = aname[:-1] + '...'
253
elif aname[-1] == '?':
254
aname = '[' + aname[:-1] + ']'
255
elif aname[-1] == '*':
256
aname = '[' + aname[:-1] + '...]'
263
def get_help_text(self, additional_see_also=None):
264
"""Return a text string with help for this command.
266
:param additional_see_also: Additional help topics to be
271
raise NotImplementedError("sorry, no detailed help yet for %r" % self.name())
274
result += 'usage: %s\n' % self._usage()
277
result += 'aliases:\n'
278
result += ', '.join(self.aliases) + '\n'
282
plugin_name = self.plugin_name()
283
if plugin_name is not None:
284
result += '(From plugin "%s")' % plugin_name
288
if result[-1] != '\n':
291
result += option.get_optparser(self.options()).format_option_help()
292
see_also = self.get_see_also(additional_see_also)
294
result += '\nSee also: '
295
result += ', '.join(see_also)
299
def get_help_topic(self):
300
"""Return the commands help topic - its name."""
303
def get_see_also(self, additional_terms=None):
237
304
"""Return a list of help topics that are related to this ommand.
239
306
The list is derived from the content of the _see_also attribute. Any
240
307
duplicates are removed and the result is in lexical order.
308
:param additional_terms: Additional help topics to cross-reference.
241
309
:return: A list of help topics.
243
return sorted(set(getattr(self, '_see_also', [])))
311
see_also = set(getattr(self, '_see_also', []))
313
see_also.update(additional_terms)
314
return sorted(see_also)
245
316
def options(self):
246
317
"""Return dict of valid options for this command.
289
360
args, opts = parse_args(self, argv, alias_argv)
290
361
if 'help' in opts: # e.g. bzr add --help
291
from bzrlib.help import help_on_command
292
help_on_command(self.name())
362
sys.stdout.write(self.get_help_text())
294
364
# mix arguments and options into one dictionary
295
365
cmdargs = _match_argform(self.name(), self.takes_args, args)
657
727
pdb.post_mortem(sys.exc_traceback)
731
class HelpCommandIndex(object):
732
"""A index for bzr help that returns commands."""
735
self.prefix = 'commands/'
737
def get_topics(self, topic):
738
"""Search for topic amongst commands.
740
:param topic: A topic to search for.
741
:return: A list which is either empty or contains a single
744
if topic and topic.startswith(self.prefix):
745
topic = topic[len(self.prefix):]
747
cmd = _get_cmd_object(topic)
660
754
if __name__ == '__main__':
661
755
sys.exit(main(sys.argv))