35
35
import inspect
36
36
import os
37
37
import re
38
- import shlex
39
38
import sys
40
39
import threading
41
40
from typing import Any , Callable , Dict , List , Mapping , Optional , Tuple , Type , Union , IO
48
47
from . import utils
49
48
from .argparse_completer import AutoCompleter , ACArgumentParser , ACTION_ARG_CHOICES
50
49
from .clipboard import can_clip , get_paste_buffer , write_to_paste_buffer
51
- from .parsing import StatementParser , Statement , Macro , MacroArg
52
50
from .history import History , HistoryItem
51
+ from .parsing import StatementParser , Statement , Macro , MacroArg , shlex_split , get_command_arg_list
53
52
54
53
# Set up readline
55
54
from .rl_utils import rl_type , RlType , rl_get_point , rl_set_prompt , vt100_support , rl_make_safe_prompt
@@ -150,24 +149,6 @@ def categorize(func: Union[Callable, Iterable], category: str) -> None:
150
149
setattr (func , HELP_CATEGORY , category )
151
150
152
151
153
- def parse_quoted_string (string : str , preserve_quotes : bool ) -> List [str ]:
154
- """
155
- Parse a quoted string into a list of arguments
156
- :param string: the string being parsed
157
- :param preserve_quotes: if True, then quotes will not be stripped
158
- """
159
- if isinstance (string , list ):
160
- # arguments are already a list, return the list we were passed
161
- lexed_arglist = string
162
- else :
163
- # Use shlex to split the command line into a list of arguments based on shell rules
164
- lexed_arglist = shlex .split (string , comments = False , posix = False )
165
-
166
- if not preserve_quotes :
167
- lexed_arglist = [utils .strip_quotes (arg ) for arg in lexed_arglist ]
168
- return lexed_arglist
169
-
170
-
171
152
def with_category (category : str ) -> Callable :
172
153
"""A decorator to apply a category to a command function."""
173
154
def cat_decorator (func ):
@@ -178,8 +159,7 @@ def cat_decorator(func):
178
159
179
160
def with_argument_list (* args : List [Callable ], preserve_quotes : bool = False ) -> Callable [[List ], Optional [bool ]]:
180
161
"""A decorator to alter the arguments passed to a do_* cmd2 method. Default passes a string of whatever the user
181
- typed. With this decorator, the decorated method will receive a list of arguments parsed from user input using
182
- shlex.split().
162
+ typed. With this decorator, the decorated method will receive a list of arguments parsed from user input.
183
163
184
164
:param args: Single-element positional argument list containing do_* method this decorator is wrapping
185
165
:param preserve_quotes: if True, then argument quotes will not be stripped
@@ -189,9 +169,9 @@ def with_argument_list(*args: List[Callable], preserve_quotes: bool = False) ->
189
169
190
170
def arg_decorator (func : Callable ):
191
171
@functools .wraps (func )
192
- def cmd_wrapper (self , cmdline ):
193
- lexed_arglist = parse_quoted_string ( cmdline , preserve_quotes )
194
- return func (self , lexed_arglist )
172
+ def cmd_wrapper (cmd2_instance , statement : Union [ Statement , str ] ):
173
+ parsed_arglist = get_command_arg_list ( statement , preserve_quotes )
174
+ return func (cmd2_instance , parsed_arglist )
195
175
196
176
cmd_wrapper .__doc__ = func .__doc__
197
177
return cmd_wrapper
@@ -214,16 +194,17 @@ def with_argparser_and_unknown_args(argparser: argparse.ArgumentParser, preserve
214
194
import functools
215
195
216
196
# noinspection PyProtectedMember
217
- def arg_decorator (func : Callable [[ Statement ], Optional [ bool ]] ):
197
+ def arg_decorator (func : Callable ):
218
198
@functools .wraps (func )
219
- def cmd_wrapper (instance , cmdline ):
220
- lexed_arglist = parse_quoted_string (cmdline , preserve_quotes )
199
+ def cmd_wrapper (cmd2_instance , statement : Union [Statement , str ]):
200
+ parsed_arglist = get_command_arg_list (statement , preserve_quotes )
201
+
221
202
try :
222
- args , unknown = argparser .parse_known_args (lexed_arglist )
203
+ args , unknown = argparser .parse_known_args (parsed_arglist )
223
204
except SystemExit :
224
205
return
225
206
else :
226
- return func (instance , args , unknown )
207
+ return func (cmd2_instance , args , unknown )
227
208
228
209
# argparser defaults the program name to sys.argv[0]
229
210
# we want it to be the name of our command
@@ -256,16 +237,18 @@ def with_argparser(argparser: argparse.ArgumentParser,
256
237
import functools
257
238
258
239
# noinspection PyProtectedMember
259
- def arg_decorator (func : Callable [[ Statement ], Optional [ bool ]] ):
240
+ def arg_decorator (func : Callable ):
260
241
@functools .wraps (func )
261
- def cmd_wrapper (instance , cmdline ):
262
- lexed_arglist = parse_quoted_string (cmdline , preserve_quotes )
242
+ def cmd_wrapper (cmd2_instance , statement : Union [Statement , str ]):
243
+
244
+ parsed_arglist = get_command_arg_list (statement , preserve_quotes )
245
+
263
246
try :
264
- args = argparser .parse_args (lexed_arglist )
247
+ args = argparser .parse_args (parsed_arglist )
265
248
except SystemExit :
266
249
return
267
250
else :
268
- return func (instance , args )
251
+ return func (cmd2_instance , args )
269
252
270
253
# argparser defaults the program name to sys.argv[0]
271
254
# we want it to be the name of our command
@@ -742,8 +725,7 @@ def tokens_for_completion(self, line: str, begidx: int, endidx: int) -> Tuple[Li
742
725
# Parse the line into tokens
743
726
while True :
744
727
try :
745
- # Use non-POSIX parsing to keep the quotes around the tokens
746
- initial_tokens = shlex .split (tmp_line [:tmp_endidx ], comments = False , posix = False )
728
+ initial_tokens = shlex_split (tmp_line [:tmp_endidx ])
747
729
748
730
# If the cursor is at an empty token outside of a quoted string,
749
731
# then that is the token being completed. Add it to the list.
@@ -1735,7 +1717,7 @@ def _run_cmdfinalization_hooks(self, stop: bool, statement: Optional[Statement])
1735
1717
# Fix those annoying problems that occur with terminal programs like "less" when you pipe to them
1736
1718
if self .stdin .isatty ():
1737
1719
import subprocess
1738
- proc = subprocess .Popen (shlex . split ( 'stty sane' ) )
1720
+ proc = subprocess .Popen ([ 'stty' , ' sane'] )
1739
1721
proc .communicate ()
1740
1722
1741
1723
try :
0 commit comments