standard
Parses Standard Pydantic Fields to Command-Line Arguments.
The standard
module contains the parse_field
function, which parses
standard pydantic
model fields to ArgumentParser
command-line arguments.
Unlike the other parser
modules, the standard
module does not contain a
should_parse
function. This is because it is the fallback case, where fields
that do not match any other types and require no special handling are parsed.
parse_field(parser, field)
Adds standard pydantic field to argument parser.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
parser |
argparse.ArgumentParser |
Argument parser to add to. |
required |
field |
pydantic.fields.ModelField |
Field to be added to parser. |
required |
Returns:
Type | Description |
---|---|
Optional[utils.pydantic.PydanticValidator] |
Possible validator method. |
Source code in pydantic_argparse/parsers/standard.py
def parse_field(
parser: argparse.ArgumentParser,
field: pydantic.fields.ModelField,
) -> Optional[utils.pydantic.PydanticValidator]:
"""Adds standard pydantic field to argument parser.
Args:
parser (argparse.ArgumentParser): Argument parser to add to.
field (pydantic.fields.ModelField): Field to be added to parser.
Returns:
Optional[utils.pydantic.PydanticValidator]: Possible validator method.
"""
# Add Standard Field
parser.add_argument(
utils.arguments.name(field),
action=argparse._StoreAction,
help=utils.arguments.description(field),
dest=field.alias,
metavar=field.alias.upper(),
required=bool(field.required),
)
# Construct and Return Validator
return utils.pydantic.as_validator(field, lambda v: v)