A lightweight, Pythonic interface for building Python CLI tools with tab completion and command history.
Project description
managed-readline-sessions
The Problem
Every Python CLI tool that wants:
- Tab completion
- Command history
- Custom key bindings
...ends up writing the same fragile boilerplate that:
- Modifies global readline state
- Often leaks those modifications
- Reimplements the same completion caching
- Struggles with nested scenarios
The Solution
import os.path
import typing
from managed_readline_sessions import TabBasedTokenCompletionSession, ReadWriteHistoryFileSession, PrefilledExampleTextSession
all_commands = ['help', 'exit', 'load']
# Can return anything that is `typing.Iterable[str]`
# We will consume the iterable and cache its elements
# Every time the user initiates a tab-based token completion
# And the values passed to `line`, `token`, and `index` change
def get_token_completions(line: str, token: str, index: int) -> typing.Iterable[str]:
# The token starts at the beginning of the line with no characters in front of it
if index == 0:
for command in all_commands:
# The token is a prefix of a command
if command.startswith(token):
# Add a space at the end of the command such that the user moves on to enter the next token
yield command + ' '
# Delimit tokens with spaces
token_boundary_delimiters = {' '}
with ReadWriteHistoryFileSession(os.path.join(os.path.expanduser('~'), '.myapp_history')):
while True:
with TabBasedTokenCompletionSession(get_token_completions, token_boundary_delimiters):
# Show a template each time (e.g., a frequently used command style)
with PrefilledExampleTextSession('myapp run --input='):
try:
command_line = input('myapp> ')
# The line will start with 'myapp run --input=' pre-inserted for the user to edit
# Process `command_line`...
except EOFError:
break # History saved automatically
Now your tool has:
- Persistent history
- Tab completion
- Clean state management
- Professional UX
All in just 10 lines of bulletproof code!
Key Benefits
- ✓ Guaranteed cleanup - Never corrupt a user's shell session again
- ✓ Nested sessions - Works correctly when called from other tools
- ✓ Performance optimized - Smart completion caching
- ✓ Example-filled prompts - Guide users with prefilled templates
- ✓ Battle-tested - Properly handles edge cases most implementations miss
- ✓ Zero dependencies - Except
pyreadlineandtyping, which are pure-Python - ✓ Compatibility - Supports all operating systems and Python 2+
Real-World Use Cases
- REPLs - Give users tab completion without breaking their existing shell
- CLI tools - Add history support that persists between runs
- Interactive apps - Implement custom key bindings safely
- Debuggers - Offer completion without interfering with parent process
Installation
pip install managed-readline-sessions
Contributing
Contributions are welcome! Please submit pull requests or open issues on the GitHub repository.
License
This project is licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file managed_readline_sessions-0.1.0a6.tar.gz.
File metadata
- Download URL: managed_readline_sessions-0.1.0a6.tar.gz
- Upload date:
- Size: 5.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
26a20f2635946abfb63ec539cc1db713257601c239b3262fc1f5bcda24494165
|
|
| MD5 |
cb774708d6017bcd0a2dee34ba57acd0
|
|
| BLAKE2b-256 |
45d816047751d0fbfb8bc2b30532b01c0184d36a9cbd3e61c55ce33b31ffaf57
|
File details
Details for the file managed_readline_sessions-0.1.0a6-py2.py3-none-any.whl.
File metadata
- Download URL: managed_readline_sessions-0.1.0a6-py2.py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b010e6c198c0655e0e4a53f712deb4415fae9848033781e1d5103decb866e513
|
|
| MD5 |
239817b54459c19e359ab1f1dfd6584c
|
|
| BLAKE2b-256 |
f26c447b51ee39f20ef12fcca9afcd563def1ea1b60f0a3de95dfbe5ee7bed7e
|