Skip to main content

The no-boilerplate CLI library powered by Python's introspection.

Project description

carmine

carmine

The no-boilerplate CLI library powered by Python's introspection.

pip install sh40-carmine

rule

the context

CLIs (command line interfaces) are one of Python's longest standing issues. The builtin solution, argparse, is hard to understand & reason about, and doesn't even follow Python's own conventions in multiple places.

Alternative solutions often feel redundant and force you to define arguments twice - once in the function's definition, and another time as an CLI option.

Turns out, there's a better solution

the solution

carmine reads your functions signatures directly, removing the need to manually define option types. It also (optionally) uses type hints to automatically convert arguments given to the CLI to the types you expect them to be. All this is done with no modification to the syntax!

"""My Carmine app."""

import sys
from carmine import carmine_cli, Choice

def hello(name: str, *, age: int) -> None:
    """Says hello!

    Args:
        name: Your name.
        age: Your age.
    """

    print(f"Hello {name}, are you {age} years old?")

def goodbye(name: str, duration: Choice("for now", "forever")) -> None:
    """Says goodbye!

    Args:
        name: Your name.
        age: How long you're gonna leave for.
    """

    print(f"It's been nice knowing you, {name}!", end="")

    if duration == "for now":
        print("See you soon!")
    else:
        print("Eh, you'll be back soon enough.")

def main(argv: list[str] | sys.argv) -> None:
    """Runs the application."""

    with carmine_cli(__doc__, argv) as register:
        register(hello, goodbye)

rule

single-command mode

Not every app needs to have multiple commands. If your app only registers one thing, carmine will no longer require a command selection, which keeps things cleaner.

"""My (simple) Carmine app.

This docstring won't be used in the help, as the sole command's overrules it.
"""

import sys
from carmine import carmine_cli, Choice


def multiply(a: int, b: int, *, power: bool = False) -> None:
    """Multiplies two numbers.

    Args:
        a: The first number.
        b: The second number.
        power: Interpret `b` as a power, rather than a factor.
    """

    result = a ** b if power else a * b
    print(result)

def main(argv: list[str] | sys.argv) -> None:
    """Runs the application."""

    with carmine_cli(__doc__, argv) as register:
        register(multiply)

rule

examples

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

sh40_carmine-0.3.2.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

sh40_carmine-0.3.2-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file sh40_carmine-0.3.2.tar.gz.

File metadata

  • Download URL: sh40_carmine-0.3.2.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.23.3

File hashes

Hashes for sh40_carmine-0.3.2.tar.gz
Algorithm Hash digest
SHA256 cc08df562ca1cdc9bff59fc9a144d18e0a549c41cbb3df9faa3558ebeaa860c8
MD5 ef094f43c6ad0f1cea4de56edb7c9ed9
BLAKE2b-256 5e3f6281c535a458d0e3abdd8d11e08f264f21bcf37aae58f97648efae29f16f

See more details on using hashes here.

File details

Details for the file sh40_carmine-0.3.2-py3-none-any.whl.

File metadata

File hashes

Hashes for sh40_carmine-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7c08bd9b220fe55b98022af561ad27c310fb9f58f6592a1615837d127e213633
MD5 b6ce2f47cb202ea006395c46c0276023
BLAKE2b-256 f0ada787c78d3af9140df90bf19491e830d5c32d0e7cc4d5cf6282b1068b0798

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page