How do I create an argument parser (argparse.ArgumentParser
) from a Pydantic model?
I have a Pydantic model:
from pydantic import BaseModel, Field
class MyItem(BaseModel):
name: str
age: int
color: str = Field(default="red", description="Color of the item")
And I want to create an instance of MyItem
using command line:
python myscript.py --name Jack --age 10 --color blue
This should yield to:
item = MyItem(name="Jack", age=10, color="blue")
... # Process the item
I would not like to hard-code the command-line arguments and I would like to create the command-line arguments dynamically from the Pydantic model.