Add better comment parsing to hooke.ui.commandline.
authorW. Trevor King <wking@drexel.edu>
Thu, 3 Jun 2010 13:59:51 +0000 (09:59 -0400)
committerW. Trevor King <wking@drexel.edu>
Thu, 3 Jun 2010 13:59:51 +0000 (09:59 -0400)
The old implemtation only handled comments after valid function calls:
  hooke> load_playlist test # a comment
but failed on pure comments and in help
  hooke> # failing comment
  hooke> help # failing comment
The new implemation handles all of these.

I changed empty lines to no-ops (rather than repeating the last
commant), because explicit is better than implicit.

doc/tutorial.txt
hooke/ui/commandline.py

index 4583d4c9b7e77ae5be8dd25f60b4dfd39e31ebcf..7b302f5379a6888c5a2cbdf8b0b080f92d3dcd3a 100644 (file)
@@ -150,13 +150,12 @@ Navigating the playlist
 
 Now you can navigate through your playlist using the commands
 ``next_curve`` and ``previous_curve``. You don’t need to type
 
 Now you can navigate through your playlist using the commands
 ``next_curve`` and ``previous_curve``. You don’t need to type
-``next_curve`` every time to run along a list of curves.  If you press
-Return to an empty prompt, Hooke will repeat the last command you
-issued explicitly.  You can also navigate through the command history
-by using the up and down arrows, or auto-complete partial commands
-with TAB.  From the last curve of your playlist, ``next_curve`` will
-wrap around to the first curve.  Analogously, issuing
-``previous_curve`` at the first curve will jump to the last.
+``next_curve`` every time to run along a list of curves.  You can
+navigate through the command history by using the up and down arrows,
+or auto-complete partial commands with TAB.  From the last curve of
+your playlist, ``next_curve`` will wrap around to the first curve.
+Analogously, issuing ``previous_curve`` at the first curve will jump
+to the last.
 
 You can also jump to a given curve::
 
 
 You can also jump to a given curve::
 
index 2df4b14c80c6720e5fe6995cdcbc6d4a070ea7ad..c5f3353b000d71a9e8dd8652dda0b6e09492efc1 100644 (file)
@@ -120,8 +120,7 @@ class DoCommand (CommandMethod):
             self.cmd.stdout.write(str(msg).rstrip()+'\n')
 
     def _parse_args(self, args):
             self.cmd.stdout.write(str(msg).rstrip()+'\n')
 
     def _parse_args(self, args):
-        argv = shlex.split(args, comments=True, posix=True)
-        options,args = self.parser.parse_args(argv)
+        options,args = self.parser.parse_args(args)
         self._check_argument_length_bounds(args)
         params = {}
         for argument in self.parser.command_opts:
         self._check_argument_length_bounds(args)
         params = {}
         for argument in self.parser.command_opts:
@@ -359,6 +358,42 @@ class HookeCmd (cmd.Cmd):
                     setattr(self.__class__, 'complete_%s' % name,
                             CompleteCommand(self, command, self._name_fn))
 
                     setattr(self.__class__, 'complete_%s' % name,
                             CompleteCommand(self, command, self._name_fn))
 
+    def parseline(self, line):
+        """Override Cmd.parseline to use shlex.split.
+
+        Notes
+        -----
+        This allows us to handle comments cleanly.  With the default
+        Cmd implementation, a pure comment line will call the .default
+        error message.
+
+        Since we use shlex to strip comments, we return a list of
+        split arguments rather than the raw argument string.
+        """
+        line = line.strip()
+        argv = shlex.split(line, comments=True, posix=True)
+        if len(argv) == 0:
+            return None, None, '' # return an empty line
+        elif argv[0] == '?':
+            argv[0] = 'help'
+        elif argv[0] == '!':
+            argv[0] = 'system'
+        return argv[0], argv[1:], line
+
+    def do_help(self, arg):
+        """Wrap Cmd.do_help to handle our .parseline argument list.
+        """
+        if len(arg) == 0:
+            return cmd.Cmd.do_help(self, '')
+        return cmd.Cmd.do_help(self, arg[0])
+
+    def empytline(self):
+        """Override Cmd.emptyline to not do anything.
+
+        Repeating the last non-empty command seems unwise.  Explicit
+        is better than implicit.
+        """
+        pass
 
 class CommandLine (UserInterface):
     """Command line interface.  Simple and powerful.
 
 class CommandLine (UserInterface):
     """Command line interface.  Simple and powerful.