From b79cc6921d9e0c2adf9f4524979411bafdec7dc5 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Tue, 5 Mar 2019 00:51:42 -0500 Subject: Added a shlex.split() wrapper to have a common way of calling it. Replaced parse_quoted_string with _get_command_arg_list. --- cmd2/parsing.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'cmd2/parsing.py') diff --git a/cmd2/parsing.py b/cmd2/parsing.py index 5ec13fb7..86227b5b 100644 --- a/cmd2/parsing.py +++ b/cmd2/parsing.py @@ -349,7 +349,7 @@ class StatementParser: return [] # split on whitespace - tokens = shlex.split(line, comments=False, posix=False) + tokens = StatementParser.shlex_split(line) # custom lexing tokens = self._split_on_punctuation(tokens) @@ -607,6 +607,16 @@ class StatementParser: return command, args + @staticmethod + def shlex_split(str_to_split: str) -> List[str]: + """ + A wrapper around shlex.split() that uses cmd2's preferred arguments + This allows other classes to easily call split() the same way StatementParser does + :param str_to_split: the string being split + :return: A list of tokens + """ + return shlex.split(str_to_split, comments=False, posix=False) + def _split_on_punctuation(self, tokens: List[str]) -> List[str]: """Further splits tokens from a command line using punctuation characters -- cgit v1.2.1 From 3d52ee70ae4612b3a32ee3a37cf2324f7ddb0828 Mon Sep 17 00:00:00 2001 From: Todd Leonhardt Date: Tue, 5 Mar 2019 20:39:25 -0500 Subject: Moved some utility functions from cmd2.py to parsing.py --- cmd2/parsing.py | 52 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 40 insertions(+), 12 deletions(-) (limited to 'cmd2/parsing.py') diff --git a/cmd2/parsing.py b/cmd2/parsing.py index 86227b5b..9c95ba75 100644 --- a/cmd2/parsing.py +++ b/cmd2/parsing.py @@ -5,7 +5,7 @@ import os import re import shlex -from typing import List, Tuple, Dict +from typing import Dict, List, Tuple, Union import attr @@ -13,6 +13,16 @@ from . import constants from . import utils +def shlex_split(str_to_split: str) -> List[str]: + """A wrapper around shlex.split() that uses cmd2's preferred arguments. + + This allows other classes to easily call split() the same way StatementParser does + :param str_to_split: the string being split + :return: A list of tokens + """ + return shlex.split(str_to_split, comments=False, posix=False) + + @attr.s(frozen=True) class MacroArg: """ @@ -226,6 +236,34 @@ class Statement(str): return rtn +def get_command_arg_list(to_parse: Union[Statement, str], preserve_quotes: bool) -> List[str]: + """ + Called by the argument_list and argparse wrappers to retrieve just the arguments being + passed to their do_* methods as a list. + + :param to_parse: what is being passed to the do_* method. It can be one of two types: + 1. An already parsed Statement + 2. An argument string in cases where a do_* method is explicitly called + e.g.: Calling do_help('alias create') would cause to_parse to be 'alias create' + + :param preserve_quotes: if True, then quotes will not be stripped from the arguments + :return: the arguments in a list + """ + if isinstance(to_parse, Statement): + # In the case of a Statement, we already have what we need + if preserve_quotes: + return to_parse.arg_list + else: + return to_parse.argv[1:] + else: + # We only have the argument string. Use the parser to split this string. + parsed_arglist = shlex_split(to_parse) + if not preserve_quotes: + parsed_arglist = [utils.strip_quotes(arg) for arg in parsed_arglist] + + return parsed_arglist + + class StatementParser: """Parse raw text into command components. @@ -349,7 +387,7 @@ class StatementParser: return [] # split on whitespace - tokens = StatementParser.shlex_split(line) + tokens = shlex_split(line) # custom lexing tokens = self._split_on_punctuation(tokens) @@ -607,16 +645,6 @@ class StatementParser: return command, args - @staticmethod - def shlex_split(str_to_split: str) -> List[str]: - """ - A wrapper around shlex.split() that uses cmd2's preferred arguments - This allows other classes to easily call split() the same way StatementParser does - :param str_to_split: the string being split - :return: A list of tokens - """ - return shlex.split(str_to_split, comments=False, posix=False) - def _split_on_punctuation(self, tokens: List[str]) -> List[str]: """Further splits tokens from a command line using punctuation characters -- cgit v1.2.1 From 38804d740f718c6e1db1e96845fae87b597a456e Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Wed, 6 Mar 2019 08:43:17 -0500 Subject: Updated a comment and moved an import --- cmd2/parsing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'cmd2/parsing.py') diff --git a/cmd2/parsing.py b/cmd2/parsing.py index 9c95ba75..d72ca4ec 100644 --- a/cmd2/parsing.py +++ b/cmd2/parsing.py @@ -256,7 +256,7 @@ def get_command_arg_list(to_parse: Union[Statement, str], preserve_quotes: bool) else: return to_parse.argv[1:] else: - # We only have the argument string. Use the parser to split this string. + # We have the arguments in a string. Use shlex to split it. parsed_arglist = shlex_split(to_parse) if not preserve_quotes: parsed_arglist = [utils.strip_quotes(arg) for arg in parsed_arglist] -- cgit v1.2.1