Skip to main content

A background command handler for python's command-line interface.

Project description

InputHandler Library

A lightweight Python library for creating interactive command-line interfaces with custom command registration, input handling, and clean log output. It supports synchronous and asynchronous modes, threaded input processing, and enhanced logging.

Features

  • Command Registration: Register commands with decorators and descriptions.
  • Threaded Input: Non-blocking input handling by default.
  • Safe Printing: Logs appear above the input line, preserving your typed text and cursor position.
  • Command History: Navigate recent commands with Up/Down arrow keys.
  • Sync & Async: Support for both synchronous and asynchronous (asyncio) applications.
  • Colored Logging: Built-in support for colored log messages.

Installation

pip install cli_ih

Quick Start (Synchronous)

from cli_ih import InputHandler, safe_print

handler = InputHandler(cursor="> ")

# Use safe_print instead of print to keep the input line clean!
@handler.command(name="greet", description="Greets the user.")
def greet(name):
    safe_print(f"Hello, {name}!")

@handler.command(name="add", description="Adds two numbers.")
def add(a, b):
    safe_print(int(a) + int(b))

handler.start()

# Using safe_print allows you to print logs in the background 
# without messing up the user's current input line.

Async Client Example

The AsyncInputHandler integrates with asyncio. The start() method is non-blocking when thread_mode=True (default).

import asyncio
from cli_ih import AsyncInputHandler, safe_print

handler = AsyncInputHandler(cursor="Async> ")

@handler.command(name="greet", description="Greets the user asynchronously.")
async def greet(name):
    await asyncio.sleep(1)
    safe_print(f"Hello, {name}")

@handler.command(name="add", description="Adds two numbers.")
async def add(a, b):
    safe_print(int(a) + int(b))

# Start the handler (runs in a separate thread by default)
handler.start()

# Keep the main thread alive or run your main event loop
async def main():
    while handler.is_running:
        await asyncio.sleep(1)

if __name__ == "__main__":
    asyncio.run(main())

Key Considerations

Safe Printing

Always use from cli_ih import safe_print for outputting text to the console. This utility automatically detects the active input handler and ensures that your log message is printed above the current input line, preserving the user's cursor and any text they are currently typing.

from cli_ih import safe_print

# Good
safe_print("Log message")

# Avoid (might disrupt input line)
print("Log message")

Thread Mode

Both InputHandler and AsyncInputHandler accept a thread_mode parameter (default True).

  • thread_mode=True: The input loop runs in a separate thread. start() returns immediately.
  • thread_mode=False: The input loop runs in the current thread. start() blocks until exit.

Command History

Use the Up and Down arrow keys to cycle through your previously entered commands, just like in a standard terminal.

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

cli_ih-0.7.1.4.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

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

cli_ih-0.7.1.4-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

Details for the file cli_ih-0.7.1.4.tar.gz.

File metadata

  • Download URL: cli_ih-0.7.1.4.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for cli_ih-0.7.1.4.tar.gz
Algorithm Hash digest
SHA256 f2d39932b30a7aec7e422ad0c26417c3cd4be579d91e8b7e8f408920f6a7aef2
MD5 9e58aefde8f4adb8cf7659d27c7261a5
BLAKE2b-256 7625d1a3fc63f3d33c1be4304ce5043e8ddbf9c78e79b6872d68b719a19ad73f

See more details on using hashes here.

File details

Details for the file cli_ih-0.7.1.4-py3-none-any.whl.

File metadata

  • Download URL: cli_ih-0.7.1.4-py3-none-any.whl
  • Upload date:
  • Size: 11.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for cli_ih-0.7.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 c9d9bbdf10db5daf6e851fbacecd5883cc2084de554cea11ced8f183d2f70f92
MD5 8e88bb99ae969f311af25a8d55336833
BLAKE2b-256 44b7c44870d81c4b08e5de45388628b2ad831ab628d820ae493855bbe043bbbf

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