comparison cmd2.py @ 413:f16f444a4d10

added arg_desc to @options, thanks Renzo Crispiatico
author Catherine Devlin <catherine.devlin@gmail.com>
date Fri, 12 Nov 2010 20:03:21 -0500
parents 9d5ff2ddfdea
children 731f2c93c1cd
comparison
equal deleted inserted replaced
412:5bd1d2b11548 413:f16f444a4d10
95 95
96 optparse.Values.get = _attr_get_ 96 optparse.Values.get = _attr_get_
97 97
98 options_defined = [] # used to distinguish --options from SQL-style --comments 98 options_defined = [] # used to distinguish --options from SQL-style --comments
99 99
100 def options(option_list): 100 def options(option_list, arg_desc="arg"):
101 '''Used as a decorator and passed a list of optparse-style options, 101 '''Used as a decorator and passed a list of optparse-style options,
102 alters a cmd2 method to populate its ``opts`` argument from its 102 alters a cmd2 method to populate its ``opts`` argument from its
103 raw text argument. 103 raw text argument.
104 104
105 Example: transform 105 Example: transform
106 def do_something(self, arg): 106 def do_something(self, arg):
107 107
108 into 108 into
109 @options([make_option('-q', '--quick', action="store_true", 109 @options([make_option('-q', '--quick', action="store_true",
110 help="Makes things fast")]) 110 help="Makes things fast")],
111 "source dest")
111 def do_something(self, arg, opts): 112 def do_something(self, arg, opts):
112 if opts.quick: 113 if opts.quick:
113 self.fast_button = True 114 self.fast_button = True
114 ''' 115 '''
115 if not isinstance(option_list, list): 116 if not isinstance(option_list, list):
118 options_defined.append(pyparsing.Literal(opt.get_opt_string())) 119 options_defined.append(pyparsing.Literal(opt.get_opt_string()))
119 def option_setup(func): 120 def option_setup(func):
120 optionParser = OptionParser() 121 optionParser = OptionParser()
121 for opt in option_list: 122 for opt in option_list:
122 optionParser.add_option(opt) 123 optionParser.add_option(opt)
123 optionParser.set_usage("%s [options] arg" % func.__name__[3:]) 124 optionParser.set_usage("%s [options] %s" % (func.__name__[3:], arg_desc))
124 optionParser._func = func 125 optionParser._func = func
125 def new_func(instance, arg): 126 def new_func(instance, arg):
126 try: 127 try:
127 opts, newArgList = optionParser.parse_args(arg.split()) 128 opts, newArgList = optionParser.parse_args(arg.split())
128 # Must find the remaining args in the original argument list, but 129 # Must find the remaining args in the original argument list, but