Skip to content

Allow variadic CliPositionalArg to accept an empty list, or have a default value #524

@jploskey

Description

@jploskey

Thank you so much for implementing #519!

However, i've pulled down the changes for testing, and did notice that there doesn't seem to be a way to have a command accept an arbitrary number of positional arguments that can be empty.

# File: example.py

from pydantic_settings import BaseSettings, CliPositionalArg, SettingsConfigDict


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

    values: CliPositionalArg[list[str]]


parsed = Main()
print(parsed)

Output when running as python example.py:

usage: that.py [-h] VALUES
example.py: error: the following arguments are required: VALUES

So, to work around this I've tried using both values: CliPositionalArg[list[str]] = [] and values: CliPositionalArg[list[str] | None] = None instead, but both result in pydantic_settings.sources.SettingsError: positional argument Main.values has a default value.

My use case is similar to a command like docker compose up, where when no positional arguments are passed there's different behavior.

docker compose up # Run all services.
docker compose up my-app postgres # Run specific services.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions