From cedc154489a654dc229eed3b4494f5eae43a4290 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Tue, 7 May 2019 23:39:08 -0400 Subject: Added capability to redirect pipe commands and chain them together --- cmd2/utils.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'cmd2/utils.py') diff --git a/cmd2/utils.py b/cmd2/utils.py index e8e8a611..45e55c2b 100644 --- a/cmd2/utils.py +++ b/cmd2/utils.py @@ -275,6 +275,27 @@ def unquote_specific_tokens(args: List[str], tokens_to_unquote: List[str]) -> No args[i] = unquoted_arg +def expand_user_in_tokens(tokens: List[str]) -> None: + """ + Call os.path.expanduser() on all tokens in an already parsed list of command-line arguments. + This also supports expanding user in quoted tokens. + :param tokens: tokens to expand + """ + for index, _ in enumerate(tokens): + if tokens[index]: + # Check if the token is quoted. Since parsing already passed, there isn't + # an unclosed quote. So we only need to check the first character. + first_char = tokens[index][0] + if first_char in constants.QUOTES: + tokens[index] = strip_quotes(tokens[index]) + + tokens[index] = os.path.expanduser(tokens[index]) + + # Restore the quotes + if first_char in constants.QUOTES: + tokens[index] = first_char + tokens[index] + first_char + + def find_editor() -> str: """Find a reasonable editor to use by default for the system that the cmd2 application is running on.""" editor = os.environ.get('EDITOR') -- cgit v1.2.1 From 35311cebc81c52f1e5f334df0dec6226d07755c1 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Wed, 8 May 2019 00:44:34 -0400 Subject: Updated a comment --- cmd2/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'cmd2/utils.py') diff --git a/cmd2/utils.py b/cmd2/utils.py index 45e55c2b..398d39e6 100644 --- a/cmd2/utils.py +++ b/cmd2/utils.py @@ -291,7 +291,7 @@ def expand_user_in_tokens(tokens: List[str]) -> None: tokens[index] = os.path.expanduser(tokens[index]) - # Restore the quotes + # Restore the quotes even if not needed to preserve what the user typed if first_char in constants.QUOTES: tokens[index] = first_char + tokens[index] + first_char -- cgit v1.2.1 From c4ee21446e1507ebd5c42a521fa2c0bcb2648c85 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Mon, 13 May 2019 12:52:49 -0400 Subject: Preserving originally typed quotes of Statement.output_to for use in Statement.post_command() --- cmd2/utils.py | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) (limited to 'cmd2/utils.py') diff --git a/cmd2/utils.py b/cmd2/utils.py index 398d39e6..54ad763d 100644 --- a/cmd2/utils.py +++ b/cmd2/utils.py @@ -275,25 +275,34 @@ def unquote_specific_tokens(args: List[str], tokens_to_unquote: List[str]) -> No args[i] = unquoted_arg +def expand_user(token: str) -> str: + """ + Wrap os.expanduser() to support expanding ~ in quoted strings + :param token: the string to expand + """ + if token: + if is_quoted(token): + quote_char = token[0] + token = strip_quotes(token) + else: + quote_char = '' + + token = os.path.expanduser(token) + + # Restore the quotes even if not needed to preserve what the user typed + if quote_char: + token = quote_char + token + quote_char + + return token + + def expand_user_in_tokens(tokens: List[str]) -> None: """ - Call os.path.expanduser() on all tokens in an already parsed list of command-line arguments. - This also supports expanding user in quoted tokens. + Call expand_user() on all tokens in a list of strings :param tokens: tokens to expand """ for index, _ in enumerate(tokens): - if tokens[index]: - # Check if the token is quoted. Since parsing already passed, there isn't - # an unclosed quote. So we only need to check the first character. - first_char = tokens[index][0] - if first_char in constants.QUOTES: - tokens[index] = strip_quotes(tokens[index]) - - tokens[index] = os.path.expanduser(tokens[index]) - - # Restore the quotes even if not needed to preserve what the user typed - if first_char in constants.QUOTES: - tokens[index] = first_char + tokens[index] + first_char + tokens[index] = expand_user(tokens[index]) def find_editor() -> str: -- cgit v1.2.1