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 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, and exit commands
  • 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=logger to the InputHandler to enable logging (this will be removed soon)
  • You can provide the thread_mode param to the InputHandler class to set if it shoud run in a thread or no. (If you are using the cli-ih module on its own without any other background task set thread_mode=False to false)
  • You can also provide a cursor param to the InputHandler class to set the cli cursor (default cusor is empty)

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.0.tar.gz (7.4 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.0-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: cli_ih-0.7.0.tar.gz
  • Upload date:
  • Size: 7.4 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.0.tar.gz
Algorithm Hash digest
SHA256 c88b9815f4a8307e2996c003b3795a4c5123b11e8a31128d23ed730824cb2edc
MD5 bbf72e21f81e05faf2284a8b59d51af5
BLAKE2b-256 54b5f4da2f610ea4ccea99f5d41e803ec7d48ad370c7f4f28443e991caa1d8da

See more details on using hashes here.

File details

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

File metadata

  • Download URL: cli_ih-0.7.0-py3-none-any.whl
  • Upload date:
  • Size: 9.1 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 34cafd58956d3769bd0e4a5ee4522651ff5abdf0e44504ca5f3c4936843a36bf
MD5 7497fc34dfe5a5f06030b0b1a59c8807
BLAKE2b-256 3bb9c260d97d6ae00508906766e7a2b3a5bcb3b3e41d94656d9477da683a8127

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