Skip to content

Commit f3325cb

Browse files
committed
Fixed a few commands that would have failed if arguments containing quotes were used
1 parent 325e524 commit f3325cb

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

cmd2/cmd2.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3398,7 +3398,7 @@ def do_run_pyscript(self, args: argparse.Namespace) -> Optional[bool]:
33983398
sys.argv = [args.script_path] + args.script_arguments
33993399

34003400
# noinspection PyTypeChecker
3401-
py_return = self.do_py('--pyscript {}'.format(utils.quote_string_if_needed(args.script_path)))
3401+
py_return = self.do_py('--pyscript {}'.format(utils.quote_string(args.script_path)))
34023402

34033403
except KeyboardInterrupt:
34043404
pass
@@ -3554,10 +3554,10 @@ def do_history(self, args: argparse.Namespace) -> Optional[bool]:
35543554
fobj.write('{}\n'.format(command.raw))
35553555
try:
35563556
# noinspection PyTypeChecker
3557-
self.do_edit(utils.quote_string_if_needed(fname))
3557+
self.do_edit(utils.quote_string(fname))
35583558

35593559
# noinspection PyTypeChecker
3560-
self.do_run_script(utils.quote_string_if_needed(fname))
3560+
self.do_run_script(utils.quote_string(fname))
35613561
finally:
35623562
os.remove(fname)
35633563
elif args.output_file:
@@ -3754,9 +3754,9 @@ def do_edit(self, args: argparse.Namespace) -> None:
37543754
if not self.editor:
37553755
raise EnvironmentError("Please use 'set editor' to specify your text editing program of choice.")
37563756

3757-
command = utils.quote_string_if_needed(os.path.expanduser(self.editor))
3757+
command = utils.quote_string(os.path.expanduser(self.editor))
37583758
if args.file_path:
3759-
command += " " + utils.quote_string_if_needed(os.path.expanduser(args.file_path))
3759+
command += " " + utils.quote_string(os.path.expanduser(args.file_path))
37603760

37613761
# noinspection PyTypeChecker
37623762
self.do_shell(command)
@@ -3867,7 +3867,7 @@ def do__relative_run_script(self, args: argparse.Namespace) -> Optional[bool]:
38673867
relative_path = os.path.join(self._current_script_dir or '', file_path)
38683868

38693869
# noinspection PyTypeChecker
3870-
return self.do_run_script(utils.quote_string_if_needed(relative_path))
3870+
return self.do_run_script(utils.quote_string(relative_path))
38713871

38723872
def _run_transcript_tests(self, transcript_paths: List[str]) -> None:
38733873
"""Runs transcript tests for provided file(s).

cmd2/utils.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,8 @@ def is_quoted(arg: str) -> bool:
2525
return len(arg) > 1 and arg[0] == arg[-1] and arg[0] in constants.QUOTES
2626

2727

28-
def quote_string_if_needed(arg: str) -> str:
29-
""" Quotes a string if it contains spaces and isn't already quoted """
30-
if is_quoted(arg) or ' ' not in arg:
31-
return arg
32-
28+
def quote_string(arg: str) -> str:
29+
"""Quote a string"""
3330
if '"' in arg:
3431
quote = "'"
3532
else:
@@ -38,8 +35,16 @@ def quote_string_if_needed(arg: str) -> str:
3835
return quote + arg + quote
3936

4037

38+
def quote_string_if_needed(arg: str) -> str:
39+
"""Quote a string if it contains spaces and isn't already quoted"""
40+
if is_quoted(arg) or ' ' not in arg:
41+
return arg
42+
43+
return quote_string(arg)
44+
45+
4146
def strip_quotes(arg: str) -> str:
42-
""" Strip outer quotes from a string.
47+
"""Strip outer quotes from a string.
4348
4449
Applies to both single and double quotes.
4550

tests/test_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def test_quote_string_if_needed_yes():
9999
your_str = '"foo" bar'
100100
assert cu.quote_string_if_needed(your_str) == "'" + your_str + "'"
101101

102-
def test_quot_string_if_needed_no():
102+
def test_quote_string_if_needed_no():
103103
my_str = "HelloWorld"
104104
assert cu.quote_string_if_needed(my_str) == my_str
105105
your_str = "'Hello World'"

0 commit comments

Comments
 (0)