47
47
from . import utils
48
48
from .argparse_completer import AutoCompleter , ACArgumentParser , ACTION_ARG_CHOICES
49
49
from .clipboard import can_clip , get_paste_buffer , write_to_paste_buffer
50
- from .parsing import StatementParser , Statement , Macro , MacroArg
50
+ from .parsing import StatementParser , Statement , Macro , MacroArg , shlex_split , get_command_arg_list
51
51
from .history import History , HistoryItem
52
52
53
53
# Set up readline
@@ -157,34 +157,6 @@ def cat_decorator(func):
157
157
return cat_decorator
158
158
159
159
160
- def _get_command_arg_list (to_parse : Union [Statement , str ], preserve_quotes : bool ) -> List [str ]:
161
- """
162
- Called by the argument_list and argparse wrappers to retrieve just the arguments being
163
- passed to their do_* methods as a list.
164
-
165
- :param to_parse: what is being passed to the do_* method. It can be one of two types:
166
- 1. An already parsed Statement
167
- 2. An argument string in cases where a do_* method is explicitly called
168
- e.g.: Calling do_help('alias create') would cause to_parse to be 'alias create'
169
-
170
- :param preserve_quotes: if True, then quotes will not be stripped from the arguments
171
- :return: the arguments in a list
172
- """
173
- if isinstance (to_parse , Statement ):
174
- # In the case of a Statement, we already have what we need
175
- if preserve_quotes :
176
- return to_parse .arg_list
177
- else :
178
- return to_parse .argv [1 :]
179
- else :
180
- # We only have the argument string. Use the parser to split this string.
181
- parsed_arglist = StatementParser .shlex_split (to_parse )
182
- if not preserve_quotes :
183
- parsed_arglist = [utils .strip_quotes (arg ) for arg in parsed_arglist ]
184
-
185
- return parsed_arglist
186
-
187
-
188
160
def with_argument_list (* args : List [Callable ], preserve_quotes : bool = False ) -> Callable [[List ], Optional [bool ]]:
189
161
"""A decorator to alter the arguments passed to a do_* cmd2 method. Default passes a string of whatever the user
190
162
typed. With this decorator, the decorated method will receive a list of arguments parsed from user input.
@@ -198,7 +170,7 @@ def with_argument_list(*args: List[Callable], preserve_quotes: bool = False) ->
198
170
def arg_decorator (func : Callable ):
199
171
@functools .wraps (func )
200
172
def cmd_wrapper (cmd2_instance , statement : Union [Statement , str ]):
201
- parsed_arglist = _get_command_arg_list (statement , preserve_quotes )
173
+ parsed_arglist = get_command_arg_list (statement , preserve_quotes )
202
174
return func (cmd2_instance , parsed_arglist )
203
175
204
176
cmd_wrapper .__doc__ = func .__doc__
@@ -225,7 +197,7 @@ def with_argparser_and_unknown_args(argparser: argparse.ArgumentParser, preserve
225
197
def arg_decorator (func : Callable ):
226
198
@functools .wraps (func )
227
199
def cmd_wrapper (cmd2_instance , statement : Union [Statement , str ]):
228
- parsed_arglist = _get_command_arg_list (statement , preserve_quotes )
200
+ parsed_arglist = get_command_arg_list (statement , preserve_quotes )
229
201
230
202
try :
231
203
args , unknown = argparser .parse_known_args (parsed_arglist )
@@ -269,7 +241,7 @@ def arg_decorator(func: Callable):
269
241
@functools .wraps (func )
270
242
def cmd_wrapper (cmd2_instance , statement : Union [Statement , str ]):
271
243
272
- parsed_arglist = _get_command_arg_list (statement , preserve_quotes )
244
+ parsed_arglist = get_command_arg_list (statement , preserve_quotes )
273
245
274
246
try :
275
247
args = argparser .parse_args (parsed_arglist )
@@ -753,7 +725,7 @@ def tokens_for_completion(self, line: str, begidx: int, endidx: int) -> Tuple[Li
753
725
# Parse the line into tokens
754
726
while True :
755
727
try :
756
- initial_tokens = StatementParser . shlex_split (tmp_line [:tmp_endidx ])
728
+ initial_tokens = shlex_split (tmp_line [:tmp_endidx ])
757
729
758
730
# If the cursor is at an empty token outside of a quoted string,
759
731
# then that is the token being completed. Add it to the list.
0 commit comments