pexpect/pexpect — pull request #51 by takluyver (2014-05-13 00:28:09+00:00)
Title: Add high level API for wrapping REPLs

This adds a simple wrapper around REPLs (interactive shells) to make it easy to send a command and get all the output before the next prompt. This is how you'd use it to control a Python shell:

```python
from pexpect.replwrap import REPLWrapper, PEXPECT_PROMPT
py = REPLWrapper("python", u">>> ", "import sys; sys.ps1=%r" % PEXPECT_PROMPT)
res = py.run_command("6*7").strip()
# res is now the string "42"
```

I haven't quite worked out yet how to deal with multiline input and continuation prompts. I think it should work so that the user gives a whole block of code to `run_command()` in one go, and if there's still a continuation prompt after that, it throws an error (and sends Ctrl-C to get back to a clean prompt? Are there any REPLs where this doesn't work?). An alternative would be that you can just keep feeding it lines, and it returns either an empty string or a sentinel value to indicate that it's got a continuation prompt.