comparison cmd2.py @ 35:e2a5cdba3113

moved from precmd and postcmd into onecmd
author devlinjs@FA7CZA6N1254998.wrightpatterson.afmc.ds.af.mil
date Fri, 21 Dec 2007 16:17:06 -0500
parents d3b2f9c6e536
children 6315f75c45ae
comparison
equal deleted inserted replaced
34:d3b2f9c6e536 35:e2a5cdba3113
5 Case-insensitive commands 5 Case-insensitive commands
6 Special-character shortcut commands 6 Special-character shortcut commands
7 Load commands from file 7 Load commands from file
8 Settable environment parameters 8 Settable environment parameters
9 9
10 still to do: 10 todo:
11 edit spits eof 11 edit spits eof
12 run 12 flags
13 > 13 >
14 """ 14 """
15 import cmd, re, os, sys 15 import cmd, re, os, sys
16 16
17 class Cmd(cmd.Cmd): 17 class Cmd(cmd.Cmd):
41 41
42 def __init__(self, *args, **kwargs): 42 def __init__(self, *args, **kwargs):
43 cmd.Cmd.__init__(self, *args, **kwargs) 43 cmd.Cmd.__init__(self, *args, **kwargs)
44 self.history = History() 44 self.history = History()
45 45
46 def precmd(self, line): 46 def onecmd(self, line):
47 """Hook method executed just before the command line is 47 """Interpret the argument as though it had been typed in response
48 interpreted, but after the input prompt is generated and issued. 48 to the prompt.
49 49
50 Makes commands case-insensitive (but unfortunately does not alter command completion). 50 This may be overridden, but should not normally need to be;
51 """ 51 see the precmd() and postcmd() methods for useful execution hooks.
52 The return value is a flag indicating whether interpretation of
53 commands by the interpreter should stop.
54
55 """
52 try: 56 try:
53 (command, args) = line.split(None,1) 57 (command, args) = line.split(None,1)
54 except ValueError: 58 except ValueError:
55 (command, args) = line, '' 59 (command, args) = line, ''
56 if self.caseInsensitive: 60 if self.caseInsensitive:
57 command = command.lower() 61 command = command.lower()
58 statement = ' '.join([command, args]) 62 statement = ' '.join([command, args])
59 if (not self.multilineCommands) or (command not in self.multilineCommands): 63 if command in self.multilineCommands:
60 return statement 64 statement = self.finishStatement(statement)
61 return self.finishStatement(statement) 65 stop = cmd.Cmd.onecmd(self, statement)
62 66 try:
63 def postcmd(self, stop, line): 67 command = statement.split(None,1)[0].lower()
64 """Hook method executed just after a command dispatch is finished."""
65 try:
66 command = line.split(None,1)[0].lower()
67 if command not in self.excludeFromHistory: 68 if command not in self.excludeFromHistory:
68 self.history.append(line) 69 self.history.append(statement)
69 finally: 70 finally:
70 return stop 71 return stop
71 72
72 def finishStatement(self, firstline): 73 def finishStatement(self, firstline):
73 statement = firstline 74 statement = firstline
74 while not self.statementHasEnded(statement): 75 while not self.statementHasEnded(statement):
75 inp = self.pseudo_raw_input(self.continuationPrompt) 76 inp = self.pseudo_raw_input(self.continuationPrompt)