Mercurial > python-cmd2
comparison cmd2.py @ 301:30af90fd46c5
refactor mostly done?
author | catherine@dellzilla |
---|---|
date | Tue, 26 Jan 2010 18:04:20 -0500 |
parents | 1e4773b325d1 |
children | b4b22dd087dc |
comparison
equal
deleted
inserted
replaced
300:1e4773b325d1 | 301:30af90fd46c5 |
---|---|
113 def new_func(instance, arg): | 113 def new_func(instance, arg): |
114 try: | 114 try: |
115 if hasattr(arg, 'parsed'): | 115 if hasattr(arg, 'parsed'): |
116 args = arg.parsed.raw | 116 args = arg.parsed.raw |
117 else: | 117 else: |
118 print 'raw arg passed to new_func!' | |
118 args = arg | 119 args = arg |
119 opts, newArgList = optionParser.parse_args(args.split()) | 120 opts, newArgList = optionParser.parse_args(args.split()) |
120 # Must find the remaining args in the original argument list, but | 121 # Must find the remaining args in the original argument list, but |
121 # mustn't include the command itself | 122 # mustn't include the command itself |
122 if hasattr(arg, 'parsed') and newArgList[0] == arg.parsed.command: | 123 if hasattr(arg, 'parsed') and newArgList[0] == arg.parsed.command: |
123 newArgList = newArgList[1:] | 124 newArgList = newArgList[1:] |
124 newArgs = remaining_args(args, newArgList) | 125 newArgs = remaining_args(args, newArgList) |
126 if isinstance(arg, ParsedString): | |
127 arg = arg.with_args_replaced(newArgs) | |
128 else: | |
129 arg = newArgs | |
125 except (optparse.OptionValueError, optparse.BadOptionError, | 130 except (optparse.OptionValueError, optparse.BadOptionError, |
126 optparse.OptionError, optparse.AmbiguousOptionError, | 131 optparse.OptionError, optparse.AmbiguousOptionError, |
127 optparse.OptionConflictError), e: | 132 optparse.OptionConflictError), e: |
128 print e | 133 print e |
129 optionParser.print_help() | 134 optionParser.print_help() |
130 return | 135 return |
131 if hasattr(opts, '_exit'): | 136 if hasattr(opts, '_exit'): |
132 return None | 137 return None |
133 if hasattr(arg, 'parsed'): | |
134 arg.parsed.args = newArgs | |
135 terminator = arg.parsed.terminator | |
136 try: | |
137 if arg.parsed.terminator[0] == '\n': | |
138 terminator = arg.parsed.terminator[0] | |
139 except IndexError: | |
140 pass | |
141 arg = arg.parser('%s %s%s%s' % (arg.parsed.command, newArgs, | |
142 terminator, arg.parsed.suffix)) | |
143 else: | |
144 arg = newArgs | |
145 result = func(instance, arg, opts) | 138 result = func(instance, arg, opts) |
146 return result | 139 return result |
147 new_func.__doc__ = '%s\n%s' % (func.__doc__, optionParser.format_help()) | 140 new_func.__doc__ = '%s\n%s' % (func.__doc__, optionParser.format_help()) |
148 return new_func | 141 return new_func |
149 return option_setup | 142 return option_setup |
222 class ParsedString(str): | 215 class ParsedString(str): |
223 def full_parsed_statement(self): | 216 def full_parsed_statement(self): |
224 new = ParsedString('%s %s' % (self.parsed.command, self.parsed.args)) | 217 new = ParsedString('%s %s' % (self.parsed.command, self.parsed.args)) |
225 new.parsed = self.parsed | 218 new.parsed = self.parsed |
226 new.parser = self.parser | 219 new.parser = self.parser |
227 return new | 220 return new |
221 def with_args_replaced(self, newargs): | |
222 new = ParsedString(newargs) | |
223 new.parsed = self.parsed | |
224 new.parser = self.parser | |
225 new.parsed['args'] = newargs | |
226 new.parsed.statement['args'] = newargs | |
227 return new | |
228 | 228 |
229 class SkipToLast(pyparsing.SkipTo): | 229 class SkipToLast(pyparsing.SkipTo): |
230 def parseImpl( self, instring, loc, doActions=True ): | 230 def parseImpl( self, instring, loc, doActions=True ): |
231 resultStore = [] | 231 resultStore = [] |
232 startLoc = loc | 232 startLoc = loc |