Rev 2444: Relocate command help onto Command. in file:///home/robertc/source/baz/help-contexts/

Robert Collins robertc at robertcollins.net
Fri Apr 20 02:57:13 BST 2007


At file:///home/robertc/source/baz/help-contexts/

------------------------------------------------------------
revno: 2444
revision-id: robertc at robertcollins.net-20070420015710-apsxzfznrnpg6x17
parent: robertc at robertcollins.net-20070420011512-wralvwk6y8esm83c
committer: Robert Collins <robertc at robertcollins.net>
branch nick: help-contexts
timestamp: Fri 2007-04-20 11:57:10 +1000
message:
  Relocate command help onto Command.
modified:
  bzrlib/commands.py             bzr.py-20050309040720-d10f4714595cf8c3
  bzrlib/help.py                 help.py-20050505025907-4dd7a6d63912f894
  bzrlib/tests/test_help.py      test_help.py-20070419045354-6q6rq15j9e2n5fna-1
  bzrlib/tests/test_plugins.py   plugins.py-20050622075746-32002b55e5e943e9
=== modified file 'bzrlib/commands.py'
--- a/bzrlib/commands.py	2007-04-19 23:57:17 +0000
+++ b/bzrlib/commands.py	2007-04-20 01:57:10 +0000
@@ -233,6 +233,58 @@
         if self.__doc__ == Command.__doc__:
             warn("No help message set for %r" % self)
 
