Skip to content

Commit 02f234f

Browse files
authored
Merge pull request #163 from python-cmd2/refactor_shell
Refactor shell command
2 parents 26743a7 + 91fc975 commit 02f234f

3 files changed

Lines changed: 5 additions & 8 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ News
2222
* ``load`` command has better error checking and reporting
2323
* Clipboard copy and paste functionality is now handled by the **pyperclip** module
2424
* NOTE: This adds an additional required 3rd-party dependency
25+
* ``shell`` command now supports redirection and piping of output
2526
* Added a lot of unit tests
2627

2728
0.7.3

cmd2.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -479,9 +479,7 @@ def __init__(self, completekey='tab', stdin=None, stdout=None, use_ipython=False
479479
self.initial_stdout = sys.stdout
480480
self.history = History()
481481
self.pystate = {}
482-
# noinspection PyUnresolvedReferences
483-
self.keywords = self.reserved_words + [fname[3:] for fname in dir(self)
484-
if fname.startswith('do_')]
482+
self.keywords = self.reserved_words + [fname[3:] for fname in dir(self) if fname.startswith('do_')]
485483
self.parser_manager = ParserManager(redirector=self.redirector, terminators=self.terminators,
486484
multilineCommands=self.multilineCommands,
487485
legalChars=self.legalChars, commentGrammars=self.commentGrammars,
@@ -1138,12 +1136,12 @@ def do_pause(self, text):
11381136
Usage: pause [text]"""
11391137
sm.input(text + '\n')
11401138

1141-
# noinspection PyMethodMayBeStatic
11421139
def do_shell(self, command):
11431140
"""Execute a command as if at the OS prompt.
11441141
11451142
Usage: shell <command> [arguments]"""
1146-
os.system(command)
1143+
proc = subprocess.Popen(command, stdout=self.stdout, stderr=sys.stderr, shell=True)
1144+
proc.communicate()
11471145

11481146
def path_complete(self, text, line, begidx, endidx, dir_exe_only=False, dir_only=False):
11491147
"""Method called to complete an input line by local file system path completion.

tests/test_cmd2.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,10 @@ def test_base_set_not_supported(base_app, capsys):
103103

104104
def test_base_shell(base_app, monkeypatch):
105105
m = mock.Mock()
106-
monkeypatch.setattr("os.system", m)
106+
monkeypatch.setattr("subprocess.Popen", m)
107107
out = run_cmd(base_app, 'shell echo a')
108108
assert out == []
109109
assert m.called
110-
m.assert_called_with('echo a')
111-
112110

113111
def test_base_py(base_app, capsys):
114112
run_cmd(base_app, 'py qqq=3')

0 commit comments

Comments
 (0)