Skip to main content

Yet another way to turn your pydantic model into console application.

Project description

yappy

Yet another way to turn your pydantic model into click console application.

What in the box?

Installation

pip install yappy

Usage

# content of newuser.py

import enum

import click
from yappy import options_from_model
from pydantic import BaseModel, Field


class UserRole(str, enum.Enum):
    PLAIN = "plain"
    ADMIN = "admin"


class NewUserModel(BaseModel):
    name: str = Field(..., description="New user name")
    role: UserRole = UserRole.PLAIN
    blocked: bool = True


@click.command()
@options_from_model(NewUserModel)
def create_new_user(name: str, role: UserRole, blocked: bool):
    """Trying to create new user."""
    click.echo(f"Create new {'inactive' if blocked else 'active'} user...")
    click.echo(f"With username {name} and {role} role...")
    click.echo(f"Fail.")


if __name__ == '__main__':
    create_new_user()
$ python newuser.py --name Pancake --role admin --no-blocked
Create new active user...
With username Pancake and admin role...
Fail.
$ python newuser.py --help
Usage: newuser.py [OPTIONS]

  Trying to create new user.

Options:
  --name TEXT               New user name  [required]
  --role [plain|admin]      [default: plain]
  --blocked / --no-blocked  [default: no-blocked]
  --help                    Show this message and exit.

Sequences

Tuple fields with fixed elements count turns into multi value options.

All other sequences (except str, of course) turns into multiple options.

Mappings

For now mappings turns into mix of multi value and multiple options - every option contains key and value pair and can repeat. Example:

$ python cli.py --dict-field key1 value1 --dict-field key2 value2

produce dict:

{
    "key1": "value1",
    "key2": "value2",
}

Unions

Union supports only same elements count, e.g. Union[tuple[str, bool], tuple[str, int]] are valid type, but Union[tuple[str, bool], tuple[str]] are not.

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

yappy-0.0.2.tar.gz (8.7 kB view hashes)

Uploaded Source

Built Distribution

yappy-0.0.2-py3-none-any.whl (7.8 kB view hashes)

Uploaded Python 3

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