expect
Script predictable terminal interactions in one call: answer matching prompts, send lines or keys, and stop on unanswered questions.
Instructions
Script predictable interactions in one call: optionally type input
(+ Enter), then answer each prompt that matches a respond rule, in
whatever order the prompts appear. The interactive form of yes |,
echo y |, gdb's -batch and an expect script: the program gets a real
terminal, and only the questions you foresaw are answered. Rules that
never match are fine -- use them for questions only sometimes asked.
Ends when the command finishes (bash), the program is back at its
prompt (prompt_pattern, default as for run), until (a regex)
matches, a stop rule matches, or -- the important case -- output
stops at a question no rule answers: that returns at once as
"unanswered" instead of waiting for the timeout, so a surprise comes
back to you rather than being guessed at.
Example, gdb's optional questions:
input="gdb -q -ex 'break lib_fn' -ex run ./prog",
respond=[{"pattern": "Make breakpoint pending", "line": "y"},
{"pattern": "Enable debuginfod", "line": "n"}]
Example, ssh, handing any password prompt to the human:
input="ssh host", respond=[{"pattern": "continue connecting", "line": "yes"},
{"pattern": "[Pp]assword:", "stop": true}]
Without input, the rules apply to a question already waiting at the
cursor (one a run came back "waiting" at).
Returns a transcript (what matched, what was sent), rules that never
fired, and the resulting region(s) as run does.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| force | No | type even over someone else's unsubmitted line at the prompt, discarding it first; only for a line you know is stale | |
| input | No | a line to type first (Enter added), e.g. the command that will ask the questions; without it the rules are applied to a question already waiting at the cursor | |
| until | No | regex: return as soon as the output matches it, the command still running (status "until"; `wait` resumes); not a prompt pattern | |
| respond | Yes | the rules: each fires when its `pattern` appears, doing its one action (line, text, keys or stop); a question no rule covers ends the call as "unanswered" | |
| session | Yes | the session's name (from session_create or session_list) | |
| max_lines | No | lines of output shown in the reply; longer output is clipped in the middle, led by its summary lines (test results, compiler errors, make's failure) when the middle holds them; all of it stays readable with `read`/`search` | |
| timeout_s | No | seconds to wait before giving up (the command keeps running; `wait` resumes); at most 1500 | |
| prompt_pattern | No | regex that marks the program's prompt at the end of the cursor line; rarely needed, the default knows shell, gdb, pdb, lldb and python prompts and any ending in $ # % >; pass one only after a reply came back "waiting" at a prompt it did not know (it offers the pattern); make it cover a part that changes |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |