Files
OpenBB/cli/openbb_cli/argparse_translator/argparse_argument.py
Danglewood 9a30186174 [V5] CLI 2.0 - Pluggable Backends, Non-TTY Default, HTTP Dispatcher, Spec Files + Codegen (#7433)
* stash some changes

* add more robust testing

* mypy

* point PR at V5

* introduce spec file

* codespell

* test fix

* fix workflow environment setup

* fix workflow environment setup

* fix workflow environment setup

* add pyyaml to dependencies

* split lint jobs

* fix workflow environment setup

* fix workflow environment setup

* workflow env setup

* workflow env setup

* clean up code comments

* add auth hook entrypoints

* codespell

* add codegen feature

* codespell

* move _unpack into dispatchers for consistency with codegen packages

* surface nested models in the response

* fix missing coverage in CI

* socrata updates

* test fix

* detect plotly output

* add --include and --exclude flags from generate-extension command

* cap test matrix at python 3.14

* no useless comments

* platform controller command description split

* merge URL overloads from path params

* exclude none and unset from model dump

---------

Co-authored-by: deeleeramone <>
Co-authored-by: Copilot <copilot@github.com>
2026-06-01 19:14:38 +03:00

59 lines
1.6 KiB
Python

"""Pydantic models for argparse arguments and argument groups."""
from typing import (
Any,
Literal,
)
from pydantic import BaseModel, model_validator
SEP = "__"
class ArgparseArgumentModel(BaseModel):
"""Pydantic model for an argparse argument."""
name: str
type: Any
dest: str
default: Any
required: bool
action: Literal["store_true", "store"]
help: str | None
nargs: Literal["+"] | None
choices: tuple | None
@model_validator(mode="after") # ty: ignore[invalid-argument-type]
@classmethod
def validate_action(cls, values: "ArgparseArgumentModel"):
"""Validate the action based on the type."""
if values.type is bool and values.action != "store_true":
raise ValueError('If type is bool, action must be "store_true"')
return values
@model_validator(mode="after") # ty: ignore[invalid-argument-type]
@classmethod
def remove_props_on_store_true(cls, values: "ArgparseArgumentModel"):
"""Remove type, nargs, and choices if action is store_true."""
if values.action == "store_true":
values.type = None
values.nargs = None
values.choices = None
return values
def model_dump(self, **kwargs):
"""Override the model_dump method to remove empty choices."""
res = super().model_dump(**kwargs)
if "choices" in res and not res["choices"]:
del res["choices"]
return res
class ArgparseArgumentGroupModel(BaseModel):
"""Pydantic model for a custom argument group."""
name: str
arguments: list[ArgparseArgumentModel]