summaryrefslogtreecommitdiff
path: root/cmd2/utils.py
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2018-09-21 01:21:07 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2018-09-21 01:21:07 -0400
commit892b5ff94642b26aa94c4a2e8adca58d66dbe4dd (patch)
tree5b1057045f2e56c24d3ad36e65cb88916c6ef25d /cmd2/utils.py
parent0bc3b978ab5ffaee2944773a6eb4ef62de23ce4a (diff)
downloadcmd2-git-892b5ff94642b26aa94c4a2e8adca58d66dbe4dd.tar.gz
Refactored do_set
Diffstat (limited to 'cmd2/utils.py')
-rw-r--r--cmd2/utils.py24
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