2
2
# -*- coding: utf-8 -*-
3
3
"""Statement parsing classes for cmd2"""
4
4
5
+ import os
5
6
import re
6
7
import shlex
7
8
from typing import List , Tuple
@@ -38,7 +39,7 @@ class Statement(str):
38
39
from the elements of the list, and aliases and shortcuts
39
40
are expanded
40
41
: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
42
43
there was one
43
44
:type terminator: str or None
44
45
:var suffix: characters appearing after the terminator but before output
@@ -49,7 +50,7 @@ class Statement(str):
49
50
:type pipe_to: list
50
51
:var output: if output was redirected, the redirection token, i.e. '>>'
51
52
: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
53
54
:type output_to: str or None
54
55
55
56
"""
@@ -297,6 +298,11 @@ def parse(self, rawinput: str) -> Statement:
297
298
pipe_pos = tokens .index (constants .REDIRECTION_PIPE )
298
299
# save everything after the first pipe as tokens
299
300
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
+
300
306
# remove all the tokens after the pipe
301
307
tokens = tokens [:pipe_pos ]
302
308
except ValueError :
@@ -309,7 +315,12 @@ def parse(self, rawinput: str) -> Statement:
309
315
try :
310
316
output_pos = tokens .index (constants .REDIRECTION_OUTPUT )
311
317
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
+
313
324
# remove all the tokens after the output redirect
314
325
tokens = tokens [:output_pos ]
315
326
except ValueError :
@@ -318,7 +329,12 @@ def parse(self, rawinput: str) -> Statement:
318
329
try :
319
330
output_pos = tokens .index (constants .REDIRECTION_APPEND )
320
331
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
+
322
338
# remove all tokens after the output redirect
323
339
tokens = tokens [:output_pos ]
324
340
except ValueError :
0 commit comments