Skip to main content

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:

  1. Modifies global readline state
  2. Often leaks those modifications
  3. Reimplements the same completion caching
  4. 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 pyreadline and typing, which are pure-Python
  • Compatibility - Supports all operating systems and Python 2+

Real-World Use Cases

  1. REPLs - Give users tab completion without breaking their existing shell
  2. CLI tools - Add history support that persists between runs
  3. Interactive apps - Implement custom key bindings safely
  4. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

managed_readline_sessions-0.1.0a6.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

managed_readline_sessions-0.1.0a6-py2.py3-none-any.whl (6.0 kB view details)

Uploaded Python 2Python 3

File details

Details for the file managed_readline_sessions-0.1.0a6.tar.gz.

File metadata

File hashes

Hashes for managed_readline_sessions-0.1.0a6.tar.gz
Algorithm Hash digest
SHA256 26a20f2635946abfb63ec539cc1db713257601c239b3262fc1f5bcda24494165
MD5 cb774708d6017bcd0a2dee34ba57acd0
BLAKE2b-256 45d816047751d0fbfb8bc2b30532b01c0184d36a9cbd3e61c55ce33b31ffaf57

See more details on using hashes here.

File details

Details for the file managed_readline_sessions-0.1.0a6-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for managed_readline_sessions-0.1.0a6-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b010e6c198c0655e0e4a53f712deb4415fae9848033781e1d5103decb866e513
MD5 239817b54459c19e359ab1f1dfd6584c
BLAKE2b-256 f26c447b51ee39f20ef12fcca9afcd563def1ea1b60f0a3de95dfbe5ee7bed7e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page