Skip to main content

A customizable interactive CLI menu dispatcher with extendable return signals and matching function calls.

Project description

Interactive Dispatcher

Interactive Dispatcher is a lightweight and customizable command-line menu system that allows you to define interactive options with corresponding return signals and handler functions. It is especially useful for CLI tools, REPLs, configuration managers, or anything needing a user-driven menu interface.


Features

  • Customizable option prompts
  • Signal-returning dispatch mechanism
  • Easy-to-extend architecture
  • Supports looped interactions
  • Clean, readable, and minimal dependency footprint
  • Add your own signals to finely control the logic flow

Installation

pip install interactive-dispatcher

Example

# example.py
from interactive_dispatcher import Action, MenuItem, ReturnSignal, interactive_dispatcher

# Register a custom signal
ReturnSignal.register("RESTART")

# Sample functions
def greet(name: str) -> str:
    print(f"Hello, {name}!")
    return f"Greeted {name}"

def log(msg: str) -> str:
    print(f"[Log] {msg}")
    return "Logged"

def exit_program() -> str:
    print("Goodbye!")
    return "Exited"

# Menu configuration
menu: list[MenuItem] = [
    MenuItem(
        keys=["greet", "g"],
        description="Greet a user and log the action",
        actions=[
            Action(function=greet, args=("Alice",)),
            Action(function=log, args=("Greeting sent to Alice",))
        ],
        return_signal=ReturnSignal.CONTINUE
    ),
    MenuItem(
        keys=["restart", "r"],
        description="Restart the application",
        actions=[
            Action(function=log, args=("Restarting...",))
        ],
        return_signal=ReturnSignal.RESTART
    ),
    MenuItem(
        keys=["exit", "e", "q"],
        description="Exit the program",
        actions=[
            Action(function=exit_program)
        ],
        return_signal=ReturnSignal.BREAK
    )
]

# Dispatcher loop
while True:
    results, signal = interactive_dispatcher("Choose an option:", menu)
    print(f"Results: {results}")
    print(f"ReturnSignal: {signal}")

    if signal == ReturnSignal.BREAK:
        break
    elif signal == ReturnSignal.RESTART:
        print("Restarting...\n")

Output

alt text


Purpose

Interactive Dispatcher abstracts away repetitive CLI input logic and allows developers to:

  • Focus on logic instead of input parsing
  • Use readable "signals" to handle control flow
  • Easily integrate menus inside other CLI tools

Customization

You can fully customize menu prompts, signals, or plug the dispatcher into larger CLI apps.

With Callable Handlers

from interactive_dispatcher import InteractiveDispatcher

def greet():
    print("Hello!")

def farewell():
    print("Goodbye!")

dispatcher = InteractiveDispatcher(
    title="Greeting Menu",
    options={
        "Say Hello": greet,
        "Say Goodbye": farewell,
        "Exit": "exit"
    }
)

while True:
    signal = dispatcher.run()
    if signal == "exit":
        break

Return Signal Philosophy

Instead of tightly coupling option text and logic, each option can return a signal string which you can route freely in your application logic.

This approach gives you:

  • Flexibility
  • Testability
  • Clean separation of concerns

Use Cases

Use cases are mostly limited to CLI driven interfaces where you want something light, easy and maintainable.

  • Interactive CLI tools and wrappers
  • Shell-like applications
  • Configuration or wizard interfaces
  • Automation Scripts

Advantages

  • Simple interface with powerful control
  • Works seamlessly in loops
  • Easily composable with your business logic
  • Minimal code makes it easy to test

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

interactive-dispatcher-1.0.1.tar.gz (13.2 kB view details)

Uploaded Source

Built Distribution

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

interactive_dispatcher-1.0.1-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

Details for the file interactive-dispatcher-1.0.1.tar.gz.

File metadata

  • Download URL: interactive-dispatcher-1.0.1.tar.gz
  • Upload date:
  • Size: 13.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.6

File hashes

Hashes for interactive-dispatcher-1.0.1.tar.gz
Algorithm Hash digest
SHA256 1085de5330499705bf86c4b8bde762cdb82feb08e9b0fdcbff8629a2e5644717
MD5 cbf7299b015b426e6beec42f9cd12820
BLAKE2b-256 513b441cf6fe1ef12678942e781af8bfd93673970c61281da65c9cb078d4c63f

See more details on using hashes here.

File details

Details for the file interactive_dispatcher-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for interactive_dispatcher-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 da3d431c40e6a19e174edc21cda8108d2f2d7ced88f69fb50ba238f4ac135747
MD5 b71b3763f6edf40ab1752ce52986004b
BLAKE2b-256 94575395d7cecbda575e82c1545754061bfdd138fd52d38713a5b71c335d30df

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