summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkotfu <kotfu@kotfu.net>2018-04-23 20:16:55 -0600
committerkotfu <kotfu@kotfu.net>2018-04-23 20:16:55 -0600
commitb3d71457e951d9d382787cb82fdf77f32951337c (patch)
tree7e5284d2a7895a678fa73df3f2e695000ef7fdcd
parent2534df2987a5aec226ce5bfd5b21f1d8f22ed3f2 (diff)
downloadcmd2-git-b3d71457e951d9d382787cb82fdf77f32951337c.tar.gz
Fix parsing of input redirection and appending output
-rw-r--r--cmd2/parsing.py15
-rw-r--r--tests/test_cmd2.py2
-rw-r--r--tests/test_shlexparsing.py18
3 files changed, 26 insertions, 9 deletions
diff --git a/cmd2/parsing.py b/cmd2/parsing.py
index c8110667..ec8e2e84 100644
--- a/cmd2/parsing.py
+++ b/cmd2/parsing.py
@@ -115,14 +115,16 @@ class CommandParser():
# check for input from file
inputFrom = None
try:
- if tokens[0] == '<':
- inputFrom = ' '.join(tokens[1:])
- tokens = []
- except IndexError:
+ input_pos = tokens.index('<')
+ inputFrom = ' '.join(tokens[input_pos+1:])
+ tokens = tokens[:input_pos]
+ except ValueError:
pass
# check for output redirect
+ output = None
+ outputTo = None
try:
output_pos = tokens.index('>')
output = '>'
@@ -130,13 +132,12 @@ class CommandParser():
# remove all the tokens after the output redirect
tokens = tokens[:output_pos]
except ValueError:
- output = None
- outputTo = None
+ pass
- # check for paste buffer
try:
output_pos = tokens.index('>>')
output = '>>'
+ outputTo = ' '.join(tokens[output_pos+1:])
# remove all tokens after the output redirect
tokens = tokens[:output_pos]
except ValueError:
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py
index 6075ca7b..f2ee16af 100644
--- a/tests/test_cmd2.py
+++ b/tests/test_cmd2.py
@@ -607,7 +607,7 @@ def test_input_redirection(base_app, request):
# NOTE: File 'redirect.txt" contains 1 word "history"
- # Verify that redirecting input ffom a file works
+ # Verify that redirecting input from a file works
out = run_cmd(base_app, 'help < {}'.format(filename))
assert out == normalize(HELP_HISTORY)
diff --git a/tests/test_shlexparsing.py b/tests/test_shlexparsing.py
index b8d4b208..b9caed7c 100644
--- a/tests/test_shlexparsing.py
+++ b/tests/test_shlexparsing.py
@@ -8,7 +8,8 @@ Todo List
- checkout Cmd2.parseline() function which parses and expands shortcuts and such
this code should probably be included in CommandParser
- get rid of legalChars
-
+- move remaining tests in test_parsing.py to test_cmd2.py
+- rename test_shlexparsing.py to test_parsing.py
Notes:
@@ -160,11 +161,26 @@ def test_output_redirect_with_dash_in_path(parser):
assert results.output == '>'
assert results.outputTo == 'python-cmd2/afile.txt'
+def test_output_redirect_append(parser):
+ line = 'output appended to >> /tmp/afile.txt'
+ results = parser.parseString(line)
+ assert results.command == 'output'
+ assert results.args == 'appended to'
+ assert results.output == '>>'
+ assert results.outputTo == '/tmp/afile.txt'
+
def test_parse_input_redirect(parser):
line = '< afile.txt'
results = parser.parseString(line)
assert results.inputFrom == 'afile.txt'
+def test_parse_input_redirect_after_command(parser):
+ line = 'help < afile.txt'
+ results = parser.parseString(line)
+ assert results.command == 'help'
+ assert results.args == ''
+ assert results.inputFrom == 'afile.txt'
+
def test_parse_input_redirect_with_dash_in_path(parser):
line = '< python-cmd2/afile.txt'
results = parser.parseString(line)