0

Based on the official pydantic-CLI docs I created the following CommandLineArguments-class from the base class "BaseModel":

from pydantic import BaseModel, Field, parse_obj_as
from typing import List, Optional

class CommandLineArguments(BaseModel):
    """Command Line Arguments."""
    config_files: Optional[List[str]] = Field(
        [],
        description='Paths to the config files that should be validated',
        cli=('--config-files', ),
    )
    with_log: Optional[bool] = Field(
        False,
        description='Enable logging ouput',
        cli=('--with-log', '--no-log'),
    )
    stacktrace: Optional[bool] = Field(
        False,
        description='Enable stack trace output',
        cli=('--with-stacktrace', '--no-stacktrace'),
    )

In the aforementioned docs, there is a Limitations-section saying:

limits on lists

Indeed, when executing the command-line script, the --config-files - part always fails since following file name(s) cannot be parsed/recognized as a list of strings:

❯ python src/scripts/config_validation.py --config-files src/config/some.json --with-log --with-stacktrace


... Starting config validation script

usage: config_validation.py [--config-files CONFIG_FILES [CONFIG_FILES ...]]
                            [--with-log] [--with-stacktrace] [--help]
                            [--version]

Status (2) Failed to run command config_validation.py: error: unrecognized arguments: --config-files src/config/some.json
  File "C:\Users\username\Projects\proj-venv\lib\site-packages\pydantic_cli\__init__.py", line 481, in _runner
    pargs = parser.parse_args(args)
  File "C:\Users\username\.pyenv\pyenv-win\versions\3.7.9\lib\argparse.py", line 1758, in parse_args
    self.error(msg % ' '.join(argv))
  File "C:\Users\username\.pyenv\pyenv-win\versions\3.7.9\lib\argparse.py", line 2508, in error
    self.exit(2, _('%(prog)s: error: %(message)s\n') % args)
  File "C:\Users\username\Projects\proj-venv\lib\site-packages\pydantic_cli\argparse.py", line 75, in exit
    status, f"Status ({status}) Failed to run command {message}"

By contrast, with argparse this just works out of the box with some additional parameter to be set.

Is there something I forgot or need to do differently in my implementation above?

config_files: Optional[List[str]] = Field(
    [],
    description='Paths to the config files that should be validated',
    cli=('--config-files', ),
)

EDIT (new approaches):

Based on this question on StackOverFlow and this GitHub issue, I tried the following with no avail either:

from typing import List, Optional
from pydantic import BaseModel, Field
from pydantic_cli import run_and_exit


class ConfigFile(BaseModel):
    config_file: str


class ConfigFiles(BaseModel):
    __root__: List[ConfigFile]


class CommandLineArguments(BaseModel):
    """Command Line Arguments."""
    config_files: Optional[ConfigFiles] = None
    with_log: Optional[bool] = Field(
        False,
        description='Enable logging ouput',
        cli=('--with-log', '--no-log'),
    )
    stacktrace: Optional[bool] = Field(
        False,
        description='Enable stack trace output',
        cli=('--with-stacktrace', '--no-stacktrace'),
    )
Andreas L.
  • 3,239
  • 5
  • 26
  • 65

0 Answers0