I am making a Python 3 app that has a prompt where the user enters commands. I'm running Python 3.5 on macOS Sierra.
I'm trying to prevent exiting with Ctrl+C
and having users exit instead by simply typing exit
into the prompt.
I am doing this using this method: https://stackoverflow.com/a/6019460/7450368
However, when prompting the user again, I get RuntimeError: can't re-enter readline
.
Is there a way I can fix this?
Here's my code:
import interpret
import signal
def SigIntHand(SIG, FRM):
print("\nUse 'exit' to exit.")
prompt()
signal.signal(signal.SIGINT, SigIntHand)
version = ("v0.0.3")
class CommandError(Exception):
pass
def prompt():
try:
task = input("Einstein " + version + " > ")
except RuntimeError:
task = input("Einstein " + version + " > ")
try:
execute(task)
except CommandError as e:
print(e)
prompt()
def execute(command):
command = command.lower()
interpret.interpret(command)
prompt()
interpret.interpret("command")
is called by main.py
to execute the command command
if it exists.
Any help would be hugely appreciated. Thanks!