Skip to main content

Yet another library for creating interactive CLI menus in Python

Project description

yacli_menu

yacli_menu simplifies the creation of CLI menus and interactive command-line interfaces. It allows you to define commands, prompt the user for input, and execute the chosen command.

Installation

pip install yacli_menu

Example

# file: example.py
from yacli_menu import command, run_menu

@command("Add Numbers")
def add():
    a = float(input("Enter first number: "))
    b = float(input("Enter second number: "))
    print(f"Result: {a + b}")

@command("Subtract Numbers")
def subtract():
    a = float(input("Enter first number: "))
    b = float(input("Enter second number: "))
    print(f"Result: {a - b}")

@command("Multiply Numbers")
def multiply():
    a = float(input("Enter first number: "))
    b = float(input("Enter second number: "))
    print(f"Result: {a * b}")

if __name__ == "__main__":
    run_menu() 
$ python example.py
1. Add Numbers
2. Subtract Numbers
3. Multiply Numbers
Enter command: 1
...

You can find more examples in the examples directory.

API Reference

@command(name: str)

Decorator that registers a function as a menu command.

Parameters:

  • name (str): The name that will be displayed in the menu for this command

The decorated function should take no arguments.

@command("Your command name")
def your_function():
    # ... command implementation ...
    pass

run_menu(**kwargs)

Starts the interactive menu loop.

Parameters:

  • prompt: (str, optional) - Input prompt text. Defaults to "Enter choice: ";
  • show_menu_on_invalid: (bool, optional) - Re-display menu if the user inserts an invalid option. Defaults to False;
  • show_menu_on_empty: (bool, optional) - Re-display menu after empty input. Defaults to False;
  • item_formatter: (Callable[[str, str], str], optional) - Function to format menu items. Defaults to "{order}. {name}";
  • order_formatter: (Callable[[int], str], optional) - Function to format menu item numbers. By default returns the string representation of the number;
  • order_start: (int, optional) - Starting number for menu items. Defaults to 1;
  • exit_order_last: (bool, optional) - If this option is True, the exit command will be Place exit option last instead of 0. Defaults to False;
  • exit_name: (str, optional) - Text for exit option. Defaults to "Exit";
# Basic usage
run_menu()

# Customized menu
run_menu(
    prompt="> ",
    show_menu_on_invalid=True,
    item_formatter=lambda o, n: f"{o}) {n}",
    exit_name="Quit",
)

Contributing

Contributions to yacli_menu are welcome! If you find issues or have suggestions for improvements, please open an issue or submit a pull request.

License

Licensed under the Apache License 2.0, Copyright © 2023-present afonsocrg

See LICENSE for more information.

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

yacli_menu-0.0.1.tar.gz (13.1 kB view details)

Uploaded Source

Built Distribution

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

yacli_menu-0.0.1-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file yacli_menu-0.0.1.tar.gz.

File metadata

  • Download URL: yacli_menu-0.0.1.tar.gz
  • Upload date:
  • Size: 13.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.13.1

File hashes

Hashes for yacli_menu-0.0.1.tar.gz
Algorithm Hash digest
SHA256 d409973fa81736a9d31bfc0ef02ae67860d7d7b0dbd0f4ed3f6904d0fe9ce7bd
MD5 0ebd22710354a3967fbe2ce58c48190e
BLAKE2b-256 fe51e96252634df627058687016a59a165c7019634304a0c78f1ed389f8b7eb5

See more details on using hashes here.

File details

Details for the file yacli_menu-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: yacli_menu-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 11.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.13.1

File hashes

Hashes for yacli_menu-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 794ba109b6b2458dfb43c6b3b93e637ddc7aae712a81ee0f44c3571cafe0c321
MD5 6ea18773cc37eff60ed6bd9ffd03c614
BLAKE2b-256 217cddc286d85e723ab9a77c53af87e452ae82b28dd06045a3a4f8ce70f84465

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