diff options
Diffstat (limited to 'parsers.py')
-rw-r--r-- | parsers.py | 59 |
1 files changed, 58 insertions, 1 deletions
@@ -1,3 +1,60 @@ ''' This file should contain all parser definitions for Cmd2. -'''
\ No newline at end of file +''' + +class OptionParser(optparse.OptionParser): + def exit(self, status=0, msg=None): + self.values._exit = True + if msg: + print (msg) + + def print_help(self, *args, **kwargs): + try: + print (self._func.__doc__) + except AttributeError: + pass + optparse.OptionParser.print_help(self, *args, **kwargs) + + def error(self, msg): + """error(msg : string) + + Print a usage message incorporating 'msg' to stderr and exit. + If you override this in a subclass, it should not return -- it + should either exit or raise an exception. + """ + raise optparse.OptParseError(msg) + + +def remaining_args(oldArgs, newArgList): + '''Preserves the original argument's spacing after + removing options. + + >>> remaining_args('-f bar bar cow', ['bar', 'cow']) + 'bar cow' + ''' + pattern = '\s+'.join(re.escape(a) for a in newArgList) + '\s*$' + matchObj = re.search(pattern, oldArgs) + + return oldArgs[matchObj.start():] + + +class ParsedString(str): + def full_parsed_statement(self): + new = ParsedString('%s %s' % (self.parsed.command, self.parsed.args)) + new.parsed = self.parsed + new.parser = self.parser + return new + def with_args_replaced(self, newargs): + new = ParsedString(newargs) + new.parsed = self.parsed + new.parser = self.parser + new.parsed['args'] = newargs + new.parsed.statement['args'] = newargs + return new + + +optparse.Values.get = _attr_get_ + +options_defined = [] # used to distinguish --options from SQL-style --comments + +pyparsing.ParserElement.setDefaultWhitespaceChars(' \t')
\ No newline at end of file |