+    def _usage(self):
+        """Return single-line grammar for this command.
+
+        Only describes arguments, not options.
+        """
+        s = 'bzr ' + self.name() + ' '
+        for aname in self.takes_args:
+            aname = aname.upper()
+            if aname[-1] in ['$', '+']:
+                aname = aname[:-1] + '...'
+            elif aname[-1] == '?':
+                aname = '[' + aname[:-1] + ']'
+            elif aname[-1] == '*':
+                aname = '[' + aname[:-1] + '...]'
+            s += aname + ' '
+                
+        assert s[-1] == ' '
+        s = s[:-1]
+        return s
+
+    def get_help_text(self):
+        """Return a text string with help for this command."""
+        doc = self.help()
+        if doc is None:
+            raise NotImplementedError("sorry, no detailed help yet for %r" % self.name())
+
+        result = ""
+        result += 'usage:%s\n' % self._usage()
+
+        if self.aliases:
+            result += 'aliases:\n'
+            result += ', '.join(self.aliases) + '\n'
+
+        result += '\n'
+
+        plugin_name = self.plugin_name()
+        if plugin_name is not None:
+            result += '(From plugin "%s")' % plugin_name
+            result += '\n\n'
+
+        result += doc
+        if result[-1] != '\n':
+            result += '\n'
+        result += '\n'
+        result += option.get_optparser(self.options()).format_option_help()
+        see_also = self.get_see_also()
+        if see_also:
+            result += '\nSee also: '
+            result += ', '.join(see_also)
+            result += '\n'
+        return result
+
     def get_see_also(self):
         """Return a list of help topics that are related to this ommand.
         
@@ -288,8 +340,7 @@
             argv = []
         args, opts = parse_args(self, argv, alias_argv)
         if 'help' in opts:  # e.g. bzr add --help
-            from bzrlib.help import help_on_command
-            help_on_command(self.name())
+            sys.stdout.write(self.get_help_text())
             return 0
         # mix arguments and options into one dictionary
         cmdargs = _match_argform(self.name(), self.takes_args, args)

=== modified file 'bzrlib/help.py'
--- a/bzrlib/help.py	2007-04-20 01:15:12 +0000
+++ b/bzrlib/help.py	2007-04-20 01:57:10 +0000
@@ -42,92 +42,9 @@
     if topics:
         outfile.write(topics[0].get_help_text())
     else:
-        help_on_command(topic, outfile=outfile)
-
-
-def command_usage(cmd_object):
-    """Return single-line grammar for command.
-
-    Only describes arguments, not options.
-    """
-    s = 'bzr ' + cmd_object.name() + ' '
-    for aname in cmd_object.takes_args:
-        aname = aname.upper()
-        if aname[-1] in ['$', '+']:
-            aname = aname[:-1] + '...'
-        elif aname[-1] == '?':
-            aname = '[' + aname[:-1] + ']'
-        elif aname[-1] == '*':
-            aname = '[' + aname[:-1] + '...]'
-        s += aname + ' '
-            
-    assert s[-1] == ' '
-    s = s[:-1]
-    
-    return s
-
-
-def print_command_plugin(cmd_object, outfile, format):
-    """Print the plugin that provides a command object, if any.
-
-    If the cmd_object is provided by a plugin, prints the plugin name to
-    outfile using the provided format string.
-    """
-    plugin_name = cmd_object.plugin_name()
-    if plugin_name is not None:
-        out_str = '(From plugin "%s")' % plugin_name
-        outfile.write(format % out_str)
-
-
-def help_on_command(cmdname, outfile=None):
-    cmdname = str(cmdname)
-    cmd_object = _mod_commands.get_cmd_object(cmdname)
-
-    return help_on_command_object(cmd_object, cmdname, outfile)
-
-
-def help_on_command_object(cmd_object, cmdname, outfile=None):
-    """Generate help on the cmd_object with a supplied name of cmdname.
-
-    :param cmd_object: An instance of a Command.
-    :param cmdname: The user supplied name. This might be an alias for example.
-    :param outfile: A stream to write the help to.
-    """
-    if outfile is None:
-        outfile = sys.stdout
-
-    doc = cmd_object.help()
-    if doc is None:
-        raise NotImplementedError("sorry, no detailed help yet for %r" % cmdname)
-
-    print >>outfile, 'usage:', command_usage(cmd_object)
-
-    if cmd_object.aliases:
-        print >>outfile, 'aliases:',
-        print >>outfile, ', '.join(cmd_object.aliases)
-
-    print >>outfile
-
-    print_command_plugin(cmd_object, outfile, '%s\n\n')
-
-    outfile.write(doc)
-    if doc[-1] != '\n':
-        outfile.write('\n')
-    help_on_command_options(cmd_object, outfile)
-    see_also = cmd_object.get_see_also()
-    if see_also:
-        outfile.write('\nSee also: ')
-        outfile.write(', '.join(see_also))
-        outfile.write('\n')
-
-
-def help_on_command_options(cmd, outfile=None):
-    from bzrlib.option import Option, get_optparser
-    if outfile is None:
-        outfile = sys.stdout
-    options = cmd.options()
-    outfile.write('\n')
-    outfile.write(get_optparser(options).format_option_help())
+        cmdname = str(topic)
+        cmd_object = _mod_commands.get_cmd_object(cmdname)
+        outfile.write(cmd_object.get_help_text())
 
 
 def help_commands(outfile=None):

=== modified file 'bzrlib/tests/test_help.py'
--- a/bzrlib/tests/test_help.py	2007-04-20 01:07:51 +0000
+++ b/bzrlib/tests/test_help.py	2007-04-20 01:57:10 +0000
@@ -35,14 +35,22 @@
             """A sample command."""
             _see_also = ['foo', 'bar']
         cmd = cmd_WithSeeAlso()
-        helpfile = StringIO()
-        help.help_on_command_object(cmd, 'cmd_sample', helpfile)
+        helptext = cmd.get_help_text()
         self.assertEndsWith(
-            helpfile.getvalue(),
+            helptext,
             '  -h, --help  show help message\n'
             '\n'
             'See also: bar, foo\n')
 
+    def test_get_help_text(self):
+        """Commands have a get_help_text method which returns their help."""
+        class cmd_Demo(commands.Command):
+            """A sample command."""
+        cmd = cmd_Demo()
+        helptext = cmd.get_help_text()
+        self.assertStartsWith(helptext, 'usage:bzr Demo')
+        self.assertEndsWith(helptext, 'show help message\n')
+    
 
 class TestRegisteredTopic(tests.TestCase):
     """Tests for the RegisteredTopic class."""

=== modified file 'bzrlib/tests/test_plugins.py'
--- a/bzrlib/tests/test_plugins.py	2007-02-02 16:17:55 +0000
+++ b/bzrlib/tests/test_plugins.py	2007-04-20 01:57:10 +0000
@@ -160,17 +160,15 @@
         for cmd_name in bzrlib.commands.builtin_command_names():
             if cmd_name in bzrlib.commands.plugin_command_names():
                 continue
-            help = StringIO()
             try:
-                bzrlib.help.help_on_command(cmd_name, help)
+                help = bzrlib.commands.get_cmd_object(cmd_name).get_help_text()
             except NotImplementedError:
                 # some commands have no help
                 pass
             else:
-                help.seek(0)
-                self.assertNotContainsRe(help.read(), 'From plugin "[^"]*"')
+                self.assertNotContainsRe(help, 'From plugin "[^"]*"')
 
-            if help in help_commands.keys():
+            if cmd_name in help_commands.keys():
                 # some commands are hidden
                 help = help_commands[cmd_name]
                 self.assertNotContainsRe(help, 'From plugin "[^"]*"')



More information about the bazaar-commits mailing list