MCPcopy Create free account
hub / github.com/pexpect/pexpect / run_command

Method run_command

pexpect/replwrap.py:68–109  ·  view source on GitHub ↗

Send a command to the REPL, wait for and return output. :param str command: The command to send. Trailing newlines are not needed. This should be a complete block of input that will trigger execution; if a continuation prompt is found after sending input, :exc:`ValueErro

(self, command, timeout=-1, async_=False)

Source from the content-addressed store, hash-verified

66 timeout=timeout, async_=async_)
67
68 def run_command(self, command, timeout=-1, async_=False):
69 """Send a command to the REPL, wait for and return output.
70
71 :param str command: The command to send. Trailing newlines are not needed.
72 This should be a complete block of input that will trigger execution;
73 if a continuation prompt is found after sending input, :exc:`ValueError`
74 will be raised.
75 :param int timeout: How long to wait for the next prompt. -1 means the
76 default from the :class:`pexpect.spawn` object (default 30 seconds).
77 None means to wait indefinitely.
78 :param bool async_: On Python 3.4, or Python 3.3 with asyncio
79 installed, passing ``async_=True`` will make this return an
80 :mod:`asyncio` Future, which you can yield from to get the same
81 result that this method would normally give directly.
82 """
83 # Split up multiline commands and feed them in bit-by-bit
84 cmdlines = command.splitlines()
85 # splitlines ignores trailing newlines - add it back in manually
86 if command.endswith('\n'):
87 cmdlines.append('')
88 if not cmdlines:
89 raise ValueError("No command was given")
90
91 if async_:
92 from ._async import repl_run_command_async
93 return repl_run_command_async(self, cmdlines, timeout)
94
95 res = []
96 self.child.sendline(cmdlines[0])
97 for line in cmdlines[1:]:
98 self._expect_prompt(timeout=timeout)
99 res.append(self.child.before)
100 self.child.sendline(line)
101
102 # Command was fully submitted, now wait for the next prompt
103 if self._expect_prompt(timeout=timeout) == 1:
104 # We got the continuation prompt - command was incomplete
105 self.child.kill(signal.SIGINT)
106 self._expect_prompt(timeout=1)
107 raise ValueError("Continuation prompt found - input was incomplete:\n"
108 + command)
109 return u''.join(res + [self.child.before])
110
111def python(command=sys.executable):
112 """Start a Python shell and return a :class:`REPLWrapper` object."""

Callers 13

__init__Method · 0.95
test_existing_spawnMethod · 0.95
test_no_change_promptMethod · 0.95
test_bashMethod · 0.80
test_pager_as_catMethod · 0.80
test_bash_envMethod · 0.80
test_multilineMethod · 0.80
test_zshMethod · 0.80
test_pythonMethod · 0.80
test_async_replwrapMethod · 0.80

Calls 4

_expect_promptMethod · 0.95
repl_run_command_asyncFunction · 0.70
sendlineMethod · 0.45
killMethod · 0.45

Tested by 12

test_existing_spawnMethod · 0.76
test_no_change_promptMethod · 0.76
test_bashMethod · 0.64
test_pager_as_catMethod · 0.64
test_bash_envMethod · 0.64
test_multilineMethod · 0.64
test_zshMethod · 0.64
test_pythonMethod · 0.64
test_async_replwrapMethod · 0.64