139
138
If true, plugin commands can override builtins.
142
return _get_cmd_object(cmd_name, plugins_override)
144
raise errors.BzrCommandError('unknown command "%s"' % cmd_name)
147
def _get_cmd_object(cmd_name, plugins_override=True):
148
"""Worker for get_cmd_object which raises KeyError rather than BzrCommandError."""
149
140
from bzrlib.externalcommand import ExternalCommand
151
142
# We want only 'ascii' command names, but the user may have typed
241
233
if self.__doc__ == Command.__doc__:
242
234
warn("No help message set for %r" % self)
244
def _maybe_expand_globs(self, file_list):
245
"""Glob expand file_list if the platform does not do that itself.
247
:return: A possibly empty list of unicode paths.
249
Introduced in bzrlib 0.18.
253
if sys.platform == 'win32':
254
file_list = win32utils.glob_expand(file_list)
255
return list(file_list)
258
"""Return single-line grammar for this command.
260
Only describes arguments, not options.
262
s = 'bzr ' + self.name() + ' '
263
for aname in self.takes_args:
264
aname = aname.upper()
265
if aname[-1] in ['$', '+']:
266
aname = aname[:-1] + '...'
267
elif aname[-1] == '?':
268
aname = '[' + aname[:-1] + ']'
269
elif aname[-1] == '*':
270
aname = '[' + aname[:-1] + '...]'
277
def get_help_text(self, additional_see_also=None):
278
"""Return a text string with help for this command.
280
:param additional_see_also: Additional help topics to be
285
raise NotImplementedError("sorry, no detailed help yet for %r" % self.name())
288
result += 'usage: %s\n' % self._usage()
291
result += 'aliases: '
292
result += ', '.join(self.aliases) + '\n'
296
plugin_name = self.plugin_name()
297
if plugin_name is not None:
298
result += '(From plugin "%s")' % plugin_name
302
if result[-1] != '\n':
305
result += option.get_optparser(self.options()).format_option_help()
306
see_also = self.get_see_also(additional_see_also)
308
result += '\nSee also: '
309
result += ', '.join(see_also)
313
def get_help_topic(self):
314
"""Return the commands help topic - its name."""
317
def get_see_also(self, additional_terms=None):
318
"""Return a list of help topics that are related to this ommand.
320
The list is derived from the content of the _see_also attribute. Any
321
duplicates are removed and the result is in lexical order.
322
:param additional_terms: Additional help topics to cross-reference.
323
:return: A list of help topics.
325
see_also = set(getattr(self, '_see_also', []))
327
see_also.update(additional_terms)
328
return sorted(see_also)
330
236
def options(self):
331
237
"""Return dict of valid options for this command.
333
239
Maps from long option name to option object."""
335
r['help'] = option._help_option
241
r['help'] = option.Option.OPTIONS['help']
336
242
for o in self.takes_options:
337
243
if isinstance(o, basestring):
338
244
o = option.Option.OPTIONS[o]
365
271
# bogus. So set the attribute, so we can find the correct encoding later.
366
272
self.outf.encoding = output_encoding
274
@deprecated_method(zero_eight)
275
def run_argv(self, argv):
276
"""Parse command line and run.
278
See run_argv_aliases for the 0.8 and beyond api.
280
return self.run_argv_aliases(argv)
368
282
def run_argv_aliases(self, argv, alias_argv=None):
369
283
"""Parse the command line and run with extra aliases in alias_argv."""
374
288
args, opts = parse_args(self, argv, alias_argv)
375
289
if 'help' in opts: # e.g. bzr add --help
376
sys.stdout.write(self.get_help_text())
290
from bzrlib.help import help_on_command
291
help_on_command(self.name())
378
293
# mix arguments and options into one dictionary
379
294
cmdargs = _match_argform(self.name(), self.takes_args, args)
549
464
def apply_lsprofiled(filename, the_callable, *args, **kwargs):
550
465
from bzrlib.lsprof import profile
551
467
ret, stats = profile(the_callable, *args, **kwargs)
553
469
if filename is None:
557
trace.note('Profile data written to "%s".', filename)
473
cPickle.dump(stats, open(filename, 'w'), 2)
474
print 'Profile data written to %r.' % filename
679
596
# 'command not found' error later.
681
598
cmd_obj = get_cmd_object(cmd, plugins_override=not opt_builtin)
682
run = cmd_obj.run_argv_aliases
683
run_argv = [argv, alias_argv]
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]
737
660
pdb.post_mortem(sys.exc_traceback)
741
class HelpCommandIndex(object):
742
"""A index for bzr help that returns commands."""
745
self.prefix = 'commands/'
747
def get_topics(self, topic):
748
"""Search for topic amongst commands.
750
:param topic: A topic to search for.
751
:return: A list which is either empty or contains a single
754
if topic and topic.startswith(self.prefix):
755
topic = topic[len(self.prefix):]
757
cmd = _get_cmd_object(topic)
764
663
if __name__ == '__main__':
765
664
sys.exit(main(sys.argv))