carmine
The no-boilerplate CLI library powered by Python's introspection.
pip install sh40-carmine
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)
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)
examples
Metadata
Release files for sh40-carmine 0.3.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| sh40_carmine-0.3.5.tar.gz | 9.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| sh40_carmine-0.3.5-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 20.8 kB
Release files / sh40_carmine-0.3.5.tar.gz
| Download URL | sh40_carmine-0.3.5.tar.gz |
|---|---|
| Size | 9.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c54f3d79426d9939288031a1b72b868be8fc1eb4e8dd0119d57f2af899001589
|
|
BLAKE2b-256 checksum How to use checksums |
488e51f827ef52a0c091143e6f9a2d131343ffd48ee85b3b1b58d5e87e888af0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-httpx/0.23.3
|
Release files / sh40_carmine-0.3.5-py3-none-any.whl
| Download URL | sh40_carmine-0.3.5-py3-none-any.whl |
|---|---|
| Size | 11.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
31eebcf862c2070966ef7aa236ce62eb8f14f336778c79b9b89f93750aa1b292
|
|
BLAKE2b-256 checksum How to use checksums |
830fd1fc0c154361ab4d217df7ba568fc6982358f86ae3e63134946e7e08ebd4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-httpx/0.23.3
|