From 9054a211b60f55e00ea446433444c0bf4970a50f Mon Sep 17 00:00:00 2001 From: Todd Leonhardt Date: Sun, 2 Jul 2017 10:26:25 -0400 Subject: 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. --- cmd2.py | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) (limited to 'cmd2.py') diff --git a/cmd2.py b/cmd2.py index 66e4716f..9d1a7160 100755 --- a/cmd2.py +++ b/cmd2.py @@ -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 [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. -- cgit v1.2.1