Skip to content

Use a pydantic model to define the command's optionsΒ #10

@choucavalier

Description

@choucavalier

In the case where I have something like this

class MyParams(BaseModel):
  foo: int
  bar: str


def main(params: MyParams):
    typer.echo(f"{params.foo}")

if __name__ == "__main__":
    pydantic_typer.run(main)

I would like to generate a CLI with arguments --foo and --bar directly instead of --params.foo and --params.bar.

In other words, I would like to define the set of parameters externally, using a pydantic model.

Is this possible?

The reason why I want to do that is that I have a bunch of BaseModel that define the arguments of multiple commands. And I would like to automatically generate the CLI based an these pydantic models.

A second question then is: how can I programmatically generate CLI commands with arguments defined by pydantic models?

Thanks a lot for your work on this extension!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions