diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2018-10-01 15:59:35 -0400 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2018-10-01 15:59:35 -0400 |
commit | bc74542becb69980d8009af5272882530b92376c (patch) | |
tree | 16a5294564a6cf2d3da87bbca851d20af95bb3f0 | |
parent | aae1c61bab32188be9c47c125604c81db120885f (diff) | |
download | cmd2-git-bc74542becb69980d8009af5272882530b92376c.tar.gz |
Preserving quotes for do_py input
-rw-r--r-- | cmd2/cmd2.py | 10 | ||||
-rw-r--r-- | tests/test_cmd2.py | 5 |
2 files changed, 11 insertions, 4 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 79d3c80a..03c71a91 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -2892,6 +2892,7 @@ class Cmd(cmd.Cmd): help='arguments to pass to command'), ACTION_ARG_CHOICES, ('path_complete',)) + # Preserve quotes since we are passing these strings to the shell @with_argparser(shell_parser, preserve_quotes=True) def do_shell(self, args: argparse.Namespace) -> None: """Execute a command as if at the OS prompt""" @@ -2947,7 +2948,8 @@ class Cmd(cmd.Cmd): py_parser.add_argument('command', help="command to run", nargs='?') py_parser.add_argument('remainder', help="remainder of command", nargs=argparse.REMAINDER) - @with_argparser(py_parser) + # Preserve quotes since we are passing these strings to Python + @with_argparser(py_parser, preserve_quotes=True) def do_py(self, args: argparse.Namespace) -> bool: """Invoke Python command or shell""" from .pyscript_bridge import PyscriptBridge, CommandResult @@ -2987,9 +2989,9 @@ class Cmd(cmd.Cmd): interp.runcode('import sys, os;sys.path.insert(0, os.getcwd())') if args.command: - full_command = utils.quote_string_if_needed(args.command) - for cur_token in args.remainder: - full_command += ' ' + utils.quote_string_if_needed(cur_token) + full_command = args.command + if args.remainder: + full_command += ' ' + ' '.join(args.remainder) interp.runcode(full_command) diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index 3ce7a11d..a382a940 100644 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -219,10 +219,15 @@ def test_base_py(base_app, capsys): run_cmd(base_app, 'py qqq=3') out, err = capsys.readouterr() assert out == '' + run_cmd(base_app, 'py print(qqq)') out, err = capsys.readouterr() assert out.rstrip() == '3' + run_cmd(base_app, 'py print("spaces" + " in this " + "command")') + out, err = capsys.readouterr() + assert out.rstrip() == 'spaces in this command' + @pytest.mark.skipif(sys.platform == 'win32', reason="Unit test doesn't work on win32, but feature does") |