comparison cmd2.py @ 283:bff1f19275e7

unknown
author catherine@DellZilla
date Fri, 09 Oct 2009 09:10:17 -0400
parents 7c6eb0fc75ef
children e02b85e6aee1
comparison
equal deleted inserted replaced
282:7c6eb0fc75ef 283:bff1f19275e7
65 Analagous to .get() for dictionaries.''' 65 Analagous to .get() for dictionaries.'''
66 try: 66 try:
67 return getattr(obj, attr) 67 return getattr(obj, attr)
68 except AttributeError: 68 except AttributeError:
69 return None 69 return None
70 70
71 optparse.Values.get = _attr_get_
72
71 def options(option_list): 73 def options(option_list):
72 def option_setup(func): 74 def option_setup(func):
73 optionParser = OptionParser() 75 optionParser = OptionParser()
74 for opt in option_list: 76 for opt in option_list:
75 optionParser.add_option(opt) 77 optionParser.add_option(opt)
76 optionParser.set_usage("%s [options] arg" % func.__name__.strip('do_')) 78 optionParser.set_usage("%s [options] arg" % func.__name__.strip('do_'))
77 optionParser._func = func 79 optionParser._func = func
78 def newFunc(instance, arg): 80 def newFunc(instance, arg):
79 try: 81 try:
80 opts, newArgList = optionParser.parse_args(arg.split()) # doesn't understand quoted strings shouldn't be dissected! 82 opts, newArgList = optionParser.parse_args(arg.split()) # doesn't understand quoted strings shouldn't be dissected!
81 opts.get = _attr_get_
82 newArgs = remainingArgs(arg, newArgList) # should it permit flags after args? 83 newArgs = remainingArgs(arg, newArgList) # should it permit flags after args?
83 except (optparse.OptionValueError, optparse.BadOptionError, 84 except (optparse.OptionValueError, optparse.BadOptionError,
84 optparse.OptionError, optparse.AmbiguousOptionError, 85 optparse.OptionError, optparse.AmbiguousOptionError,
85 optparse.OptionConflictError), e: 86 optparse.OptionConflictError), e:
86 print e 87 print e