diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2019-05-13 23:59:03 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-13 23:59:03 -0400 |
commit | 3ee97d121887d3055fc6326b1d9bc290f5235866 (patch) | |
tree | f5695aece2c4e6173513da3f436df73099b88c09 /cmd2/utils.py | |
parent | cbf0313306c99c02f3c503f60d70df4bda2cce64 (diff) | |
parent | 6c051808d83b75108c0549acbc97fe2201f8de63 (diff) | |
download | cmd2-git-3ee97d121887d3055fc6326b1d9bc290f5235866.tar.gz |
Merge pull request #676 from python-cmd2/pipe_chaining
Pipe chaining
Diffstat (limited to 'cmd2/utils.py')
-rw-r--r-- | cmd2/utils.py | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/cmd2/utils.py b/cmd2/utils.py index e8e8a611..54ad763d 100644 --- a/cmd2/utils.py +++ b/cmd2/utils.py @@ -275,6 +275,36 @@ 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 expand_user() on all tokens in a list of strings + :param tokens: tokens to expand + """ + for index, _ in enumerate(tokens): + tokens[index] = expand_user(tokens[index]) + + 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') |