Skip to content

Commit dbb82ee

Browse files
committed
Allowing quoted paths when piping and redirecting
1 parent dbeef11 commit dbb82ee

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

cmd2/cmd2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1840,7 +1840,7 @@ def _redirect_output(self, statement):
18401840
# REDIRECTION_APPEND or REDIRECTION_OUTPUT
18411841
if statement.output == constants.REDIRECTION_APPEND:
18421842
mode = 'a'
1843-
sys.stdout = self.stdout = open(os.path.expanduser(shlex.split(statement.output_to)[0]), mode)
1843+
sys.stdout = self.stdout = open(statement.output_to, mode)
18441844
else:
18451845
# going to a paste buffer
18461846
sys.stdout = self.stdout = tempfile.TemporaryFile(mode="w+")

cmd2/parsing.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# -*- coding: utf-8 -*-
33
"""Statement parsing classes for cmd2"""
44

5+
import os
56
import re
67
import shlex
78
from typing import List, Tuple
@@ -38,7 +39,7 @@ class Statement(str):
3839
from the elements of the list, and aliases and shortcuts
3940
are expanded
4041
:type argv: list
41-
:var terminator: the charater which terminated the multiline command, if
42+
:var terminator: the character which terminated the multiline command, if
4243
there was one
4344
:type terminator: str or None
4445
:var suffix: characters appearing after the terminator but before output
@@ -49,7 +50,7 @@ class Statement(str):
4950
:type pipe_to: list
5051
:var output: if output was redirected, the redirection token, i.e. '>>'
5152
:type output: str or None
52-
:var output_to: if output was redirected, the destination, usually a filename
53+
:var output_to: if output was redirected, the destination file
5354
:type output_to: str or None
5455
5556
"""
@@ -297,6 +298,11 @@ def parse(self, rawinput: str) -> Statement:
297298
pipe_pos = tokens.index(constants.REDIRECTION_PIPE)
298299
# save everything after the first pipe as tokens
299300
pipe_to = tokens[pipe_pos+1:]
301+
302+
for pos, cur_token in enumerate(pipe_to):
303+
unquoted_token = utils.strip_quotes(cur_token)
304+
pipe_to[pos] = os.path.expanduser(unquoted_token)
305+
300306
# remove all the tokens after the pipe
301307
tokens = tokens[:pipe_pos]
302308
except ValueError:
@@ -309,7 +315,12 @@ def parse(self, rawinput: str) -> Statement:
309315
try:
310316
output_pos = tokens.index(constants.REDIRECTION_OUTPUT)
311317
output = constants.REDIRECTION_OUTPUT
312-
output_to = ' '.join(tokens[output_pos+1:])
318+
319+
# Check if we are redirecting to a file
320+
if len(tokens) > output_pos + 1:
321+
unquoted_path = utils.strip_quotes(tokens[output_pos + 1])
322+
output_to = os.path.expanduser(unquoted_path)
323+
313324
# remove all the tokens after the output redirect
314325
tokens = tokens[:output_pos]
315326
except ValueError:
@@ -318,7 +329,12 @@ def parse(self, rawinput: str) -> Statement:
318329
try:
319330
output_pos = tokens.index(constants.REDIRECTION_APPEND)
320331
output = constants.REDIRECTION_APPEND
321-
output_to = ' '.join(tokens[output_pos+1:])
332+
333+
# Check if we are redirecting to a file
334+
if len(tokens) > output_pos + 1:
335+
unquoted_path = utils.strip_quotes(tokens[output_pos + 1])
336+
output_to = os.path.expanduser(unquoted_path)
337+
322338
# remove all tokens after the output redirect
323339
tokens = tokens[:output_pos]
324340
except ValueError:

0 commit comments

Comments
 (0)