Click REPL Plugin
click-repl is a Python library that provides a REPL (Read-Eval-Print Loop) plugin for Click-based command-line applications. It allows users to execute multiple commands in a single interactive session, maintaining a shared context. The current version is 0.3.0, released in June 2023, and it generally follows the release cadence of its upstream dependency, Click.
Warnings
- breaking `click-repl` 0.3.0 dropped support for Python 2.x. Applications running on Python 2 must use an older version (e.g., 0.2.x).
- breaking In `click-repl` 0.3.0, the internal implementation for the `prompt()` function was changed to use `prompt_toolkit.PromptSession()` instead of `prompt_toolkit.prompt()`. While this fixed an unbound memory usage growth, it might subtly affect highly customized REPL environments that were directly interacting with the internal prompt mechanism.
- gotcha Older versions of `click-repl` (specifically prior to 0.2.0) were incompatible with Click 8.0.0 due to changes in Click's internal API. Attempting to use `click-repl` 0.1.x with Click 8+ will result in import errors or runtime failures.
- gotcha When using `click-repl`, the parent context (`ctx.obj`) is reused between subcommands. This is a feature for performance but can be a footgun if you expect a fresh context for each REPL command, potentially leading to stale data or unexpected side effects if not handled carefully.
Install
-
pip install click-repl
Imports
- register_repl
from click_repl import register_repl
- repl
from click_repl import repl
Quickstart
import click
from click_repl import register_repl
@click.group()
def cli():
"""A simple CLI with a REPL."""
pass
@cli.command()
def hello():
click.echo("Hello from the REPL!")
@cli.command()
@click.argument('name')
def greet(name):
click.echo(f"Nice to meet you, {name}!")
if __name__ == '__main__':
register_repl(cli)
cli()