diff options
Diffstat (limited to 'cmd2/parsing.py')
-rw-r--r-- | cmd2/parsing.py | 34 |
1 files changed, 18 insertions, 16 deletions
diff --git a/cmd2/parsing.py b/cmd2/parsing.py index dbfabc80..dfa248e9 100644 --- a/cmd2/parsing.py +++ b/cmd2/parsing.py @@ -359,15 +359,13 @@ class StatementParser: errmsg = '' return valid, errmsg - def tokenize(self, line: str, expand: bool = True) -> List[str]: + def tokenize(self, line: str, *, expand: bool = True) -> List[str]: """ Lex a string into a list of tokens. Shortcuts and aliases are expanded and comments are removed :param line: the command line being lexed :param expand: If True, then aliases and shortcuts will be expanded. - Set this to False if no expansion should occur because the command name is already known. - Otherwise the command could be expanded if it matched an alias name. This is for cases where - a do_* method was called manually (e.g do_help('alias'). + Set this to False if the command token should not be altered. Defaults to True. :return: A list of tokens :raises ValueError if there are unclosed quotation marks. """ @@ -387,7 +385,7 @@ class StatementParser: tokens = self._split_on_punctuation(tokens) return tokens - def parse(self, line: str, expand: bool = True) -> Statement: + def parse(self, line: str, *, expand: bool = True) -> Statement: """ Tokenize the input and parse it into a Statement object, stripping comments, expanding aliases and shortcuts, and extracting output @@ -395,10 +393,8 @@ class StatementParser: :param line: the command line being parsed :param expand: If True, then aliases and shortcuts will be expanded. - Set this to False if no expansion should occur because the command name is already known. - Otherwise the command could be expanded if it matched an alias name. This is for cases where - a do_* method was called manually (e.g do_help('alias'). - :return: A parsed Statement + Set this to False if the command token should not be altered. Defaults to True. + :return: the created Statement :raises ValueError if there are unclosed quotation marks """ @@ -414,7 +410,7 @@ class StatementParser: arg_list = [] # lex the input into a list of tokens - tokens = self.tokenize(line, expand) + tokens = self.tokenize(line, expand=expand) # of the valid terminators, find the first one to occur in the input terminator_pos = len(tokens) + 1 @@ -533,11 +529,10 @@ class StatementParser: suffix=suffix, pipe_to=pipe_to, output=output, - output_to=output_to, - ) + output_to=output_to) return statement - def parse_command_only(self, rawinput: str) -> Statement: + def parse_command_only(self, rawinput: str, *, expand: bool = True) -> Statement: """Partially parse input into a Statement object. The command is identified, and shortcuts and aliases are expanded. @@ -560,9 +555,17 @@ class StatementParser: Different from parse(), this method does not remove redundant whitespace within args. However, it does ensure args has no leading or trailing whitespace. + + :param rawinput: the command line as entered by the user + :param expand: If True, then aliases and shortcuts will be expanded. + Set this to False if the command token should not be altered. Defaults to True. + :return: the created Statement """ + line = rawinput + # expand shortcuts and aliases - line = self._expand(rawinput) + if expand: + line = self._expand(rawinput) command = '' args = '' @@ -591,8 +594,7 @@ class StatementParser: statement = Statement(args, raw=rawinput, command=command, - multiline_command=multiline_command, - ) + multiline_command=multiline_command) return statement def get_command_arg_list(self, command_name: str, to_parse: Union[Statement, str], |