Skip to content

command

Parses Nested Pydantic Model Fields to Sub-Commands.

The command module contains the should_parse function, which checks whether this module should be used to parse the field, as well as the parse_field function, which parses nested pydantic model fields to ArgumentParser sub-commands.

should_parse(field)

Checks whether the field should be parsed as a command.

Parameters:

Name Type Description Default
field pydantic.fields.ModelField

Field to check.

required

Returns:

Type Description
bool

Whether the field should be parsed as a command.

Source code in pydantic_argparse/parsers/command.py
def should_parse(field: pydantic.fields.ModelField) -> bool:
    """Checks whether the field should be parsed as a `command`.

    Args:
        field (pydantic.fields.ModelField): Field to check.

    Returns:
        bool: Whether the field should be parsed as a `command`.
    """
    # Check and Return
    return utils.types.is_field_a(field, pydantic.BaseModel)

parse_field(subparser, field)

Adds command pydantic field to argument parser.

Parameters:

Name Type Description Default
subparser argparse._SubParsersAction

Sub-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/command.py
def parse_field(
    subparser: argparse._SubParsersAction,
    field: pydantic.fields.ModelField,
) -> Optional[utils.pydantic.PydanticValidator]:
    """Adds command pydantic field to argument parser.

    Args:
        subparser (argparse._SubParsersAction): Sub-parser to add to.
        field (pydantic.fields.ModelField): Field to be added to parser.

    Returns:
        Optional[utils.pydantic.PydanticValidator]: Possible validator method.
    """
    # Add Command
    subparser.add_parser(
        field.alias,
        help=field.field_info.description,
        model=field.outer_type_,  # type: ignore[call-arg]
        exit_on_error=False,  # Allow top level parser to handle exiting
    )

    # Return
    return None