Wrapper around Typer to create command line interfaces for Pydantic models
Project description
CoTyper
Inspired by Typer and Pydantic. CoTyper since you add a lot of co in category theory.
CoTyper is a wrapper around Typer but with support for (nested) pydantic models as function arguments.
โ Note this project is in early development
Install
Install the package via pip:
pip install cotyper
Or install the package via uv:
uv add cotyper
Build from Source
Clone the repository and build the package as wheel file
git clone https://gitlab.opencode.de/lgln/cotyper
cd cotyper
python -m build --wheel # or pip install .
๐งโ๐ป Examples
For the examples it is recommended to use the uv command line tool, but feel free to
build the environment with pip or poetry as well.
import datetime
from pydantic import BaseModel
from cotyper import App, unfold_cls
app = App()
class MetaData(BaseModel):
date: datetime.datetime = "2023-10-01T00:00:00"
class Config(BaseModel):
user_name: str
url: str
meta: MetaData = MetaData()
class Foo(BaseModel):
bar: str = "baz"
@app.struct_command("example")
@unfold_cls(Config) # this will unfold the Config model into command line options, hence, exposing the fields of the model itself
def example_command(config: Config, foo: Foo = Foo()):
"""
Example command that takes a nested pydantic model as argument.
Args:
config (Config): Configuration model with user_name, url, and meta data.
foo (Foo): Another model with a bar attribute.
"""
print(f"User: {config.user_name}, URL: {config.url}, Meta Date: {config.meta.date}")
print(f"Foo Bar: {foo.bar}")
if __name__ == "__main__":
app()
And requesting help will show the nested models as options:
uv run examples/readme.py --help
Usage: readme.py [OPTIONS]
โญโ Options โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ * --url TEXT [default: None] [required] โ
โ --install-completion Install completion for the current shell. โ
โ --show-completion Show completion for the current shell, to copy it or โ
โ customize the installation. โ
โ --help Show this message and exit. โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
โญโ User โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ * --user-name TEXT [default: None] [required] โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
โญโ Meta โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ --meta-date [%Y-%m-%d|%Y-%m-%dT%H:%M:%S|%Y-%m-%d [default: 2023-10-01 00:00:00] โ
โ %H:%M:%S] โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
โญโ Foo โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ --foo-bar TEXT [default: baz] โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ```
๐๏ธ Note, that the @unfold_cls(Config) decorator is used to unfold the Config model into
the fields of the model directly.
And running the command with the toy information provides
uv run examples/readme.py --url "pydantic.dev" --user-name "Monty Python"
User: Monty Python, URL: pydantic.dev, Meta Date: 2023-10-01 00:00:00
Foo Bar: baz
Extensive Examples
See examples/ for more examples. For example
uv run examples/extensive.py --help
๐งโ๐ฌ Tests
Run
uv run pytest
๐ ToDo
-[ ] documentation
๐ Contributing
See CONTRIBUTING.md for details on how to contribute to this project.
๐๏ธ License
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cotyper-0.7.6-py3-none-any.whl.
File metadata
- Download URL: cotyper-0.7.6-py3-none-any.whl
- Upload date:
- Size: 4.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1b754690f139b46c5195796a652ef8d8869678ec719e1c63c366f3a043b3874b
|
|
| MD5 |
89008a540935fb7d76b0cececf5ddbd9
|
|
| BLAKE2b-256 |
6f06aeba7d68a425b04236ca9578c56b6eb9eba4d1d7a604391f237a34ef3c98
|