22# -*- coding: utf-8 -*-
33"""Statement parsing classes for cmd2"""
44
5+ import os
56import re
67import shlex
78from 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