comparison cmd2.py @ 162:c50615cf814f

merged with changes from work
author catherine@Elli.myhome.westell.com
date Tue, 02 Dec 2008 11:00:41 -0500
parents 1d1439be0a55
children 61a57c44cd93
comparison
equal deleted inserted replaced
161:1d1439be0a55 162:c50615cf814f
50 optionParser.add_option(opt) 50 optionParser.add_option(opt)
51 optionParser.set_usage("%s [options] arg" % func.__name__.strip('do_')) 51 optionParser.set_usage("%s [options] arg" % func.__name__.strip('do_'))
52 def newFunc(instance, arg): 52 def newFunc(instance, arg):
53 try: 53 try:
54 opts, newArgs = optionParser.parse_args(arg.split()) 54 opts, newArgs = optionParser.parse_args(arg.split())
55 newArgs = arg[arg.find(newArgs[0]):] 55 newArgs = (newArgs and arg[arg.find(newArgs[0]):]) or ''
56 except (optparse.OptionValueError, optparse.BadOptionError, 56 except (optparse.OptionValueError, optparse.BadOptionError,
57 optparse.OptionError, optparse.AmbiguousOptionError, 57 optparse.OptionError, optparse.AmbiguousOptionError,
58 optparse.OptionConflictError), e: 58 optparse.OptionConflictError), e:
59 print e 59 print e
60 optionParser.print_help() 60 optionParser.print_help()
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, useTerminatorFrom=None):
409 if isinstance(raw, ParsedString):
410 if useTerminatorFrom:
411 raw['terminator'] = useTerminatorFrom.parsed.terminator
412 raw['suffix'] = useTerminatorFrom.parsed.suffix
413 return raw
409 s = self.inputParser.transformString(raw.strip()) 414 s = self.inputParser.transformString(raw.strip())
410 for (shortcut, expansion) in self.shortcuts.items(): 415 for (shortcut, expansion) in self.shortcuts.items():
411 if s.startswith(shortcut): 416 if s.startswith(shortcut):
412 s = s.replace(shortcut, expansion + ' ', 1) 417 s = s.replace(shortcut, expansion + ' ', 1)
413 break 418 break
414 result = self.parser.parseString(s) 419 result = self.parser.parseString(s)
415 result['command'] = result.multilineCommand or result.command 420 result['command'] = result.multilineCommand or result.command
416 #result['statement'] = ' '.join(result.statement)
417 result['raw'] = raw 421 result['raw'] = raw
418 #result['clean'] = self.commentGrammars.transformString(result.statement)
419 result['clean'] = self.commentGrammars.transformString(result.args) 422 result['clean'] = self.commentGrammars.transformString(result.args)
420 result['expanded'] = s 423 result['expanded'] = s
421 if useTerminatorFrom: 424 if useTerminatorFrom:
422 result['terminator'] = useTerminatorFrom.parsed.terminator 425 result['terminator'] = useTerminatorFrom.parsed.terminator
423 result['suffix'] = useTerminatorFrom.parsed.suffix 426 result['suffix'] = useTerminatorFrom.parsed.suffix