# HG changeset patch # User catherine@DellZilla # Date 1254936838 14400 # Node ID 38198b11f48c7f113f6301d9f4b44e694a42c205 # Parent c6064bfd4d6c865e402f6aadb07c353268040960 adding .get() to opts diff -r c6064bfd4d6c -r 38198b11f48c cmd2.py --- a/cmd2.py Sat Aug 08 00:16:27 2009 -0400 +++ b/cmd2.py Wed Oct 07 13:33:58 2009 -0400 @@ -26,7 +26,7 @@ import unittest, string, datetime, urllib, glob from code import InteractiveConsole, InteractiveInterpreter, softspace from optparse import make_option -__version__ = '0.5.5' +__version__ = '0.5.6' class OptionParser(optparse.OptionParser): def exit(self, status=0, msg=None): @@ -59,6 +59,14 @@ pattern = '\s+'.join(re.escape(a) for a in newArgList) + '\s*$' matchObj = re.search(pattern, oldArgs) return oldArgs[matchObj.start():] + +def _attr_get_(obj, attr): + '''Returns an attribute's value, or None (no error) if undefined. + Analagous to .get() for dictionaries.''' + try: + return getattr(obj, attr) + except AttributeError: + return None def options(option_list): def option_setup(func): @@ -70,6 +78,7 @@ def newFunc(instance, arg): try: opts, newArgList = optionParser.parse_args(arg.split()) # doesn't understand quoted strings shouldn't be dissected! + opts.get = _attr_get_ newArgs = remainingArgs(arg, newArgList) # should it permit flags after args? except (optparse.OptionValueError, optparse.BadOptionError, optparse.OptionError, optparse.AmbiguousOptionError, @@ -563,7 +572,7 @@ result = self.parser.parseString(s) result['command'] = result.multilineCommand or result.command result['raw'] = raw - result['clean'] = self.commentGrammars.transformString(result.args) + result['clean'] = self.commentGrammars.transformString(result.args) # oh no, strips ls box/* result['expanded'] = s p = ParsedString(result.clean) p.parsed = result @@ -815,6 +824,7 @@ End with `Ctrl-D` (Unix) / `Ctrl-Z` (Windows), `quit()`, 'exit()`. Non-python commands can be issued with `cmd("your command")`. ''' + self.pystate['self'] = self arg = arg.parsed.raw[2:].strip() if arg.strip(): interp = InteractiveInterpreter(locals=self.pystate) diff -r c6064bfd4d6c -r 38198b11f48c setup.py --- a/setup.py Sat Aug 08 00:16:27 2009 -0400 +++ b/setup.py Wed Oct 07 13:33:58 2009 -0400 @@ -3,7 +3,7 @@ setup( name="cmd2", - version="0.5.5", + version="0.5.6", py_modules=["cmd2",], # metadata for upload to PyPI