summaryrefslogtreecommitdiff
path: root/cmd2/parsing.py
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2019-05-13 10:08:09 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2019-05-13 10:08:09 -0400
commit2c5417be5cc52f7c76097f85664adda2c976b1e4 (patch)
tree57f143a32eb3ce2c448b7d19b15c31fb0c48cc87 /cmd2/parsing.py
parent1dd2c0eae0a578deda8257a5592daf9aca2809ee (diff)
downloadcmd2-git-2c5417be5cc52f7c76097f85664adda2c976b1e4.tar.gz
Fixed parsing issue in case where output redirection (e.g. > file) appears before a pipe.
In that case, the pipe was given precedence even though it appeared later in the command.
Diffstat (limited to 'cmd2/parsing.py')
-rw-r--r--cmd2/parsing.py70
1 files changed, 34 insertions, 36 deletions
diff --git a/cmd2/parsing.py b/cmd2/parsing.py
index 306d3bdd..067c9bf4 100644
--- a/cmd2/parsing.py
+++ b/cmd2/parsing.py
@@ -453,56 +453,54 @@ class StatementParser:
arg_list = tokens[1:]
tokens = []
- # check for a pipe to a shell process
- # if there is a pipe, everything after the pipe needs to be passed
- # to the shell, even redirected output
- # this allows '(Cmd) say hello | wc > countit.txt'
+ pipe_to = ''
+ output = ''
+ output_to = ''
+
+ # Find which redirector character appears first in the command
+ try:
+ pipe_index = tokens.index(constants.REDIRECTION_PIPE)
+ except ValueError:
+ pipe_index = len(tokens)
+
try:
- # find the first pipe if it exists
- pipe_pos = tokens.index(constants.REDIRECTION_PIPE)
+ redir_index = tokens.index(constants.REDIRECTION_OUTPUT)
+ except ValueError:
+ redir_index = len(tokens)
+
+ try:
+ append_index = tokens.index(constants.REDIRECTION_APPEND)
+ except ValueError:
+ append_index = len(tokens)
+
+ # Check if output should be piped to a shell command
+ if pipe_index < redir_index and pipe_index < append_index:
# Get the tokens for the pipe command and expand ~ where needed
- pipe_to_tokens = tokens[pipe_pos + 1:]
+ pipe_to_tokens = tokens[pipe_index + 1:]
utils.expand_user_in_tokens(pipe_to_tokens)
# Build the pipe command line string
pipe_to = ' '.join(pipe_to_tokens)
# remove all the tokens after the pipe
- tokens = tokens[:pipe_pos]
- except ValueError:
- pipe_to = ''
+ tokens = tokens[:pipe_index]
- # check for output redirect
- output = ''
- output_to = ''
- try:
- output_pos = tokens.index(constants.REDIRECTION_OUTPUT)
- output = constants.REDIRECTION_OUTPUT
+ # Check for output redirect/append
+ elif redir_index != append_index:
+ if redir_index < append_index:
+ output = constants.REDIRECTION_OUTPUT
+ output_index = redir_index
+ else:
+ output = constants.REDIRECTION_APPEND
+ output_index = append_index
- # Check if we are redirecting to a file
- if len(tokens) > output_pos + 1:
- unquoted_path = utils.strip_quotes(tokens[output_pos + 1])
+ if len(tokens) > output_index + 1:
+ unquoted_path = utils.strip_quotes(tokens[output_index + 1])
output_to = os.path.expanduser(unquoted_path)
# remove all the tokens after the output redirect
- tokens = tokens[:output_pos]
- except ValueError:
- pass
-
- try:
- output_pos = tokens.index(constants.REDIRECTION_APPEND)
- output = constants.REDIRECTION_APPEND
-
- # Check if we are redirecting to a file
- if len(tokens) > output_pos + 1:
- unquoted_path = utils.strip_quotes(tokens[output_pos + 1])
- output_to = os.path.expanduser(unquoted_path)
-
- # remove all tokens after the output redirect
- tokens = tokens[:output_pos]
- except ValueError:
- pass
+ tokens = tokens[:output_index]
if terminator:
# whatever is left is the suffix