comparison cmd2.py @ 171:0b93f1a4076c

lstripping commands
author catherine@dellzilla
date Fri, 12 Dec 2008 09:54:08 -0500
parents 310ebf4baa7a
children c01f8e612a0a
comparison
equal deleted inserted replaced
170:310ebf4baa7a 171:0b93f1a4076c
199 Commands are %(casesensitive)scase-sensitive. 199 Commands are %(casesensitive)scase-sensitive.
200 Commands may be terminated with: %(terminators)s 200 Commands may be terminated with: %(terminators)s
201 Settable parameters: %(settable)s 201 Settable parameters: %(settable)s
202 """ % 202 """ %
203 { 'casesensitive': ('not ' and self.caseInsensitive) or '', 203 { 'casesensitive': ('not ' and self.caseInsensitive) or '',
204 'terminators': self.terminatorPattern, 204 'terminators': str(self.terminators),
205 'settable': ' '.join(self.settable) 205 'settable': ' '.join(self.settable)
206 }) 206 })
207 207
208 def do_help(self, arg): 208 def do_help(self, arg):
209 cmd.Cmd.do_help(self, arg) 209 cmd.Cmd.do_help(self, arg)
225 self.stdout.write("Single-key shortcuts for other commands:\n%s\n" % (result)) 225 self.stdout.write("Single-key shortcuts for other commands:\n%s\n" % (result))
226 226
227 commentGrammars = pyparsing.Or([pyparsing.pythonStyleComment, pyparsing.cStyleComment]) 227 commentGrammars = pyparsing.Or([pyparsing.pythonStyleComment, pyparsing.cStyleComment])
228 commentGrammars.addParseAction(lambda x: '') 228 commentGrammars.addParseAction(lambda x: '')
229 commentInProgress = pyparsing.Literal('/*') + pyparsing.SkipTo(pyparsing.stringEnd) 229 commentInProgress = pyparsing.Literal('/*') + pyparsing.SkipTo(pyparsing.stringEnd)
230 terminators = [';', '\n\n'] 230 terminators = [';']
231 blankLinesAllowed = False
231 multilineCommands = [] 232 multilineCommands = []
232 233
233 def _init_parser(self): 234 def _init_parser(self):
234 ''' 235 '''
235 >>> c = Cmd() 236 >>> c = Cmd()
371 - multilineCommand: multiline 372 - multilineCommand: multiline
372 - terminator: ; 373 - terminator: ;
373 - terminator: ; 374 - terminator: ;
374 ''' 375 '''
375 outputParser = pyparsing.oneOf(['>>','>'])('output') 376 outputParser = pyparsing.oneOf(['>>','>'])('output')
376 terminatorParser = pyparsing.oneOf(self.terminators)('terminator') 377 terminatorParser = pyparsing.Or([(hasattr(t, 'parseString') and t) or pyparsing.Literal(t) for t in self.terminators])('terminator')
377 stringEnd = pyparsing.stringEnd ^ '\nEOF' 378 stringEnd = pyparsing.stringEnd ^ '\nEOF'
378 multilineCommand = pyparsing.Or([pyparsing.Keyword(c, caseless=self.caseInsensitive) for c in self.multilineCommands])('multilineCommand') 379 multilineCommand = pyparsing.Or([pyparsing.Keyword(c, caseless=self.caseInsensitive) for c in self.multilineCommands])('multilineCommand')
379 oneLineCommand = pyparsing.Word(self.legalChars)('command') 380 oneLineCommand = pyparsing.Word(self.legalChars)('command')
380 pipe = pyparsing.Keyword('|', identChars='|') 381 pipe = pyparsing.Keyword('|', identChars='|')
381 self.commentGrammars.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString).setParseAction(lambda x: '') 382 self.commentGrammars.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString).setParseAction(lambda x: '')
384 pyparsing.Optional(pipe + pyparsing.SkipTo(outputParser ^ stringEnd)('pipeTo')) + \ 385 pyparsing.Optional(pipe + pyparsing.SkipTo(outputParser ^ stringEnd)('pipeTo')) + \
385 pyparsing.Optional(outputParser + pyparsing.SkipTo(stringEnd).setParseAction(lambda x: x[0].strip())('outputTo')) 386 pyparsing.Optional(outputParser + pyparsing.SkipTo(stringEnd).setParseAction(lambda x: x[0].strip())('outputTo'))
386 if self.caseInsensitive: 387 if self.caseInsensitive:
387 multilineCommand.setParseAction(lambda x: x[0].lower()) 388 multilineCommand.setParseAction(lambda x: x[0].lower())
388 oneLineCommand.setParseAction(lambda x: x[0].lower()) 389 oneLineCommand.setParseAction(lambda x: x[0].lower())
390 blankLineTerminator = (pyparsing.Literal('\n') + pyparsing.stringEnd)('terminator')
391 if self.blankLinesAllowed:
392 subparser0 = pyparsing.NoMatch
393 else:
394 subparser0 = ((multilineCommand ^ oneLineCommand) + pyparsing.SkipTo(blankLineTerminator).setParseAction(lambda x: x[0].strip())('args') + terminatorParser)('statement')
389 subparser1 = (((multilineCommand ^ oneLineCommand) + pyparsing.SkipTo(terminatorParser).setParseAction(lambda x: x[0].strip())('args') + terminatorParser)('statement') + 395 subparser1 = (((multilineCommand ^ oneLineCommand) + pyparsing.SkipTo(terminatorParser).setParseAction(lambda x: x[0].strip())('args') + terminatorParser)('statement') +
390 pyparsing.SkipTo(outputParser ^ pipe ^ stringEnd).setParseAction(lambda x: x[0].strip())('suffix') + afterElements) 396 pyparsing.SkipTo(outputParser ^ pipe ^ stringEnd).setParseAction(lambda x: x[0].strip())('suffix') + afterElements)
397 #subparser1 = (((multilineCommand ^ oneLineCommand) + pyparsing.SkipTo(terminatorParser).setParseAction(lambda x: x[0].strip())('args') + terminatorParser)('statement') +
398 # pyparsing.Optional(pyparsing.SkipTo(outputParser ^ pipe ^ stringEnd).setParseAction(lambda x: x[0].strip()))('suffix') + afterElements)
391 subparser2 = ((oneLineCommand + pyparsing.SkipTo(terminatorParser ^ stringEnd ^ pipe ^ outputParser).setParseAction(lambda x:x[0].strip())('args'))('statement') + 399 subparser2 = ((oneLineCommand + pyparsing.SkipTo(terminatorParser ^ stringEnd ^ pipe ^ outputParser).setParseAction(lambda x:x[0].strip())('args'))('statement') +
392 pyparsing.Optional(terminatorParser) + afterElements) 400 pyparsing.Optional(terminatorParser) + afterElements)
393 self.parser = ( 401 self.parser = (
394 pyparsing.stringEnd 402 pyparsing.stringEnd
403 |
404 subparser0
395 | 405 |
396 subparser1 406 subparser1
397 | 407 |
398 multilineCommand + pyparsing.SkipTo(pyparsing.stringEnd) 408 multilineCommand + pyparsing.SkipTo(pyparsing.stringEnd)
399 | 409 |
410 420
411 def parsed(self, raw, **kwargs): 421 def parsed(self, raw, **kwargs):
412 if isinstance(raw, ParsedString): 422 if isinstance(raw, ParsedString):
413 p = raw 423 p = raw
414 else: 424 else:
415 s = self.inputParser.transformString(raw) # used to be raw.strip() 425 s = self.inputParser.transformString(raw.lstrip())
416 for (shortcut, expansion) in self.shortcuts.items(): 426 for (shortcut, expansion) in self.shortcuts.items():
417 if s.startswith(shortcut): 427 if s.startswith(shortcut):
418 s = s.replace(shortcut, expansion + ' ', 1) 428 s = s.replace(shortcut, expansion + ' ', 1)
419 break 429 break
420 result = self.parser.parseString(s) 430 result = self.parser.parseString(s)
439 commands by the interpreter should stop. 449 commands by the interpreter should stop.
440 450
441 This (`cmd2`) version of `onecmd` already override's `cmd`'s `onecmd`. 451 This (`cmd2`) version of `onecmd` already override's `cmd`'s `onecmd`.
442 452
443 """ 453 """
444 #line = line.strip() # trying to allow proper \n\n endings
445 if not line: 454 if not line:
446 return self.emptyline() 455 return self.emptyline()
447 if not pyparsing.Or(self.commentGrammars).setParseAction(lambda x: '').transformString(line): 456 if not pyparsing.Or(self.commentGrammars).setParseAction(lambda x: '').transformString(line):
448 return 0 457 return 0
449 try: 458 try: