Skip to main content

Declaratively connect cli and config definition using pydantic

Project description

clonf

Declaratively connect cli and config definition using pydantic.

Why another?

There are a lot of tools out there which try to bring cli libraries and pydantic together, but they all seem to forget that more sophisticated applications need to be controlled via configuration files as well. clonf tries to solve this problem by focusing on compatibility with pydantic-settings and its configuration sources. This allows having a single source of truth for cli and configuration definition. clonf uses composition rather than inheritance and utilizes logic from pydantic and pydantic-settings to achieve this with as much simplicity and flexibility as possible. First versions focus on integration with click, but other cli libraries might receive interfaces as well in the future.

Key differences to other pydantic x cli libraries

  • CLI behavior is opt-in.
  • As much as possible is done via annotations. Combined with pydantic best practices, this encourages creating a single source of truth inside your codebase.

Installation

clonf can be installed via pip or your favorite python package manager with different extras:

pip install clonf[all,click,settings]

Creating a CLI

click

Quickstart

from pydantic import BaseModel
from clonf import clonf_click, CliArgument, CliOption
from typing import Annotated
import click


class Arguments(BaseModel):
    name: Annotated[str, CliArgument()]


class Options(BaseModel):
    greeting: Annotated[str, CliOption()] = "Hello"


@click.command
@clonf_click
def cli(arguments: Arguments, options: Options) -> None:
    click.echo(f"{options.greeting} {arguments.name}")


if __name__ == "__main__":
    cli()

Using click types

Similar to pydanclick, the following types will be converted automatically:

Python type Click type
bool click.BOOL
str click.STRING
int click.INT
float click.FLOAT
Annotated[int, Field(lt=..., ge=...) click.IntRange()
Annotated[float, Field(lt=..., ge=...) click.FloatRange()
pathlib.Path click.Path()
uuid.UUID click.UUID
datetime.datetime, datetime.date click.DateTime()
Literal click.Choice

Additionally, custom click types can be passed via annotations to have finer control over the resulting click type:

from pydantic import BaseModel
from typing import Annotated
from clonf import CliArgument
import pathlib
import click

class Config(BaseModel):
    file_path: Annotated[pathlib.Path, CliArgument(), click.Path(exists=True)]

Contributing

This project uses PDM as a package manager and pre-commit for linting before commits.

Read more about the tools and how to use them:

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

clonf-0.1.0.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

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

clonf-0.1.0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file clonf-0.1.0.tar.gz.

File metadata

  • Download URL: clonf-0.1.0.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.24.1 CPython/3.13.3 Linux/6.11.0-1012-azure

File hashes

Hashes for clonf-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8aa76dc394552e8d6db6d6edab7f0b94cdcf0785e867a175c4375e2e471ed18b
MD5 76e8e6ff1b852a8b8b2cf0bb0d6f1775
BLAKE2b-256 eaea55e35f83d5e8bf9145742a824419717d10f51cdeb398787aaa81f4212589

See more details on using hashes here.

File details

Details for the file clonf-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: clonf-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.24.1 CPython/3.13.3 Linux/6.11.0-1012-azure

File hashes

Hashes for clonf-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8fb0ccd90a277ce4b3f859ab4a5ca2a477fd25008eb12988b345f431b8638958
MD5 4b15214a5a8689049832c1bf1572172a
BLAKE2b-256 5bd106b4b7b5b8971d3a43b818801b813bf50e1d7d86692cc341c2e404489472

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