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