summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCatherine Devlin <catherine.devlin@gmail.com>2009-01-28 12:37:02 -0500
committerCatherine Devlin <catherine.devlin@gmail.com>2009-01-28 12:37:02 -0500
commit2cea2dfa6dd2e5de61ebbe390b69ca9e2e62a6d1 (patch)
tree5af6536cf293bc8a6bc94bdd586eafe56ff3d497
parent3dd87ecf0d462fcf1e203814c04bd0dae2521234 (diff)
downloadcmd2-git-0.4.5.tar.gz
newline terminators ok0.4.5
-rwxr-xr-xcmd2.py21
-rwxr-xr-xexample/exampleSession.txt1
2 files changed, 19 insertions, 3 deletions
diff --git a/cmd2.py b/cmd2.py
index cab27024..8033b263 100755
--- a/cmd2.py
+++ b/cmd2.py
@@ -43,6 +43,15 @@ class OptionParser(optparse.OptionParser):
"""
raise
+def remainingArgs(oldArgs, newArgList):
+ '''
+ >>> remainingArgs('-f bar bar cow', ['bar', 'cow'])
+ 'bar cow'
+ '''
+ pattern = '\s+'.join(newArgList) + '\s*$'
+ matchObj = re.search(pattern, oldArgs)
+ return oldArgs[matchObj.start():]
+
def options(option_list):
def option_setup(func):
optionParser = OptionParser()
@@ -51,8 +60,8 @@ def options(option_list):
optionParser.set_usage("%s [options] arg" % func.__name__.strip('do_'))
def newFunc(instance, arg):
try:
- opts, newArgs = optionParser.parse_args(arg.split())
- newArgs = (newArgs and arg[arg.find(newArgs[0]):]) or ''
+ opts, newArgList = optionParser.parse_args(arg.split())
+ newArgs = remainingArgs(arg, newArgList)
except (optparse.OptionValueError, optparse.BadOptionError,
optparse.OptionError, optparse.AmbiguousOptionError,
optparse.OptionConflictError), e:
@@ -61,7 +70,13 @@ def options(option_list):
return
if hasattr(opts, '_exit'):
return None
- arg = arg.parser('%s %s%s%s' % (arg.parsed.command, newArgs, arg.parsed.terminator, arg.parsed.suffix))
+ 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))
result = func(instance, arg, opts)
return result
newFunc.__doc__ = '%s\n%s' % (func.__doc__, optionParser.format_help())
diff --git a/example/exampleSession.txt b/example/exampleSession.txt
index 6e3c2b3f..d023f726 100755
--- a/example/exampleSession.txt
+++ b/example/exampleSession.txt
@@ -1,3 +1,4 @@
+(Cmd) say put this in a file > myfile.txt
(Cmd) say < myfile.txt
put this in a file
(Cmd) # comments do nothing