diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2018-09-21 01:21:07 -0400 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2018-09-21 01:21:07 -0400 |
commit | 892b5ff94642b26aa94c4a2e8adca58d66dbe4dd (patch) | |
tree | 5b1057045f2e56c24d3ad36e65cb88916c6ef25d /cmd2/utils.py | |
parent | 0bc3b978ab5ffaee2944773a6eb4ef62de23ce4a (diff) | |
download | cmd2-git-892b5ff94642b26aa94c4a2e8adca58d66dbe4dd.tar.gz |
Refactored do_set
Diffstat (limited to 'cmd2/utils.py')
-rw-r--r-- | cmd2/utils.py | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/cmd2/utils.py b/cmd2/utils.py index 64401895..f7ca29c2 100644 --- a/cmd2/utils.py +++ b/cmd2/utils.py @@ -20,6 +20,28 @@ def strip_ansi(text: str) -> str: return constants.ANSI_ESCAPE_RE.sub('', text) +def is_quoted(arg: str) -> bool: + """ + Checks if a string is quoted + :param arg: the string being checked for quotes + :return: True if a string is quotes + """ + return len(arg) > 1 and arg[0] == arg[-1] and arg[0] in constants.QUOTES + + +def quote_string_if_needed(arg: str) -> str: + """ Quotes a string if it contains spaces and isn't already quoted """ + if is_quoted(arg) or ' ' not in arg: + return arg + + if '"' in arg: + quote = "'" + else: + quote = '"' + + return quote + arg + quote + + def strip_quotes(arg: str) -> str: """ Strip outer quotes from a string. @@ -28,7 +50,7 @@ def strip_quotes(arg: str) -> str: :param arg: string to strip outer quotes from :return: same string with potentially outer quotes stripped """ - if len(arg) > 1 and arg[0] == arg[-1] and arg[0] in constants.QUOTES: + if is_quoted(arg): arg = arg[1:-1] return arg |