@@ -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
@@ -382,16 +354,22 @@ def is_valid_command(self, word: str) -> Tuple[bool, str]:
382354 errmsg = ''
383355 return valid , errmsg
384356
385- def tokenize (self , line : str ) -> List [str ]:
386- """Lex a string into a list of tokens.
387-
388- shortcuts and aliases are expanded and comments are removed
389-
390- Raises ValueError if there are unclosed quotation marks.
357+ def tokenize (self , line : str , expand : bool = True ) -> List [str ]:
358+ """
359+ Lex a string into a list of tokens. Shortcuts and aliases are expanded and comments are removed
360+
361+ :param line: the command line being lexed
362+ :param expand: If True, then aliases and shortcuts will be expanded.
363+ Set this to False if no expansion should occur because the command name is already known.
364+ Otherwise the command could be expanded if it matched an alias name. This is for cases where
365+ a do_* method was called manually (e.g do_help('alias').
366+ :return: A list of tokens
367+ :raises ValueError if there are unclosed quotation marks.
391368 """
392369
393370 # expand shortcuts and aliases
394- line = self ._expand (line )
371+ if expand :
372+ line = self ._expand (line )
395373
396374 # check if this line is a comment
397375 if line .strip ().startswith (constants .COMMENT_CHAR ):
@@ -404,12 +382,19 @@ def tokenize(self, line: str) -> List[str]:
404382 tokens = self ._split_on_punctuation (tokens )
405383 return tokens
406384
407- def parse (self , line : str ) -> Statement :
408- """Tokenize the input and parse it into a Statement object, stripping
385+ def parse (self , line : str , expand : bool = True ) -> Statement :
386+ """
387+ Tokenize the input and parse it into a Statement object, stripping
409388 comments, expanding aliases and shortcuts, and extracting output
410389 redirection directives.
411390
412- Raises ValueError if there are unclosed quotation marks.
391+ :param line: the command line being parsed
392+ :param expand: If True, then aliases and shortcuts will be expanded.
393+ Set this to False if no expansion should occur because the command name is already known.
394+ Otherwise the command could be expanded if it matched an alias name. This is for cases where
395+ a do_* method was called manually (e.g do_help('alias').
396+ :return: A parsed Statement
397+ :raises ValueError if there are unclosed quotation marks
413398 """
414399
415400 # handle the special case/hardcoded terminator of a blank line
@@ -424,7 +409,7 @@ def parse(self, line: str) -> Statement:
424409 arg_list = []
425410
426411 # lex the input into a list of tokens
427- tokens = self .tokenize (line )
412+ tokens = self .tokenize (line , expand )
428413
429414 # of the valid terminators, find the first one to occur in the input
430415 terminator_pos = len (tokens ) + 1
@@ -605,6 +590,35 @@ def parse_command_only(self, rawinput: str) -> Statement:
605590 )
606591 return statement
607592
593+ def get_command_arg_list (self , command_name : str , to_parse : Union [Statement , str ],
594+ preserve_quotes : bool ) -> Tuple [Statement , List [str ]]:
595+ """
596+ Called by the argument_list and argparse wrappers to retrieve just the arguments being
597+ passed to their do_* methods as a list.
598+
599+ :param command_name: name of the command being run
600+ :param to_parse: what is being passed to the do_* method. It can be one of two types:
601+ 1. An already parsed Statement
602+ 2. An argument string in cases where a do_* method is explicitly called
603+ e.g.: Calling do_help('alias create') would cause to_parse to be 'alias create'
604+
605+ In this case, the string will be converted to a Statement and returned along
606+ with the argument list.
607+
608+ :param preserve_quotes: if True, then quotes will not be stripped from the arguments
609+ :return: A tuple containing:
610+ The Statement used to retrieve the arguments
611+ The argument list
612+ """
613+ # Check if to_parse needs to be converted to a Statement
614+ if not isinstance (to_parse , Statement ):
615+ to_parse = self .parse (command_name + ' ' + to_parse , expand = False )
616+
617+ if preserve_quotes :
618+ return to_parse , to_parse .arg_list
619+ else :
620+ return to_parse , to_parse .argv [1 :]
621+
608622 def _expand (self , line : str ) -> str :
609623 """Expand shortcuts and aliases"""
610624
0 commit comments