changeset 17:25e908abf199

multiline into cmd2
author devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
date Wed, 19 Dec 2007 11:11:24 -0500
parents 2776755a3a7e
children d0f0b3e0b89b
files cmd2.py
diffstat 1 files changed, 40 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/cmd2.py	Wed Dec 19 10:28:01 2007 -0500
+++ b/cmd2.py	Wed Dec 19 11:11:24 2007 -0500
@@ -42,10 +42,30 @@
             return [itm for itm in self if isin(itm)]
 
 class Cmd(cmd.Cmd):
-    def __init__(self, *args, **kwargs):
+    excludeFromHistory = '''run r list l history hi ed li'''.split()
+    caseInsensitive = True
+    multilineCommands = []
+    continuationPrompt = '> '    
+    def __init__(self, *args, **kwargs):	
         cmd.Cmd.__init__(self, *args, **kwargs)
         self.history = History()
-        self.excludeFromHistory = '''run r list l history hi ed li'''.split()
+	
+    def precmd(self, line):
+        """Hook method executed just before the command line is
+        interpreted, but after the input prompt is generated and issued.
+        
+        Makes commands case-insensitive (but unfortunately does not alter command completion).
+        """
+        try:
+            (command, args) = line.split(None,1)
+	except ValueError:
+	    (command, args) = line, ''
+	if self.caseInsensitive:
+	    command = command.lower()
+	statement = ' '.join([command, args])
+	if (not self.multilineCommands) or (command not in self.multilineCommands):
+	    return statement
+	return self.finishStatement(statement)
 
     def postcmd(self, stop, line):
         """Hook method executed just after a command dispatch is finished."""
@@ -56,6 +76,22 @@
 	finally:
 	    return stop
 	
+    def finishStatement(self, firstline):
+	statement = firstline
+	while not self.statementHasEnded(statement):
+	    statement = '%s\n%s' % (statement, raw_input(self.continuationPrompt))
+	return statement
+	# assembling a list of lines and joining them at the end would be faster, 
+	# but statementHasEnded needs a string arg; anyway, we're getting
+	# user input and users are slow.
+	
+    def statementHasEnded(self, lines):
+	"""This version lets statements end with ; or with a blank line.
+	Override for your own needs."""
+	if not lines.splitlines()[-1].strip():
+	    return True
+        return (lines.strip()[-1] == ';') 
+	
     def do_history(self, arg):
         """history [arg]: lists past commands issued
         
@@ -94,4 +130,6 @@
     do_hi = do_history
     do_l = do_list
     do_li = do_list
+    
+