Skip to content

Commit 6005458

Browse files
authored
Merge pull request #328 from python-cmd2/redirect_quoted
Allow quoted file paths when redirecting with < and >
2 parents e056174 + a5054a0 commit 6005458

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

cmd2.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,9 @@ def replace_with_file_contents(fname):
748748
:return: str - contents of file "fname"
749749
"""
750750
try:
751-
with open(os.path.expanduser(fname[0])) as source_file:
751+
# Any outer quotes are not part of the filename
752+
unquoted_file = strip_quotes(fname[0])
753+
with open(os.path.expanduser(unquoted_file)) as source_file:
752754
result = source_file.read()
753755
except IOError:
754756
result = '< %s' % fname[0] # wasn't a file after all
@@ -2936,8 +2938,8 @@ def _build_main_parser(self, redirector, terminators, multilineCommands, legalCh
29362938
pyparsing.Optional(pipe + pyparsing.SkipTo(output_destination_parser ^ string_end,
29372939
ignore=do_not_parse)('pipeTo')) + \
29382940
pyparsing.Optional(output_destination_parser +
2939-
pyparsing.SkipTo(string_end,
2940-
ignore=do_not_parse).setParseAction(lambda x: x[0].strip())('outputTo'))
2941+
pyparsing.SkipTo(string_end, ignore=do_not_parse).
2942+
setParseAction(lambda x: strip_quotes(x[0].strip()))('outputTo'))
29412943

29422944
multilineCommand.setParseAction(lambda x: x[0])
29432945
oneline_command.setParseAction(lambda x: x[0])
@@ -2984,7 +2986,10 @@ def _build_input_source_parser(legalChars, commentInProgress):
29842986

29852987
input_mark = pyparsing.Literal('<')
29862988
input_mark.setParseAction(lambda x: '')
2987-
file_name = pyparsing.Word(legalChars + '/\\')
2989+
2990+
# Also allow spaces, slashes, and quotes
2991+
file_name = pyparsing.Word(legalChars + ' /\\"\'')
2992+
29882993
input_from = file_name('inputFrom')
29892994
input_from.setParseAction(replace_with_file_contents)
29902995
# a not-entirely-satisfactory way of distinguishing < as in "import from" from <

0 commit comments

Comments
 (0)