Transitioned help to Command-format
authorW. Trevor King <wking@drexel.edu>
Tue, 15 Dec 2009 01:33:35 +0000 (20:33 -0500)
committerW. Trevor King <wking@drexel.edu>
Tue, 15 Dec 2009 01:33:35 +0000 (20:33 -0500)
libbe/command/__init__.py
libbe/command/base.py
libbe/command/help.py
libbe/ui/command_line.py

index 855888203989783fecdc3a11e49a53184bbc2d5b..916b5ce90b4832b58b9a01f579d281ddd5ee815a 100644 (file)
@@ -20,10 +20,11 @@ import base
 UserError = base.UserError
 UnknownCommand = base.UnknownCommand
 get_command = base.get_command
+get_command_class = base.get_command_class
 commands = base.commands
 Option = base.Option
 Argument = base.Argument
 Command = base.Command
 
-__all__ = [UserError, UnknownCommand, get_command, commands,
-           Option, Argument, Command]
+__all__ = [UserError, UnknownCommand, get_command, get_command_class,
+           commands, Option, Argument, Command]
index fe29908af01c6cf01c076243588887f2935a5844..54463c8fee0e261edcf7190a690faeb620570eef 100644 (file)
@@ -180,6 +180,7 @@ class Command (object):
     def __init__(self, input_encoding=None, output_encoding=None):
         self.status = None
         self.result = None
+        self.ui = None # calling user-interface, e.g. for Help()
         self.requires_bugdir = False
         self.requires_storage = False
         self.requires_unconnected_storage = False
index 9e6d1aafb6edd85bae0452268b769d09c8f83a63..c8d700dc685519d268e959b7a0dda4413429ba4b 100644 (file)
 # You should have received a copy of the GNU General Public License along
 # with this program; if not, write to the Free Software Foundation, Inc.,
 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-"""Print help for given subcommand"""
-from libbe import cmdutil, utility
-__desc__ = __doc__
 
-def execute(args, manipulate_encodings=True, restrict_file_access=False,
-            dir="."):
-    """
-    Print help of specified command (the manipulate_encodings argument
-    is ignored).
+import libbe
+import libbe.command
+import libbe.command.util
+
+TOPICS = {}
+
+class Help (libbe.command.Command):
+    """Print help for given command or topic
+
+    >>> import sys
+    >>> import libbe.bugdir
+    >>> cmd = Help()
+    >>> cmd._setup_io = lambda i_enc,o_enc : None
+    >>> cmd.stdout = sys.stdout
 
-    >>> execute(["help"])
-    Usage: be help [COMMAND]
+    >>> ret = cmd.run(args=['help'])
+    usage: be help [options] [TOPIC]
     <BLANKLINE>
     Options:
-      -h, --help  Print a help message
-      --complete  Print a list of available completions
+      -h, --help  Print a help message.
     <BLANKLINE>
-    Print help for specified command or list of all commands.
+      --complete  Print a list of possible completions.
     <BLANKLINE>
+    <BLANKLINE>
+    Print help for specified command/topic or list of all commands.
     """
-    parser = get_parser()
-    options, args = parser.parse_args(args)
-    complete(options, args, parser)
-    if len(args) > 1:
-        raise cmdutil.UsageError("Too many arguments.")
-    if len(args) == 0:
-        print cmdutil.help()
-    else:
-        try:
-            print cmdutil.help(args[0])
-        except AttributeError:
-            print "No help available"    
+    name = 'help'
 
-def get_parser():
-    parser = cmdutil.CmdOptionParser("be help [COMMAND]")
-    return parser
+    def __init__(self, *args, **kwargs):
+        libbe.command.Command.__init__(self, *args, **kwargs)
+        self.args.extend([
+                libbe.command.Argument(
+                    name='topic', metavar='TOPIC', default=None,
+                    optional=True,
+                    completion_callback=self.complete_topic)
+                ])
 
-longhelp="""
-Print help for specified command or list of all commands.
-"""
+    def _run(self, storage, bugdir, **params):
+        if params['topic'] == None:
+            if hasattr(self.ui, 'help'):
+                self.ui.help()
+        elif params['topic'] in libbe.command.commands():
+            module = libbe.command.get_command(params['topic'])
+            Class = libbe.command.get_command_class(module,params['topic'])
+            c = Class()
+            print >> self.stdout, c.help().rstrip('\n')
+        elif params['topic'] in TOPICS:
+            print >> self.stdout, TOPICS[params['topic']].rstrip('\n')
+        else:
+            raise libbe.command.UserError(
+                '"%s" is neither a command nor topic' % params['topic'])
+        return 0
 
-def help():
-    return get_parser().help_str() + longhelp
+    def _long_help(self):
+        return """
+Print help for specified command/topic or list of all commands.
+"""
 
-def complete(options, args, parser):
-    for option, value in cmdutil.option_value_pairs(options, parser):
-        if value == "--complete":
-            # no argument-options at the moment, so this is future-proofing
-            raise cmdutil.GetCompletions()
-    if "--complete" in args:
-        cmds = [command for command,module in cmdutil.iter_commands()]
-        raise cmdutil.GetCompletions(cmds)
+    def complete_topic(self, command, argument, fragment=None):
+        commands = libbe.command.util.complete_command()
+        topics = sorted(TOPICS.keys())
+        return commands + topics
index feeccd46ef39e4fab904182b747ed2f9106642c1..84f9450236085079659f701ecb8001357d8055f8 100755 (executable)
@@ -265,6 +265,7 @@ def main():
         return 1
     Class = getattr(module, command_name.capitalize())
     command = Class()
+    command.ui = self
     parser = CmdOptionParser(command)
     storage = None
     bugdir = None