Skip to content

arguments

Arguments Utility Functions for Declarative Typed Argument Parsing.

The arguments module contains utility functions used for formatting argument names and formatting argument descriptions.

name(field, invert=False)

Standardises argument name.

Parameters:

Name Type Description Default
field pydantic.fields.ModelField

Field to construct name for.

required
invert bool

Whether to invert the name by prepending --no-.

False

Returns:

Type Description
str

Standardised name of the argument.

Source code in pydantic_argparse/utils/arguments.py
def name(field: pydantic.fields.ModelField, invert: bool = False) -> str:
    """Standardises argument name.

    Args:
        field (pydantic.fields.ModelField): Field to construct name for.
        invert (bool): Whether to invert the name by prepending `--no-`.

    Returns:
        str: Standardised name of the argument.
    """
    # Construct Prefix
    prefix = "--no-" if invert else "--"

    # Prepend prefix, replace '_' with '-'
    return f"{prefix}{field.alias.replace('_', '-')}"

description(field)

Standardises argument description.

Parameters:

Name Type Description Default
field pydantic.fields.ModelField

Field to construct description for.

required

Returns:

Type Description
str

Standardised description of the argument.

Source code in pydantic_argparse/utils/arguments.py
def description(field: pydantic.fields.ModelField) -> str:
    """Standardises argument description.

    Args:
        field (pydantic.fields.ModelField): Field to construct description for.

    Returns:
        str: Standardised description of the argument.
    """
    # Construct Default String
    default = f"(default: {field.get_default()})" if not field.required else None

    # Return Standardised Description String
    return " ".join(filter(None, [field.field_info.description, default]))