Added command parser to hooke.ui.commandline for argument listing.
authorW. Trevor King <wking@drexel.edu>
Mon, 10 May 2010 13:43:55 +0000 (09:43 -0400)
committerW. Trevor King <wking@drexel.edu>
Mon, 10 May 2010 13:43:55 +0000 (09:43 -0400)
hooke/__init__.py
hooke/command.py
hooke/plugin/cut.py
hooke/plugin/playlist.py
hooke/ui/commandline.py

index b90ff3d8f016e52a1717013bc565ccfcc93731db..283d0dd2955b6ee28a46a9d04402a32bbe532752 100644 (file)
@@ -40,7 +40,7 @@ def version(depth=-1, version_tuple=None):
 
     Since I seem to be unable to override __version__ in a Doctest,
     we'll pass the version tuple in as an argument.  You can ignore
-    ``version_tuple``.
+    `version_tuple`.
 
     >>> v = (1, 2, 3, 'devel', '20100501', 'Kenzo')
 
index bfa4c9f1611fa414f3ab1c1c673eaa362af65c6b..97d8ea5d6241e0f69462651bba7a16a8f7ba62d5 100644 (file)
@@ -3,6 +3,7 @@
 """
 
 import Queue as queue
+import textwrap
 
 
 class CommandExit (Exception):
@@ -24,6 +25,7 @@ class Command (object):
     Command: test
     <BLANKLINE>
     Arguments:
+    <BLANKLINE>
     help BOOL (bool) Print a help message.
     <BLANKLINE>
     An example Command.
@@ -101,7 +103,12 @@ class Command (object):
             name_part += ' (%s)' % ', '.join(self.aliases)
         parts = [name_part]
         if len(self.arguments) > 0:
-            argument_part = ['Arguments:'] + [a.help() for a in self.arguments]
+            argument_part = ['Arguments:', '']
+            for a in self.arguments:
+                argument_part.append(textwrap.fill(
+                        a.help(),
+                        initial_indent="",
+                        subsequent_indent="    "))
             argument_part = '\n'.join(argument_part)
             parts.append(argument_part)
         parts.append(self._help) # help part
index c1d8ca9c05bc45dcfaa01e923b439d38fdd9d608..15d6bbed2dd352026ff5fd2e286759ef09290990 100644 (file)
@@ -23,7 +23,7 @@ class CutCommand (Command):
             name='cut',
             arguments=[
                 Argument(name='curve', type='curve', optional=False, help="""
-:class:``hooke.curve.Curve`` to cut from.
+:class:`hooke.curve.Curve` to cut from.
 """.strip()),
                 Argument(name='block', aliases=['set'], type='int', default=0,
                     help="""
index 78ad4eb76148951756ad72745c35df98ddba0967..44f18bdd7ebab974e415f83b7456adf2fcd54078 100644 (file)
@@ -29,7 +29,7 @@ class NextCommand (Command):
             arguments=[
                 Argument(name='playlist', type='playlist', optional=False,
                          help="""
-:class:``hooke.plugin.playlist.Playlist`` to act on.
+:class:`hooke.plugin.playlist.Playlist` to act on.
 """.strip()),
                 ],
             help=self.__doc__)
@@ -46,7 +46,7 @@ class PreviousCommand (Command):
             arguments=[
                 Argument(name='playlist', type='playlist', optional=False,
                          help="""
-:class:``hooke.plugin.playlist.Playlist`` to act on.
+:class:`hooke.plugin.playlist.Playlist` to act on.
 """.strip()),
                 ],
             help=self.__doc__)
@@ -63,7 +63,7 @@ class JumpCommand (Command):
             arguments=[
                 Argument(name='playlist', type='playlist', optional=False,
                          help="""
-:class:``hooke.plugin.playlist.Playlist`` to act on.
+:class:`hooke.plugin.playlist.Playlist` to act on.
 """.strip()),
                 Argument(name='index', type='int', optional=False, help="""
 Index of target curve.
@@ -83,7 +83,7 @@ class SaveCommand (Command):
             arguments=[
                 Argument(name='playlist', type='playlist', optional=False,
                          help="""
-:class:``hooke.plugin.playlist.Playlist`` to act on.
+:class:`hooke.plugin.playlist.Playlist` to act on.
 """.strip()),
                 Argument(name='output', type='file',
                          help="""
@@ -129,7 +129,7 @@ class AddCommand (Command):
             arguments=[
                 Argument(name='playlist', type='playlist', optional=False,
                          help="""
-:class:``hooke.plugin.playlist.Playlist`` to act on.
+:class:`hooke.plugin.playlist.Playlist` to act on.
 """.strip()),
                 Argument(name='input', type='file', optional=False,
                          help="""
@@ -159,7 +159,7 @@ class AddGlobCommand (Command):
             arguments=[
                 Argument(name='playlist', type='playlist', optional=False,
                          help="""
-:class:``hooke.plugin.playlist.Playlist`` to act on.
+:class:`hooke.plugin.playlist.Playlist` to act on.
 """.strip()),
                 Argument(name='input', type='glob', optional=False,
                          help="""
@@ -185,7 +185,7 @@ class RemoveCommand (Command):
             arguments=[
                 Argument(name='playlist', type='playlist', optional=False,
                          help="""
-:class:``hooke.plugin.playlist.Playlist`` to act on.
+:class:`hooke.plugin.playlist.Playlist` to act on.
 """.strip()),
                 Argument(name='index', type='int', optional=False, help="""
 Index of target curve.
@@ -210,7 +210,7 @@ class FilterCommand (Command):
             arguments=[
                 Argument(name='playlist', type='playlist', optional=False,
                          help="""
-:class:``hooke.plugin.playlist.Playlist`` to act on.
+:class:`hooke.plugin.playlist.Playlist` to act on.
 """.strip()),
                 Argument(name='filter', type='function', optional=False,
                          help="""
index 02174c40374a870701c76c2c547baea3df3092bb..cab42969d79f63450697808741291f966390ad13 100644 (file)
@@ -3,11 +3,13 @@ line.
 """
 
 import cmd
-import readline
+import optparse
+import readline # including readline makes cmd.Cmd.cmdloop() smarter
 
 from ..command import CommandExit, Command, Argument
 from ..ui import UserInterface, CommandMessage
 
+
 # Define a few helper classes.  The .__call__ methods of these
 # functions will provide the do_*, help_*, and complete_* methods of
 # HookeCmd.
@@ -20,7 +22,25 @@ class CommandMethod (object):
     def __call__(self, *args, **kwargs):
         raise NotImplementedError
 
+def command_parser(command):
+    p = optparse.OptionParser()
+    args = []
+    for a in command.arguments:
+        if a.name == 'help':
+            continue # 'help' is a default OptionParser option
+        if a.optional == True:
+            p.add_option(
+                '--%s' % a.name.replace('_', '-'),
+                dest=a.name, default=a.default)
+        else:
+            args.append((a.name.replace('_', '-'), a))
+    return (p, args)
+
 class DoCommand (CommandMethod):
+    def __init__(self, *args, **kwargs):
+        super(DoCommand, self).__init__(*args, **kwargs)
+        self.parser,self.args = command_parser(self.command)
+
     def __call__(self, args):
         args = self._parse_args(self.command, args)
         self.cmd.inqueue.put(CommandMessage(self.command, args))
@@ -35,6 +55,10 @@ class DoCommand (CommandMethod):
         return {}
 
 class HelpCommand (CommandMethod):
+    def __init__(self, *args, **kwargs):
+        super(HelpCommand, self).__init__(*args, **kwargs)
+        self.parser,self.args = command_parser(self.command)
+
     def __call__(self):
         blocks = [self.command.help(),
                   '------',
@@ -46,7 +70,15 @@ class HelpCommand (CommandMethod):
         return self.command.help()
 
     def _usage_string(self):
-        return ' '.join([self.command.name, '[args]'])
+        if len(self.args) == len(self.command.arguments):
+            options_string = ''
+        else:
+            options_string = '[options]'
+        arg_string = ' '.join([name for name,arg in self.args])
+        return ' '.join([x for x in [self.command.name,
+                                     options_string,
+                                     arg_string]
+                         if x != ''])
 
 class CompleteCommand (CommandMethod):
     def __call__(self, text, line, begidx, endidx):