summaryrefslogtreecommitdiff
path: root/cmd2/utils.py
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2019-05-07 23:39:08 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2019-05-07 23:39:08 -0400
commitcedc154489a654dc229eed3b4494f5eae43a4290 (patch)
tree5a5300c54a47326f2f293b4486e913fdad7c6cab /cmd2/utils.py
parentf9ea58edbbe27ba5bcea2534263c992e8a2c7ab8 (diff)
downloadcmd2-git-cedc154489a654dc229eed3b4494f5eae43a4290.tar.gz
Added capability to redirect pipe commands and chain them together
Diffstat (limited to 'cmd2/utils.py')
-rw-r--r--cmd2/utils.py21
1 files changed, 21 insertions, 0 deletions
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')