comparison cmd2.py @ 285:58be960b8bf9

--flags with sqlpython
author catherine@DellZilla
date Thu, 15 Oct 2009 19:03:29 -0400
parents e02b85e6aee1
children 3c4ba65cb303
comparison
equal deleted inserted replaced
284:e02b85e6aee1 285:58be960b8bf9
51 """ 51 """
52 raise 52 raise
53 53
54 def remainingArgs(oldArgs, newArgList): 54 def remainingArgs(oldArgs, newArgList):
55 ''' 55 '''
56 Preserves the spacing originally in the argument after
57 the removal of options.
58
56 >>> remainingArgs('-f bar bar cow', ['bar', 'cow']) 59 >>> remainingArgs('-f bar bar cow', ['bar', 'cow'])
57 'bar cow' 60 'bar cow'
58 ''' 61 '''
59 pattern = '\s+'.join(re.escape(a) for a in newArgList) + '\s*$' 62 pattern = '\s+'.join(re.escape(a) for a in newArgList) + '\s*$'
60 matchObj = re.search(pattern, oldArgs) 63 matchObj = re.search(pattern, oldArgs)
77 optionParser.add_option(opt) 80 optionParser.add_option(opt)
78 optionParser.set_usage("%s [options] arg" % func.__name__.strip('do_')) 81 optionParser.set_usage("%s [options] arg" % func.__name__.strip('do_'))
79 optionParser._func = func 82 optionParser._func = func
80 def newFunc(instance, arg): 83 def newFunc(instance, arg):
81 try: 84 try:
82 opts, newArgList = optionParser.parse_args(arg.split()) # doesn't understand quoted strings shouldn't be dissected! 85 if hasattr(arg, 'parsed'):
83 newArgs = remainingArgs(arg, newArgList) # should it permit flags after args? 86 args = arg.parsed.raw
87 else:
88 args = arg
89 opts, newArgList = optionParser.parse_args(args.split()) # doesn't understand quoted strings shouldn't be dissected!
90 # Must find the remaining args in the original argument list, but
91 # mustn't include the command itself
92 if hasattr(arg, 'parsed') and newArgList[0] == arg.parsed.command:
93 newArgList = newArgList[1:]
94 newArgs = remainingArgs(args, newArgList) # should it permit flags after args?
84 except (optparse.OptionValueError, optparse.BadOptionError, 95 except (optparse.OptionValueError, optparse.BadOptionError,
85 optparse.OptionError, optparse.AmbiguousOptionError, 96 optparse.OptionError, optparse.AmbiguousOptionError,
86 optparse.OptionConflictError), e: 97 optparse.OptionConflictError), e:
87 print e 98 print e
88 optionParser.print_help() 99 optionParser.print_help()
517 terminatorParser = pyparsing.Or([(hasattr(t, 'parseString') and t) or pyparsing.Literal(t) for t in self.terminators])('terminator') 528 terminatorParser = pyparsing.Or([(hasattr(t, 'parseString') and t) or pyparsing.Literal(t) for t in self.terminators])('terminator')
518 stringEnd = pyparsing.stringEnd ^ '\nEOF' 529 stringEnd = pyparsing.stringEnd ^ '\nEOF'
519 self.multilineCommand = pyparsing.Or([pyparsing.Keyword(c, caseless=self.case_insensitive) for c in self.multilineCommands])('multilineCommand') 530 self.multilineCommand = pyparsing.Or([pyparsing.Keyword(c, caseless=self.case_insensitive) for c in self.multilineCommands])('multilineCommand')
520 oneLineCommand = (~self.multilineCommand + pyparsing.Word(self.legalChars))('command') 531 oneLineCommand = (~self.multilineCommand + pyparsing.Word(self.legalChars))('command')
521 pipe = pyparsing.Keyword('|', identChars='|') 532 pipe = pyparsing.Keyword('|', identChars='|')
522 self.commentGrammars.ignore(pyparsing.quotedString).setParseAction(lambda x: '') 533 self.commentGrammars.ignore(pyparsing.quotedString).setParseAction(lambda x: '')
523 self.commentInProgress.ignore(pyparsing.quotedString).ignore(pyparsing.cStyleComment)
524 afterElements = \ 534 afterElements = \
525 pyparsing.Optional(pipe + pyparsing.SkipTo(outputParser ^ stringEnd)('pipeTo')) + \ 535 pyparsing.Optional(pipe + pyparsing.SkipTo(outputParser ^ stringEnd)('pipeTo')) + \
526 pyparsing.Optional(outputParser + pyparsing.SkipTo(stringEnd).setParseAction(lambda x: x[0].strip())('outputTo')) 536 pyparsing.Optional(outputParser + pyparsing.SkipTo(stringEnd).setParseAction(lambda x: x[0].strip())('outputTo'))
527 if self.case_insensitive: 537 if self.case_insensitive:
528 self.multilineCommand.setParseAction(lambda x: x[0].lower()) 538 self.multilineCommand.setParseAction(lambda x: x[0].lower())