@@ -236,34 +236,6 @@ def argv(self) -> List[str]:
236236 return rtn
237237
238238
239- def get_command_arg_list (to_parse : Union [Statement , str ], preserve_quotes : bool ) -> List [str ]:
240- """
241- Called by the argument_list and argparse wrappers to retrieve just the arguments being
242- passed to their do_* methods as a list.
243-
244- :param to_parse: what is being passed to the do_* method. It can be one of two types:
245- 1. An already parsed Statement
246- 2. An argument string in cases where a do_* method is explicitly called
247- e.g.: Calling do_help('alias create') would cause to_parse to be 'alias create'
248-
249- :param preserve_quotes: if True, then quotes will not be stripped from the arguments
250- :return: the arguments in a list
251- """
252- if isinstance (to_parse , Statement ):
253- # In the case of a Statement, we already have what we need
254- if preserve_quotes :
255- return to_parse .arg_list
256- else :
257- return to_parse .argv [1 :]
258- else :
259- # We have the arguments in a string. Use shlex to split it.
260- parsed_arglist = shlex_split (to_parse )
261- if not preserve_quotes :
262- parsed_arglist = [utils .strip_quotes (arg ) for arg in parsed_arglist ]
263-
264- return parsed_arglist
265-
266-
267239class StatementParser :
268240 """Parse raw text into command components.
269241
@@ -371,16 +343,22 @@ def is_valid_command(self, word: str) -> Tuple[bool, str]:
371343 errmsg = ''
372344 return valid , errmsg
373345
374- def tokenize (self , line : str ) -> List [str ]:
375- """Lex a string into a list of tokens.
376-
377- shortcuts and aliases are expanded and comments are removed
378-
379- Raises ValueError if there are unclosed quotation marks.
346+ def tokenize (self , line : str , expand : bool = True ) -> List [str ]:
347+ """
348+ Lex a string into a list of tokens. Shortcuts and aliases are expanded and comments are removed
349+
350+ :param line: the command line being lexed
351+ :param expand: If True, then aliases and shortcuts will be expanded.
352+ Set this to False if no expansion should occur because the command name is already known.
353+ Otherwise the command could be expanded if it matched an alias name. This is for cases where
354+ a do_* method was called manually (e.g do_help('alias').
355+ :return: A list of tokens
356+ :raises ValueError if there are unclosed quotation marks.
380357 """
381358
382359 # expand shortcuts and aliases
383- line = self ._expand (line )
360+ if expand :
361+ line = self ._expand (line )
384362
385363 # check if this line is a comment
386364 if line .strip ().startswith (constants .COMMENT_CHAR ):
@@ -393,12 +371,19 @@ def tokenize(self, line: str) -> List[str]:
393371 tokens = self ._split_on_punctuation (tokens )
394372 return tokens
395373
396- def parse (self , line : str ) -> Statement :
397- """Tokenize the input and parse it into a Statement object, stripping
374+ def parse (self , line : str , expand : bool = True ) -> Statement :
375+ """
376+ Tokenize the input and parse it into a Statement object, stripping
398377 comments, expanding aliases and shortcuts, and extracting output
399378 redirection directives.
400379
401- Raises ValueError if there are unclosed quotation marks.
380+ :param line: the command line being parsed
381+ :param expand: If True, then aliases and shortcuts will be expanded.
382+ Set this to False if no expansion should occur because the command name is already known.
383+ Otherwise the command could be expanded if it matched an alias name. This is for cases where
384+ a do_* method was called manually (e.g do_help('alias').
385+ :return: A parsed Statement
386+ :raises ValueError if there are unclosed quotation marks
402387 """
403388
404389 # handle the special case/hardcoded terminator of a blank line
@@ -413,7 +398,7 @@ def parse(self, line: str) -> Statement:
413398 arg_list = []
414399
415400 # lex the input into a list of tokens
416- tokens = self .tokenize (line )
401+ tokens = self .tokenize (line , expand )
417402
418403 # of the valid terminators, find the first one to occur in the input
419404 terminator_pos = len (tokens ) + 1
@@ -594,6 +579,35 @@ def parse_command_only(self, rawinput: str) -> Statement:
594579 )
595580 return statement
596581
582+ def get_command_arg_list (self , command_name : str , to_parse : Union [Statement , str ],
583+ preserve_quotes : bool ) -> Tuple [Statement , List [str ]]:
584+ """
585+ Called by the argument_list and argparse wrappers to retrieve just the arguments being
586+ passed to their do_* methods as a list.
587+
588+ :param command_name: name of the command being run
589+ :param to_parse: what is being passed to the do_* method. It can be one of two types:
590+ 1. An already parsed Statement
591+ 2. An argument string in cases where a do_* method is explicitly called
592+ e.g.: Calling do_help('alias create') would cause to_parse to be 'alias create'
593+
594+ In this case, the string will be converted to a Statement and returned along
595+ with the argument list.
596+
597+ :param preserve_quotes: if True, then quotes will not be stripped from the arguments
598+ :return: A tuple containing:
599+ The Statement used to retrieve the arguments
600+ The argument list
601+ """
602+ # Check if to_parse needs to be converted to a Statement
603+ if not isinstance (to_parse , Statement ):
604+ to_parse = self .parse (command_name + ' ' + to_parse , expand = False )
605+
606+ if preserve_quotes :
607+ return to_parse , to_parse .arg_list
608+ else :
609+ return to_parse , to_parse .argv [1 :]
610+
597611 def _expand (self , line : str ) -> str :
598612 """Expand shortcuts and aliases"""
599613
0 commit comments