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 and input handling. It supports threaded input processing and includes enhanced logging with color-coded output.
Features
- Command registration system with descriptions
- Threaded or non-threaded input handling
- Colored logging with support for debug mode
- Built-in
help,debug, andexitcommands - Error handling for missing or invalid command arguments
- NEW: Register commands with decorators
Installation
pip install cli_ih
Quick Start
from cli_ih import InputHandler
def greet(args):
print(f"Hello, {' '.join(args)}!")
handler = InputHandler(cursor="> ")
# NEW
@handler.command(name="add", description="Performs the `+` operator on the first 2 arguments.") # The name param will use the func name if its not provided
def add(args):
print(int(args[0])+int(args[1]))
handler.register_command("greet", greet, "Greets the user. Usage: greet [name]")
handler.start()
# Now type commands like:
# > greet world
# Hello, world!
# > add 1 2
# 3
# > help
# Available commands:
# help: Displays all the available commands
# debug: If a logger is present changes the logging level to DEBUG.
# exit: Exits the Input Handler irreversibly.
# add: Performs the `+` operator on the first 2 arguments.
# greet: Greets the user. Usage: greet [name]
#
# > debug
# > exit
New Async client
import asyncio
from cli_ih import AsyncInputHandler
print(cli_ih.__version__)
handler = AsyncInputHandler(cursor="> ")
@handler.command(name="greet", description="Greets the user. Usage: greet [name]")
async def greet(name, *args):
await asyncio.sleep(1)
print(f"Hello, {name}{" " if args else ""}{' '.join(args)}!")
# NEW
@handler.command(name="add", description="Performs the `+` operator on the first 2 arguments.")
async def add(a, b):
print(a+b)
asyncio.run(handler.start())
Additional Info
- You can provide a valid logger
logger=loggerto theInputHandlerto enable logging (this will be removed soon) - You can provide the
thread_modeparam to theInputHandlerclass to set if it shoud run in a thread or no. (If you are using thecli-ihmodule on its own without any other background task setthread_mode=Falseto false) - You can also provide a
cursorparam to theInputHandlerclass to set the cli cursor (default cusor is empty)
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
cli_ih-0.6.2.1.tar.gz
(4.9 kB
view details)
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 cli_ih-0.6.2.1.tar.gz.
File metadata
- Download URL: cli_ih-0.6.2.1.tar.gz
- Upload date:
- Size: 4.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
33d8b921c3cfb4266c344618533d900e6a1c1f7ab68df163e346f8f51ae0dfee
|
|
| MD5 |
1201dcec7193f7cfbf99d67aba9fb39b
|
|
| BLAKE2b-256 |
e31352e5c5e5faa6c40fc52881cd6750d5aa97372fb5b9f940e8fb84fcb05f48
|
File details
Details for the file cli_ih-0.6.2.1-py3-none-any.whl.
File metadata
- Download URL: cli_ih-0.6.2.1-py3-none-any.whl
- Upload date:
- Size: 7.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ccf417a5f577a9c5e071a3494127cb45d17130101c5eacd95eebea0da225746f
|
|
| MD5 |
f87f19b0e113499df6359e912d02ca75
|
|
| BLAKE2b-256 |
a30e7b9577b5ac73e1a101201e918708a461b415939b9c28cb18bb185826ba8a
|