Skip to content

CLI integration with existing parser is broken. #654

@braindevices

Description

@braindevices

version >=2.10 the additional argument cannot be add to the parser

test script:

from pydantic_settings import CliApp, CliSettingsSource, SettingsConfigDict, BaseSettings
import pydantic_settings
import argparse
class CLISettings(BaseSettings):
    model_config = SettingsConfigDict(
        case_sensitive=True,
        cli_kebab_case=True,
        cli_parse_args=True,
        cli_implicit_flags=True
    )

    test: str
    

def parse_args():
    print(f"{pydantic_settings.VERSION=}")
    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawDescriptionHelpFormatter,
        description=__doc__
    )
    cli_settings = CliSettingsSource(CLISettings, root_parser=parser)
    parser.add_argument(
        "-v","--verbose",
        dest='verbose',
        default=0,
        action="count",
    )
    parsed_args = parser.parse_args()
    print(f"{parsed_args=}")
    settings = CliApp.run(
        CLISettings,
        cli_args=parsed_args,
        cli_settings_source=cli_settings
    )
    return settings

if __name__ == "__main__":
    parse_args()

here is result:

+ exec python test-pydantic.py -h
pydantic_settings.VERSION='2.10.0'
usage: test-pydantic.py [-h] [--test str]

options:
  -h, --help  show this help message and exit
  --test str  (required)

+ exec python test-pydantic.py --test hello -vvv
pydantic_settings.VERSION='2.10.0'
usage: test-pydantic.py [-h] [--test str]
test-pydantic.py: error: unrecognized arguments: -vvv

expecting result as <2.10

+ exec python test-pydantic.py -h
pydantic_settings.VERSION='2.9.1'
usage: test-pydantic.py [-h] [--test str] [-v]

options:
  -h, --help     show this help message and exit
  --test str     (required)
  -v, --verbose

+ exec python test-pydantic.py --test hello -vvv
pydantic_settings.VERSION='2.9.1'
parsed_args=Namespace(verbose=3, test='hello')

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions