Cappa
Cappa
- Full documentation here.
- Comparison vs existing libraries..
- Annotation inference details
- “invoke” (click-like) details
Cappa is a declarative command line parsing library, taking much of its inspiration from the “Derive” API from the Clap written in Rust.
from dataclasses import dataclass, field
import cappa
from typing import Literal
from typing_extensions import Annotated
@dataclass
class Example:
positional_arg: str = "optional"
boolean_flag: bool = False
single_option: Annotated[int | None, cappa.Arg(short=True, help="A number")] = None
multiple_option: Annotated[
list[Literal["one", "two", "three"]],
cappa.Arg(long=True, help="Pick one!"),
] = field(default_factory=list)
args: Example = cappa.parse(Example, backend=cappa.backend)
print(args)
Produces the following CLI:
In this way, you can turn any dataclass-like object (with some additional annotations, depending on what you’re looking for) into a CLI.
...