comparison cmd2.py @ 158:87d3f3203b96

accept blanks
author catherine@Elli.myhome.westell.com
date Mon, 01 Dec 2008 01:34:05 -0500
parents 10e917acf787
children 5a94501b6b93
comparison
equal deleted inserted replaced
157:10e917acf787 158:87d3f3203b96
231 ''' 231 '''
232 >>> c = Cmd() 232 >>> c = Cmd()
233 >>> c.multilineCommands = ['multiline'] 233 >>> c.multilineCommands = ['multiline']
234 >>> c.caseInsensitive = True 234 >>> c.caseInsensitive = True
235 >>> c._init_parser() 235 >>> c._init_parser()
236 >>> print c.parser.parseString('').dump()
237 []
238 >>> print c.parser.parseString('/* empty command */').dump()
239 []
240 >>> print c.parser.parseString('plainword').dump()
241 ['plainword', '']
242 - command: plainword
243 - statement: ['plainword', '']
244 - command: plainword
236 >>> print c.parser.parseString('termbare;').dump() 245 >>> print c.parser.parseString('termbare;').dump()
246 ['termbare', '', ';', '']
247 - command: termbare
248 - statement: ['termbare', '', ';']
249 - command: termbare
250 - terminator: ;
251 - terminator: ;
237 >>> print c.parser.parseString('termbare; suffx').dump() 252 >>> print c.parser.parseString('termbare; suffx').dump()
253 ['termbare', '', ';', 'suffx']
254 - command: termbare
255 - statement: ['termbare', '', ';']
256 - command: termbare
257 - terminator: ;
258 - suffix: suffx
259 - terminator: ;
238 >>> print c.parser.parseString('barecommand').dump() 260 >>> print c.parser.parseString('barecommand').dump()
239 ['barecommand', ''] 261 ['barecommand', '']
240 - command: barecommand 262 - command: barecommand
241 - statement: ['barecommand', ''] 263 - statement: ['barecommand', '']
242 - command: barecommand 264 - command: barecommand
246 - command: command 268 - command: command
247 - statement: ['command', 'with args'] 269 - statement: ['command', 'with args']
248 - args: with args 270 - args: with args
249 - command: command 271 - command: command
250 >>> print c.parser.parseString('command with args and terminator; and suffix').dump() 272 >>> print c.parser.parseString('command with args and terminator; and suffix').dump()
251 ['command', 'with args and terminator', ';', ' and suffix'] 273 ['command', 'with args and terminator', ';', 'and suffix']
252 - args: with args and terminator 274 - args: with args and terminator
253 - command: command 275 - command: command
254 - statement: ['command', 'with args and terminator', ';'] 276 - statement: ['command', 'with args and terminator', ';']
255 - args: with args and terminator 277 - args: with args and terminator
256 - command: command 278 - command: command
257 - terminator: ; 279 - terminator: ;
258 - suffix: and suffix 280 - suffix: and suffix
259 - terminator: ; 281 - terminator: ;
260 >>> print c.parser.parseString('simple | piped').dump() 282 >>> print c.parser.parseString('simple | piped').dump()
261 ['simple', '', '|', ' piped'] 283 ['simple', '', '|', ' piped']
262 - command: simple 284 - command: simple
263 - pipeTo: piped 285 - pipeTo: piped
264 - statement: ['simple', ''] 286 - statement: ['simple', '']
310 - command: ignore 332 - command: ignore
311 - statement: ['ignore', 'the /* commented | > */ stuff', ';'] 333 - statement: ['ignore', 'the /* commented | > */ stuff', ';']
312 - args: the /* commented | > */ stuff 334 - args: the /* commented | > */ stuff
313 - command: ignore 335 - command: ignore
314 - terminator: ; 336 - terminator: ;
315 - suffix:
316 - terminator: ; 337 - terminator: ;
317 >>> print c.parser.parseString('has > inside;').dump() 338 >>> print c.parser.parseString('has > inside;').dump()
318 ['has', '> inside', ';', ''] 339 ['has', '> inside', ';', '']
319 - args: > inside 340 - args: > inside
320 - command: has 341 - command: has
321 - statement: ['has', '> inside', ';'] 342 - statement: ['has', '> inside', ';']
322 - args: > inside 343 - args: > inside
323 - command: has 344 - command: has
324 - terminator: ; 345 - terminator: ;
325 - suffix:
326 - terminator: ; 346 - terminator: ;
327 >>> print c.parser.parseString('multiline has > inside an unfinished command').dump() 347 >>> print c.parser.parseString('multiline has > inside an unfinished command').dump()
328 ['multiline', 'has > inside an unfinished command'] 348 ['multiline', 'has > inside an unfinished command']
329 - multilineCommand: multiline 349 - multilineCommand: multiline
330 >>> print c.parser.parseString('multiline has > inside;').dump() 350 >>> print c.parser.parseString('multiline has > inside;').dump()
333 - multilineCommand: multiline 353 - multilineCommand: multiline
334 - statement: ['multiline', 'has > inside', ';'] 354 - statement: ['multiline', 'has > inside', ';']
335 - args: has > inside 355 - args: has > inside
336 - multilineCommand: multiline 356 - multilineCommand: multiline
337 - terminator: ; 357 - terminator: ;
338 - suffix:
339 - terminator: ; 358 - terminator: ;
340 >>> print c.parser.parseString('multiline command /* with comment in progress;').dump() 359 >>> print c.parser.parseString('multiline command /* with comment in progress;').dump()
341 ['multiline', 'command /* with comment in progress;'] 360 ['multiline', 'command /* with comment in progress;']
342 - multilineCommand: multiline 361 - multilineCommand: multiline
343 >>> print c.parser.parseString('multiline command /* with comment complete */ is done;').dump() 362 >>> print c.parser.parseString('multiline command /* with comment complete */ is done;').dump()
346 - multilineCommand: multiline 365 - multilineCommand: multiline
347 - statement: ['multiline', 'command /* with comment complete */ is done', ';'] 366 - statement: ['multiline', 'command /* with comment complete */ is done', ';']
348 - args: command /* with comment complete */ is done 367 - args: command /* with comment complete */ is done
349 - multilineCommand: multiline 368 - multilineCommand: multiline
350 - terminator: ; 369 - terminator: ;
351 - suffix: 370 - terminator: ;
352 - terminator: ;
353 ''' 371 '''
354 outputParser = pyparsing.oneOf(['>>','>'])('output') 372 outputParser = pyparsing.oneOf(['>>','>'])('output')
355 terminatorParser = pyparsing.oneOf(self.terminators)('terminator') 373 terminatorParser = pyparsing.oneOf(self.terminators)('terminator')
356 stringEnd = pyparsing.stringEnd ^ '\nEOF' 374 stringEnd = pyparsing.stringEnd ^ '\nEOF'
357 multilineCommand = pyparsing.Or([pyparsing.Keyword(c, caseless=self.caseInsensitive) for c in self.multilineCommands])('multilineCommand') 375 multilineCommand = pyparsing.Or([pyparsing.Keyword(c, caseless=self.caseInsensitive) for c in self.multilineCommands])('multilineCommand')
358 oneLineCommand = pyparsing.Word(self.legalChars)('command') 376 oneLineCommand = pyparsing.Word(self.legalChars + pyparsing.alphanums + pyparsing.alphas8bit)('command')
359 afterElements = \ 377 afterElements = \
360 pyparsing.Optional('|' + pyparsing.SkipTo(outputParser ^ stringEnd)('pipeTo')) + \ 378 pyparsing.Optional('|' + pyparsing.SkipTo(outputParser ^ stringEnd)('pipeTo')) + \
361 pyparsing.Optional(outputParser + pyparsing.SkipTo(stringEnd).setParseAction(lambda x: x[0].strip())('outputTo')) 379 pyparsing.Optional(outputParser + pyparsing.SkipTo(stringEnd).setParseAction(lambda x: x[0].strip())('outputTo'))
362 if self.caseInsensitive: 380 if self.caseInsensitive:
363 multilineCommand.setParseAction(lambda x: x[0].lower()) 381 multilineCommand.setParseAction(lambda x: x[0].lower())
364 oneLineCommand.setParseAction(lambda x: x[0].lower()) 382 oneLineCommand.setParseAction(lambda x: x[0].lower())
365 self.parser = ( 383 self.parser = (
384 pyparsing.stringEnd
385 ^
366 (((multilineCommand ^ oneLineCommand) + pyparsing.SkipTo(terminatorParser).setParseAction(lambda x: x[0].strip())('args') + terminatorParser)('statement') + 386 (((multilineCommand ^ oneLineCommand) + pyparsing.SkipTo(terminatorParser).setParseAction(lambda x: x[0].strip())('args') + terminatorParser)('statement') +
367 pyparsing.SkipTo(outputParser ^ '|' ^ stringEnd)('suffix') + afterElements) 387 pyparsing.SkipTo(outputParser ^ '|' ^ stringEnd).setParseAction(lambda x: x[0].strip())('suffix') + afterElements)
368 ^ 388 ^
369 multilineCommand + pyparsing.SkipTo(pyparsing.stringEnd) 389 multilineCommand + pyparsing.SkipTo(pyparsing.stringEnd)
370 ^ 390 ^
371 ((oneLineCommand + pyparsing.SkipTo(terminatorParser ^ stringEnd ^ '|' ^ outputParser).setParseAction(lambda x:x[0].strip())('args'))('statement') + 391 ((oneLineCommand + pyparsing.SkipTo(terminatorParser ^ stringEnd ^ '|' ^ outputParser).setParseAction(lambda x:x[0].strip())('args'))('statement') +
372 pyparsing.Optional(terminatorParser) + afterElements) 392 pyparsing.Optional(terminatorParser) + afterElements)
910 def tearDown(self): 930 def tearDown(self):
911 if self.CmdApp: 931 if self.CmdApp:
912 self.outputTrap.tearDown() 932 self.outputTrap.tearDown()
913 933
914 if __name__ == '__main__': 934 if __name__ == '__main__':
915 #doctest.testmod(optionflags = doctest.NORMALIZE_WHITESPACE) 935 doctest.testmod(optionflags = doctest.NORMALIZE_WHITESPACE)
916 c = Cmd() 936 #c = Cmd()