I would like to convert a program I wrote from expect
to pexpect
, but the api is radically different and a lot of the features I know and love from expect
I haven't figured out how to use in python
.
I wondered if anyone has a way of interacting purely with a user. In expect I use send_user
paired with expect_user
, this type of sequence is usually triggered by a pattern observed in a spawned process or from special key codes used in an interaction with a spawned process.
I saw this example for send_user
, and tried to print a prompt for input followed by the python
input()
function, but my program locks up.
Here is a code snippet:
import pexpect
import sys
def input_filter(s):
if s == b'\004': # ctrl-d
sys.stdout.write(f'\n\rYou pressed ctrl-d, press y to quit.\r\n')
sys.stdout.flush()
i = input()
if i == 'y':
return b'\r: ok, bye; exit\r'
else:
return b''
else:
return s
proc = pexpect.spawn('bash --norc')
proc.interact(input_filter=input_filter)