~ubuntu-branches/ubuntu/gutsy/bzr/gutsy

« back to all changes in this revision

Viewing changes to bzrlib/commands.py

  • Committer: Bazaar Package Importer
  • Author(s): Etienne Goyer
  • Date: 2007-04-27 17:53:49 UTC
  • mfrom: (1.1.23 upstream)
  • Revision ID: james.westby@ubuntu.com-20070427175349-rvowqx994rfuikuu
Tags: 0.16~rc1-0ubuntu1
New upstream development release 

Show diffs side-by-side

added added

removed removed

Lines of Context:
137
137
    plugins_override
138
138
        If true, plugin commands can override builtins.
139
139
    """
 
140
    try:
 
141
        return _get_cmd_object(cmd_name, plugins_override)
 
142
    except KeyError:
 
143
        raise errors.BzrCommandError('unknown command "%s"' % cmd_name)
 
144
 
 
145
 
 
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
141
149
 
142
150
    # We want only 'ascii' command names, but the user may have typed
159
167
    cmd_obj = ExternalCommand.find_command(cmd_name)
160
168
    if cmd_obj:
161
169
        return cmd_obj
162
 
 
163
 
    raise errors.BzrCommandError('unknown command "%s"' % cmd_name)
 
170
    raise KeyError
164
171
 
165
172
 
166
173
class Command(object):
233
240
        if self.__doc__ == Command.__doc__:
234
241
            warn("No help message set for %r" % self)
235
242
 
 
243
    def _usage(self):
 
244
        """Return single-line grammar for this command.
 
245
 
 
246
        Only describes arguments, not options.
 
247
        """
 
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] + '...]'
 
257
            s += aname + ' '
 
258
                
 
259
        assert s[-1] == ' '
 
260
        s = s[:-1]
 
261
        return s
 
262
 
 
263
    def get_help_text(self, additional_see_also=None):
 
264
        """Return a text string with help for this command.
 
265
        
 
266
        :param additional_see_also: Additional help topics to be
 
267
            cross-referenced.
 
268
        """
 
269
        doc = self.help()
 
270
        if doc is None:
 
271
            raise NotImplementedError("sorry, no detailed help yet for %r" % self.name())
 
272
 
 
273
        result = ""
 
274
        result += 'usage: %s\n' % self._usage()
 
275
 
 
276
        if self.aliases:
 
277
            result += 'aliases:\n'
 
278
            result += ', '.join(self.aliases) + '\n'
 
279
 
 
280
        result += '\n'
 
281
 
 
282
        plugin_name = self.plugin_name()
 
283
        if plugin_name is not None:
 
284
            result += '(From plugin "%s")' % plugin_name
 
285
            result += '\n\n'
 
286
 
 
287
        result += doc
 
288
        if result[-1] != '\n':
 
289
            result += '\n'
 
290
        result += '\n'
 
291
        result += option.get_optparser(self.options()).format_option_help()
 
292
        see_also = self.get_see_also(additional_see_also)
 
293
        if see_also:
 
294
            result += '\nSee also: '
 
295
            result += ', '.join(see_also)
 
296
            result += '\n'
 
297
        return result
 
298
 
 
299
    def get_help_topic(self):
 
300
        """Return the commands help topic - its name."""
 
301
        return self.name()
 
302
 
 
303
    def get_see_also(self, additional_terms=None):
 
304
        """Return a list of help topics that are related to this ommand.
 
305
        
 
306
        The list is derived from the content of the _see_also attribute. Any
 
307
        duplicates are removed and the result is in lexical order.
 
308
        :param additional_terms: Additional help topics to cross-reference.
 
309
        :return: A list of help topics.
 
310
        """
 
311
        see_also = set(getattr(self, '_see_also', []))
 
312
        if additional_terms:
 
313
            see_also.update(additional_terms)
 
314
        return sorted(see_also)
 
315
 
236
316
    def options(self):
237
317
        """Return dict of valid options for this command.
238
318
 
271
351
        # bogus. So set the attribute, so we can find the correct encoding later.
272
352
        self.outf.encoding = output_encoding
273
353
 
274
 
    @deprecated_method(zero_eight)
275
 
    def run_argv(self, argv):
276
 
        """Parse command line and run.
277
 
        
278
 
        See run_argv_aliases for the 0.8 and beyond api.
279
 
        """
280
 
        return self.run_argv_aliases(argv)
281
 
 
282
354
    def run_argv_aliases(self, argv, alias_argv=None):
283
355
        """Parse the command line and run with extra aliases in alias_argv."""
284
356
        if argv is None:
287
359
            argv = []
288
360
        args, opts = parse_args(self, argv, alias_argv)
289
361
        if 'help' in opts:  # e.g. bzr add --help
290
 
            from bzrlib.help import help_on_command
291
 
            help_on_command(self.name())
 
362
            sys.stdout.write(self.get_help_text())
292
363
            return 0
293
364
        # mix arguments and options into one dictionary
294
365
        cmdargs = _match_argform(self.name(), self.takes_args, args)
596
667
    # 'command not found' error later.
597
668
 
598
669
    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
601
 
        run_argv = [argv]
602
 
    else:
603
 
        run = cmd_obj.run_argv_aliases
604
 
        run_argv = [argv, alias_argv]
 
670
    run = cmd_obj.run_argv_aliases
 
671
    run_argv = [argv, alias_argv]
605
672
 
606
673
    try:
607
674
        if opt_lsprof:
647
714
 
648
715
def run_bzr_catch_errors(argv):
649
716
    try:
650
 
        return run_bzr(argv)
651
 
        # do this here inside the exception wrappers to catch EPIPE
652
 
        sys.stdout.flush()
 
717
        try:
 
718
            return run_bzr(argv)
 
719
        finally:
 
720
            # do this here inside the exception wrappers to catch EPIPE
 
721
            sys.stdout.flush()
653
722
    except (KeyboardInterrupt, Exception), e:
654
723
        # used to handle AssertionError and KeyboardInterrupt
655
724
        # specially here, but hopefully they're handled ok by the logger now
660
729
            pdb.post_mortem(sys.exc_traceback)
661
730
        return 3
662
731
 
 
732
 
 
733
class HelpCommandIndex(object):
 
734
    """A index for bzr help that returns commands."""
 
735
 
 
736
    def __init__(self):
 
737
        self.prefix = 'commands/'
 
738
 
 
739
    def get_topics(self, topic):
 
740
        """Search for topic amongst commands.
 
741
 
 
742
        :param topic: A topic to search for.
 
743
        :return: A list which is either empty or contains a single
 
744
            Command entry.
 
745
        """
 
746
        if topic and topic.startswith(self.prefix):
 
747
            topic = topic[len(self.prefix):]
 
748
        try:
 
749
            cmd = _get_cmd_object(topic)
 
750
        except KeyError:
 
751
            return []
 
752
        else:
 
753
            return [cmd]
 
754
 
 
755
 
663
756
if __name__ == '__main__':
664
757
    sys.exit(main(sys.argv))