Mercurial > python-cmd2
changeset 301:30af90fd46c5
refactor mostly done?
author | catherine@dellzilla |
---|---|
date | Tue, 26 Jan 2010 18:04:20 -0500 |
parents | 1e4773b325d1 |
children | b4b22dd087dc |
files | cmd2.py example/example.py example/exampleSession.txt |
diffstat | 3 files changed, 16 insertions(+), 15 deletions(-) [+] |
line wrap: on
line diff
--- 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 ):
--- 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
--- 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