Create an interactive application with `plac.Interpreter`
masterIf your script has a large startup time, you can wrap its main function in a plac.Interpreter to turn it into an interactive application. This allows the startup code to run only once, while you execute multiple commands in a loop.
Use the .interact() method to start the interactive session. The session displays the main function's docstring as an introduction and uses a configurable prompt. The session ends when the user sends a CTRL-D (or CTRL-Z on Windows).
Key features:
- Command Abbreviations: The interpreter supports abbreviations (e.g.,
delfordelete). If an abbreviation is ambiguous,placraises aNameError. - Custom Prompt: You can specify a custom prompt string via the
promptargument.
import plac
import my_script
# Wrap the main function
interpreter = plac.Interpreter(my_script.main)
# Start interactive mode with a custom prompt
interpreter.interact(prompt='my-app> ')