# HG changeset patch # User catherine@dellzilla # Date 1264547060 18000 # Node ID 30af90fd46c5f2b7ef771fabd53ea43782ccd726 # Parent 1e4773b325d1313721b04396bc03265265bdc6d7 refactor mostly done? diff -r 1e4773b325d1 -r 30af90fd46c5 cmd2.py --- a/cmd2.py Tue Jan 26 17:39:29 2010 -0500 +++ b/cmd2.py Tue Jan 26 18:04:20 2010 -0500 @@ -115,6 +115,7 @@ if hasattr(arg, 'parsed'): args = arg.parsed.raw else: + print 'raw arg passed to new_func!' args = arg opts, newArgList = optionParser.parse_args(args.split()) # Must find the remaining args in the original argument list, but @@ -122,6 +123,10 @@ if hasattr(arg, 'parsed') and newArgList[0] == arg.parsed.command: newArgList = newArgList[1:] newArgs = remaining_args(args, newArgList) + if isinstance(arg, ParsedString): + arg = arg.with_args_replaced(newArgs) + else: + arg = newArgs except (optparse.OptionValueError, optparse.BadOptionError, optparse.OptionError, optparse.AmbiguousOptionError, optparse.OptionConflictError), e: @@ -130,18 +135,6 @@ return if hasattr(opts, '_exit'): return None - if hasattr(arg, 'parsed'): - arg.parsed.args = newArgs - terminator = arg.parsed.terminator - try: - if arg.parsed.terminator[0] == '\n': - terminator = arg.parsed.terminator[0] - except IndexError: - pass - arg = arg.parser('%s %s%s%s' % (arg.parsed.command, newArgs, - terminator, arg.parsed.suffix)) - else: - arg = newArgs result = func(instance, arg, opts) return result new_func.__doc__ = '%s\n%s' % (func.__doc__, optionParser.format_help()) @@ -224,7 +217,14 @@ new = ParsedString('%s %s' % (self.parsed.command, self.parsed.args)) new.parsed = self.parsed new.parser = self.parser - return new + 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 class SkipToLast(pyparsing.SkipTo): def parseImpl( self, instring, loc, doActions=True ): diff -r 1e4773b325d1 -r 30af90fd46c5 example/example.py --- a/example/example.py Tue Jan 26 17:39:29 2010 -0500 +++ b/example/example.py Tue Jan 26 18:04:20 2010 -0500 @@ -17,7 +17,7 @@ """Repeats what you tell me to.""" arg = ''.join(arg) if opts.piglatin: - arg = '%s%say' % (arg[1:], arg[0]) + arg = '%s%say' % (arg[1:].rstrip(), arg[0]) if opts.shout: arg = arg.upper() repetitions = opts.repeat or 1 diff -r 1e4773b325d1 -r 30af90fd46c5 example/exampleSession.txt --- a/example/exampleSession.txt Tue Jan 26 17:39:29 2010 -0500 +++ b/example/exampleSession.txt Tue Jan 26 18:04:20 2010 -0500 @@ -31,14 +31,15 @@ abbrev: True case_insensitive: True continuation_prompt: > +debug: True default_file_name: command.txt echo: False editor: gedit feedback_to_output: False +maxrepeats: 3 prompt: (Cmd) quiet: False timing: False -maxrepeats: 3 (Cmd) set maxrepeats 5 maxrepeats - was: 3 now: 5