diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2017-07-02 10:26:25 -0400 |
---|---|---|
committer | Todd Leonhardt <todd.leonhardt@gmail.com> | 2017-07-02 10:26:25 -0400 |
commit | 9054a211b60f55e00ea446433444c0bf4970a50f (patch) | |
tree | 8772cac8fc4d002e23886d41cee629f1a4972acf | |
parent | cc7a3303e1c95f713e84cf3bbd6acf262823e3a2 (diff) | |
download | cmd2-git-9054a211b60f55e00ea446433444c0bf4970a50f.tar.gz |
shell command now uses cmd.Cmd.stdout for output
The main advantages of this are that shell command output can now be
- piped to another shell command
- redirected to a file
In the future, I may take it a step further where the output is piped, but using a pipe versus a real terminal changes the behavior of many shell commands, for example by getting rid of ANSI color and various other pretty formatting.
-rwxr-xr-x | cmd2.py | 12 | ||||
-rw-r--r-- | tests/test_cmd2.py | 4 |
2 files changed, 4 insertions, 12 deletions
@@ -1138,21 +1138,15 @@ class Cmd(cmd.Cmd): Usage: pause [text]""" sm.input(text + '\n') - # noinspection PyMethodMayBeStatic def do_shell(self, command): """Execute a command as if at the OS prompt. Usage: shell <command> [arguments]""" try: - out = subprocess.check_output(shlex.split(command)) - except subprocess.CalledProcessError as e: - self.perror(e, traceback_war=False) + proc = subprocess.Popen(command, stdout=self.stdout, stderr=sys.stderr, shell=True) + proc.communicate() except FileNotFoundError as e: - self.perror(e, traceback_war=False) - else: - if six.PY3: - out = out.decode() - self.stdout.write(out + '\n') + self.perror(e.strerror, traceback_war=False) def path_complete(self, text, line, begidx, endidx, dir_exe_only=False, dir_only=False): """Method called to complete an input line by local file system path completion. diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index 592e6af2..9dd2697b 100644 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -103,12 +103,10 @@ To enable full traceback, run the following command: 'set debug true' def test_base_shell(base_app, monkeypatch): m = mock.Mock() - monkeypatch.setattr("os.system", m) + monkeypatch.setattr("subprocess.Popen", m) out = run_cmd(base_app, 'shell echo a') assert out == [] assert m.called - m.assert_called_with('echo a') - def test_base_py(base_app, capsys): run_cmd(base_app, 'py qqq=3') |