Skip to content

Commit 9ca3476

Browse files
authored
Merge pull request #556 from python-cmd2/preserve_quotes
Preserving quotes for do_py input
2 parents aae1c61 + bc74542 commit 9ca3476

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

cmd2/cmd2.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2892,6 +2892,7 @@ def do_set(self, args: argparse.Namespace) -> None:
28922892
help='arguments to pass to command'),
28932893
ACTION_ARG_CHOICES, ('path_complete',))
28942894

2895+
# Preserve quotes since we are passing these strings to the shell
28952896
@with_argparser(shell_parser, preserve_quotes=True)
28962897
def do_shell(self, args: argparse.Namespace) -> None:
28972898
"""Execute a command as if at the OS prompt"""
@@ -2947,7 +2948,8 @@ def _reset_py_display() -> None:
29472948
py_parser.add_argument('command', help="command to run", nargs='?')
29482949
py_parser.add_argument('remainder', help="remainder of command", nargs=argparse.REMAINDER)
29492950

2950-
@with_argparser(py_parser)
2951+
# Preserve quotes since we are passing these strings to Python
2952+
@with_argparser(py_parser, preserve_quotes=True)
29512953
def do_py(self, args: argparse.Namespace) -> bool:
29522954
"""Invoke Python command or shell"""
29532955
from .pyscript_bridge import PyscriptBridge, CommandResult
@@ -2987,9 +2989,9 @@ def run(filename: str):
29872989
interp.runcode('import sys, os;sys.path.insert(0, os.getcwd())')
29882990

29892991
if args.command:
2990-
full_command = utils.quote_string_if_needed(args.command)
2991-
for cur_token in args.remainder:
2992-
full_command += ' ' + utils.quote_string_if_needed(cur_token)
2992+
full_command = args.command
2993+
if args.remainder:
2994+
full_command += ' ' + ' '.join(args.remainder)
29932995

29942996
interp.runcode(full_command)
29952997

tests/test_cmd2.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,15 @@ def test_base_py(base_app, capsys):
219219
run_cmd(base_app, 'py qqq=3')
220220
out, err = capsys.readouterr()
221221
assert out == ''
222+
222223
run_cmd(base_app, 'py print(qqq)')
223224
out, err = capsys.readouterr()
224225
assert out.rstrip() == '3'
225226

227+
run_cmd(base_app, 'py print("spaces" + " in this " + "command")')
228+
out, err = capsys.readouterr()
229+
assert out.rstrip() == 'spaces in this command'
230+
226231

227232
@pytest.mark.skipif(sys.platform == 'win32',
228233
reason="Unit test doesn't work on win32, but feature does")

0 commit comments

Comments
 (0)