comparison cmd2.py @ 44:b66372883bb4

marked to v 0.3.3
author catherine@cordelia
date Mon, 26 May 2008 13:54:45 -0400
parents e03a8c03ac37
children 67cde3f501de
comparison
equal deleted inserted replaced
43:e03a8c03ac37 44:b66372883bb4
16 16
17 CHANGES: 17 CHANGES:
18 As of 0.3.0, options should be specified as `optparse` options. See README.txt. 18 As of 0.3.0, options should be specified as `optparse` options. See README.txt.
19 flagReader.py options are still supported for backward compatibility 19 flagReader.py options are still supported for backward compatibility
20 """ 20 """
21 import cmd, re, os, sys, optparse, subprocess, tempfile 21 import cmd, re, os, sys, optparse, subprocess, tempfile, pyparsing
22 from optparse import make_option 22 from optparse import make_option
23 23
24 class OptionParser(optparse.OptionParser): 24 class OptionParser(optparse.OptionParser):
25 def exit(self, status=0, msg=None): 25 def exit(self, status=0, msg=None):
26 self.values._exit = True 26 self.values._exit = True
173 def do_shortcuts(self, args): 173 def do_shortcuts(self, args):
174 """Lists single-key shortcuts available.""" 174 """Lists single-key shortcuts available."""
175 result = "\n".join('%s: %s' % (sc[0], sc[1]) for sc in self.shortcuts.items()) 175 result = "\n".join('%s: %s' % (sc[0], sc[1]) for sc in self.shortcuts.items())
176 self.stdout.write("Single-key shortcuts for other commands:\n%s\n" % (result)) 176 self.stdout.write("Single-key shortcuts for other commands:\n%s\n" % (result))
177 177
178 pipeFinder = pyparsing.SkipTo(pyparsing.Literal('|') ^ pyparsing.StringEnd())
179 pipeFinder.ignore(pyparsing.sglQuotedString)
180 pipeFinder.ignore(pyparsing.dblQuotedString)
181 pipeFinder.ignore("--" + pyparsing.ZeroOrMore(pyparsing.CharsNotIn("\n"))) # sql-style comment
182 pipeFinder.ignore(pyparsing.cStyleComment)
183
178 legalFileName = re.compile(r'''^[^"'\s]+$''') 184 legalFileName = re.compile(r'''^[^"'\s]+$''')
179 def parseRedirector(self, statement, symbol, mustBeTerminated=False): 185 def parseRedirector(self, statement, symbol, mustBeTerminated=False):
186 # pipeFinder.scanString(statement)
180 parts = statement.split(symbol) 187 parts = statement.split(symbol)
181 if (len(parts) < 2): 188 if (len(parts) < 2):
182 return statement, None 189 return statement, None
183 if mustBeTerminated and (not self.statementEndPattern.search(parts[-2])): 190 if mustBeTerminated and (not self.statementEndPattern.search(parts[-2])):
184 return statement, None 191 return statement, None
210 newStatement, redirect = self.parseRedirector(statement, '<', mustBeTerminated) 217 newStatement, redirect = self.parseRedirector(statement, '<', mustBeTerminated)
211 if redirect: 218 if redirect:
212 return newStatement, redirect, 'r' 219 return newStatement, redirect, 'r'
213 return statement, '', '' 220 return statement, '', ''
214 221
215 def onecmd(self, line): 222 def onecmd(self, line, assumeComplete=False):
216 """Interpret the argument as though it had been typed in response 223 """Interpret the argument as though it had been typed in response
217 to the prompt. 224 to the prompt.
218 225
219 This may be overridden, but should not normally need to be; 226 This may be overridden, but should not normally need to be;
220 see the precmd() and postcmd() methods for useful execution hooks. 227 see the precmd() and postcmd() methods for useful execution hooks.
222 commands by the interpreter should stop. 229 commands by the interpreter should stop.
223 230
224 """ 231 """
225 command, args = self.extractCommand(line) 232 command, args = self.extractCommand(line)
226 statement = ' '.join([command, args]) 233 statement = ' '.join([command, args])
227 if command in self.multilineCommands: 234 if (not assumeComplete) and (command in self.multilineCommands):
228 statement = self.finishStatement(statement) 235 statement = self.finishStatement(statement)
229 statekeeper = None 236 statekeeper = None
230 stop = 0 237 stop = 0
231 statement, redirect, mode = self.parseRedirectors(statement) 238 statement, redirect, mode = self.parseRedirectors(statement)
232 if redirect == self._TO_PASTE_BUFFER: 239 if redirect == self._TO_PASTE_BUFFER: