comparison cmd2.py @ 165:3dc882a00e53

terminators + suffixes now preserved
author catherine@dellzilla
date Thu, 04 Dec 2008 16:43:57 -0500
parents 5209d230f96b
children a3414ac38677
comparison
equal deleted inserted replaced
164:5209d230f96b 165:3dc882a00e53
59 print e 59 print e
60 optionParser.print_help() 60 optionParser.print_help()
61 return 61 return
62 if hasattr(opts, '_exit'): 62 if hasattr(opts, '_exit'):
63 return None 63 return None
64 arg = arg.parser('%s %s' % (arg.parsed.command, newArgs)) 64 arg = arg.parser('%s %s%s%s' % (arg.parsed.command, newArgs, arg.parsed.terminator, arg.parsed.suffix))
65 result = func(instance, arg, opts) 65 result = func(instance, arg, opts)
66 return result 66 return result
67 newFunc.__doc__ = '%s\n%s' % (func.__doc__, optionParser.format_help()) 67 newFunc.__doc__ = '%s\n%s' % (func.__doc__, optionParser.format_help())
68 return newFunc 68 return newFunc
69 return option_setup 69 return option_setup
403 inputFrom = pyparsing.Word(self.legalChars)('inputFrom') 403 inputFrom = pyparsing.Word(self.legalChars)('inputFrom')
404 inputFrom.setParseAction(lambda x: (x and open(x[0]).read()) or getPasteBuffer()) 404 inputFrom.setParseAction(lambda x: (x and open(x[0]).read()) or getPasteBuffer())
405 self.inputParser = inputMark + pyparsing.Optional(inputFrom) 405 self.inputParser = inputMark + pyparsing.Optional(inputFrom)
406 self.inputParser.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString).ignore(self.commentGrammars).ignore(self.commentInProgress) 406 self.inputParser.ignore(pyparsing.sglQuotedString).ignore(pyparsing.dblQuotedString).ignore(self.commentGrammars).ignore(self.commentInProgress)
407 407
408 def parsed(self, raw, useTerminatorFrom=None): 408 def parsed(self, raw, **kwargs):
409 if isinstance(raw, ParsedString): 409 if isinstance(raw, ParsedString):
410 if useTerminatorFrom: 410 p = raw
411 raw['terminator'] = useTerminatorFrom.parsed.terminator 411 else:
412 raw['suffix'] = useTerminatorFrom.parsed.suffix 412 s = self.inputParser.transformString(raw.strip())
413 return raw 413 for (shortcut, expansion) in self.shortcuts.items():
414 s = self.inputParser.transformString(raw.strip()) 414 if s.startswith(shortcut):
415 for (shortcut, expansion) in self.shortcuts.items(): 415 s = s.replace(shortcut, expansion + ' ', 1)
416 if s.startswith(shortcut): 416 break
417 s = s.replace(shortcut, expansion + ' ', 1) 417 result = self.parser.parseString(s)
418 break 418 result['command'] = result.multilineCommand or result.command
419 result = self.parser.parseString(s) 419 result['raw'] = raw
420 result['command'] = result.multilineCommand or result.command 420 result['clean'] = self.commentGrammars.transformString(result.args)
421 if useTerminatorFrom: 421 result['expanded'] = s
422 return self.parsed('%s %s%s%s' % (result.command, result.args, useTerminatorFrom.parsed.terminator, useTerminatorFrom.parsed.suffix)) 422 p = ParsedString(result.args)
423 result['raw'] = raw 423 p.parsed = result
424 result['clean'] = self.commentGrammars.transformString(result.args) 424 p.parser = self.parsed
425 result['expanded'] = s 425 for (key, val) in kwargs.items():
426 p = ParsedString(result.args) 426 p.parsed[key] = val
427 p.parsed = result
428 p.parser = self.parsed
429 return p 427 return p
430 428
431 def onecmd(self, line, assumeComplete=False): 429 def onecmd(self, line, assumeComplete=False):
432 """Interpret the argument as though it had been typed in response 430 """Interpret the argument as though it had been typed in response
433 to the prompt. 431 to the prompt.