Skip to content

Support default values for CliPositionalArg when parsing from the CLI #517

Closed
@jploskey

Description

@jploskey

Currently, there doesn't appear to be a way to have an optional positional argument when parsing settings from the CLI. This is a blocker for me, trying to migrate over from typer.

Running the following will result in this error: pydantic_settings.sources.SettingsError: positional argument BaseMainCommand.value has a default value

from pydantic_settings import BaseSettings, CliPositionalArg, SettingsConfigDict


class Main(BaseSettings):
    model_config = SettingsConfigDict(
        cli_parse_args=True,
        cli_enforce_required=True,
    )

    value: CliPositionalArg[int | None] = None


parsed = Main() # Error is raised here.
print(parsed.value)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions