comparison cmd2.py @ 152:693d11072e8e

hmm, complications with gt within statements
author catherine@Elli.myhome.westell.com
date Fri, 21 Nov 2008 08:48:38 -0500
parents 2377461a35f3
children 5c5c458a6b70
comparison
equal deleted inserted replaced
151:2377461a35f3 152:693d11072e8e
22 mercurial repository at http://www.assembla.com/wiki/show/python-cmd2 22 mercurial repository at http://www.assembla.com/wiki/show/python-cmd2
23 CHANGES: 23 CHANGES:
24 As of 0.3.0, options should be specified as `optparse` options. See README.txt. 24 As of 0.3.0, options should be specified as `optparse` options. See README.txt.
25 flagReader.py options are still supported for backward compatibility 25 flagReader.py options are still supported for backward compatibility
26 """ 26 """
27 import cmd, re, os, sys, optparse, subprocess, tempfile, pyparsing, doctest, unittest 27 import cmd, re, os, sys, optparse, subprocess, tempfile, pyparsing, doctest, unittest, string
28 from optparse import make_option 28 from optparse import make_option
29 __version__ = '0.4.4' 29 __version__ = '0.4.4'
30 30
31 class OptionParser(optparse.OptionParser): 31 class OptionParser(optparse.OptionParser):
32 def exit(self, status=0, msg=None): 32 def exit(self, status=0, msg=None):
189 pass 189 pass
190 190
191 def __init__(self, *args, **kwargs): 191 def __init__(self, *args, **kwargs):
192 cmd.Cmd.__init__(self, *args, **kwargs) 192 cmd.Cmd.__init__(self, *args, **kwargs)
193 self.history = History() 193 self.history = History()
194 self.terminatorPattern = (pyparsing.oneOf(self.terminators) ^ (pyparsing.Literal('\nEOF') + pyparsing.lineEnd))('terminator') 194 self._init_parser()
195 for p in (self.terminatorPattern, self.pipePattern, self.redirectInPattern, self.redirectOutPattern, self.punctuationPattern):
196 p.ignore(pyparsing.sglQuotedString)
197 p.ignore(pyparsing.dblQuotedString)
198 p.ignore(self.commentGrammars)
199 p.ignore(self.commentInProgress)
200 self.commentGrammars.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString).setParseAction(lambda x: '')
201 self.commentInProgress.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString).ignore(pyparsing.cStyleComment)
202 195
203 def do_shortcuts(self, args): 196 def do_shortcuts(self, args):
204 """Lists single-key shortcuts available.""" 197 """Lists single-key shortcuts available."""
205 result = "\n".join('%s: %s' % (sc[0], sc[1]) for sc in self.shortcuts.items()) 198 result = "\n".join('%s: %s' % (sc[0], sc[1]) for sc in self.shortcuts.items())
206 self.stdout.write("Single-key shortcuts for other commands:\n%s\n" % (result)) 199 self.stdout.write("Single-key shortcuts for other commands:\n%s\n" % (result))
217 + pyparsing.Optional(filenamePattern)('outputTo') 210 + pyparsing.Optional(filenamePattern)('outputTo')
218 redirectInPattern = pyparsing.Literal('<')('input') \ 211 redirectInPattern = pyparsing.Literal('<')('input') \
219 + pyparsing.Optional(filenamePattern)('inputFrom') 212 + pyparsing.Optional(filenamePattern)('inputFrom')
220 punctuationPattern = pipePattern ^ redirectInPattern ^ redirectOutPattern 213 punctuationPattern = pipePattern ^ redirectInPattern ^ redirectOutPattern
221 214
222 def p2(self, s, assumeComplete=False): 215
216 def _init_parser(self):
223 ''' 217 '''
224 >>> c = Cmd() 218 >>> c = Cmd()
225 >>> print c.p2('barecommand').dump() 219 >>> print c.parser.parseString('barecommand').dump()
226 >>> print c.p2('command with args').dump() 220 >>> print c.parser.parseString('command with args').dump()
227 >>> print c.p2('command with args and terminator; and suffix').dump() 221 >>> print c.parser.parseString('command with args and terminator; and suffix').dump()
228 >>> print c.p2('command with args, terminator;sufx | piped').dump() 222 >>> print c.parser.parseString('command with args, terminator;sufx | piped').dump()
229 >>> print c.p2('simple | piped').dump() 223 >>> print c.parser.parseString('simple | piped').dump()
230 >>> print c.p2('output into > afile.txt').dump() 224 >>> print c.parser.parseString('output into > afile.txt').dump()
231 >>> print c.p2('output into;sufx | pipethrume plz > afile.txt').dump() 225 >>> print c.parser.parseString('output into;sufx | pipethrume plz > afile.txt').dump()
232 >>> print c.p2('output to paste buffer >> ').dump() 226 >>> print c.parser.parseString('output to paste buffer >> ').dump()
233 >>> print c.p2('ignore the /* commented | > */ stuff;').dump() 227 >>> print c.parser.parseString('ignore the /* commented | > */ stuff;').dump()
228 >>> print c.parser.parseString('do not parse > when formally terminated;').dump()
229 >>> print c.parser.parseString('do not parse > when formally terminated;').dump()
234 ''' 230 '''
235 outputParser = pyparsing.oneOf(['>>','>'])('output') 231 outputParser = pyparsing.oneOf(['>>','>'])('output')
236 terminatorParser = pyparsing.oneOf(self.terminators)('terminator') 232 terminatorParser = pyparsing.oneOf(self.terminators)('terminator')
237 (pyparsing.stringEnd ^ pyparsing.oneOf(self.terminators) ^ '\nEOF' ^ '|' ^ outputParser)('terminator') 233 stringEnd = pyparsing.stringEnd ^ '\nEOF'
238 statementParser = (pyparsing.Word(pyparsing.printables)('command') + 234 command = pyparsing.Word(pyparsing.printables)('command')
239 pyparsing.SkipTo(terminatorParser ^ '\nEOF' ^ '|' ^ outputParser ^ pyparsing.stringEnd)('args') + 235 if self.caseInsensitive:
236 command.setParseAction(lambda x: x[0].lower())
237 statementParser = \
238 (command +
239 pyparsing.SkipTo(terminatorParser)('args') +
240 terminatorParser
241 )('statement') ^ \
242 (command +
243 pyparsing.SkipTo(terminatorParser ^ '|' ^ outputParser ^ stringEnd)('args') +
240 pyparsing.Optional(terminatorParser) 244 pyparsing.Optional(terminatorParser)
241 )('statement') 245 )('statement')
242 self.commentGrammars.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString).setParseAction(lambda x: '') 246 self.commentGrammars.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString).setParseAction(lambda x: '')
243 self.commentInProgress.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString).ignore(pyparsing.cStyleComment) 247 self.commentInProgress.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString).ignore(pyparsing.cStyleComment)
244 parser = statementParser + \ 248 self.parser = statementParser + \
245 pyparsing.SkipTo(outputParser ^ '|' ^ pyparsing.stringEnd)('suffix') + \ 249 pyparsing.SkipTo(outputParser ^ '|' ^ stringEnd)('suffix') + \
246 pyparsing.Optional('|' + pyparsing.SkipTo(outputParser ^ pyparsing.stringEnd)('pipeDest')) + \ 250 pyparsing.Optional('|' + pyparsing.SkipTo(outputParser ^ stringEnd)('pipeDest')) + \
247 pyparsing.Optional(outputParser + pyparsing.SkipTo(pyparsing.stringEnd)('outputDest')) 251 pyparsing.Optional(outputParser + pyparsing.SkipTo(stringEnd)('outputDest'))
248 parser.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString).ignore(self.commentGrammars).ignore(self.commentInProgress) 252 self.parser.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString).ignore(self.commentGrammars).ignore(self.commentInProgress)
249 return parser.parseString(s)
250 253
251 def parsed(self, s, assumeComplete=False): 254 def parsed(self, s, assumeComplete=False):
252 pass 255 pass
253 ''' 256 '''
254 >>> c = Cmd() 257 >>> c = Cmd()