summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2019-07-24 11:09:23 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2019-07-24 11:09:23 -0400
commitf3325cb39e9e20aa24d07107ebf048029059b44f (patch)
treeb7c053bfdf81da5baf78758d0732c7e33af5460e
parent325e52451a81b900ac77626a2d2a271854544627 (diff)
downloadcmd2-git-f3325cb39e9e20aa24d07107ebf048029059b44f.tar.gz
Fixed a few commands that would have failed if arguments containing quotes were used
-rw-r--r--cmd2/cmd2.py12
-rw-r--r--cmd2/utils.py17
-rw-r--r--tests/test_utils.py2
3 files changed, 18 insertions, 13 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index 753f28ee..df7ce833 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -3398,7 +3398,7 @@ class Cmd(cmd.Cmd):
sys.argv = [args.script_path] + args.script_arguments
# noinspection PyTypeChecker
- py_return = self.do_py('--pyscript {}'.format(utils.quote_string_if_needed(args.script_path)))
+ py_return = self.do_py('--pyscript {}'.format(utils.quote_string(args.script_path)))
except KeyboardInterrupt:
pass
@@ -3554,10 +3554,10 @@ class Cmd(cmd.Cmd):
fobj.write('{}\n'.format(command.raw))
try:
# noinspection PyTypeChecker
- self.do_edit(utils.quote_string_if_needed(fname))
+ self.do_edit(utils.quote_string(fname))
# noinspection PyTypeChecker
- self.do_run_script(utils.quote_string_if_needed(fname))
+ self.do_run_script(utils.quote_string(fname))
finally:
os.remove(fname)
elif args.output_file:
@@ -3754,9 +3754,9 @@ class Cmd(cmd.Cmd):
if not self.editor:
raise EnvironmentError("Please use 'set editor' to specify your text editing program of choice.")
- command = utils.quote_string_if_needed(os.path.expanduser(self.editor))
+ command = utils.quote_string(os.path.expanduser(self.editor))
if args.file_path:
- command += " " + utils.quote_string_if_needed(os.path.expanduser(args.file_path))
+ command += " " + utils.quote_string(os.path.expanduser(args.file_path))
# noinspection PyTypeChecker
self.do_shell(command)
@@ -3867,7 +3867,7 @@ class Cmd(cmd.Cmd):
relative_path = os.path.join(self._current_script_dir or '', file_path)
# noinspection PyTypeChecker
- return self.do_run_script(utils.quote_string_if_needed(relative_path))
+ return self.do_run_script(utils.quote_string(relative_path))
def _run_transcript_tests(self, transcript_paths: List[str]) -> None:
"""Runs transcript tests for provided file(s).
diff --git a/cmd2/utils.py b/cmd2/utils.py
index b19c2b49..d0ce10bc 100644
--- a/cmd2/utils.py
+++ b/cmd2/utils.py
@@ -25,11 +25,8 @@ def is_quoted(arg: str) -> bool:
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
-
+def quote_string(arg: str) -> str:
+ """Quote a string"""
if '"' in arg:
quote = "'"
else:
@@ -38,8 +35,16 @@ def quote_string_if_needed(arg: str) -> str:
return quote + arg + quote
+def quote_string_if_needed(arg: str) -> str:
+ """Quote a string if it contains spaces and isn't already quoted"""
+ if is_quoted(arg) or ' ' not in arg:
+ return arg
+
+ return quote_string(arg)
+
+
def strip_quotes(arg: str) -> str:
- """ Strip outer quotes from a string.
+ """Strip outer quotes from a string.
Applies to both single and double quotes.
diff --git a/tests/test_utils.py b/tests/test_utils.py
index edb6ca68..c32d6870 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -99,7 +99,7 @@ def test_quote_string_if_needed_yes():
your_str = '"foo" bar'
assert cu.quote_string_if_needed(your_str) == "'" + your_str + "'"
-def test_quot_string_if_needed_no():
+def test_quote_string_if_needed_no():
my_str = "HelloWorld"
assert cu.quote_string_if_needed(my_str) == my_str
your_str = "'Hello World'"