Files
OpenBB/cli/openbb_cli/argparse_translator/utils.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

65 lines
2.4 KiB
Python

"""Utilities for argparse_translator module."""
from argparse import Action, ArgumentParser
def in_group(parser: ArgumentParser, argument_name: str, group_title: str) -> bool:
"""Check if an argument is in a group of an ArgumentParser."""
for action_group in parser._action_groups:
if action_group.title == group_title:
for action in action_group._group_actions:
opts = action.option_strings
if (opts and opts[0] == argument_name) or action.dest == argument_name:
return True
return False
def remove_argument(parser: ArgumentParser, argument_name: str) -> list[str | None]:
"""Remove an argument from an ArgumentParser."""
groups_w_arg = []
for action in parser._actions:
opts = action.option_strings
if (opts and opts[0] == argument_name) or action.dest == argument_name:
parser._remove_action(action)
break
for action_group in parser._action_groups:
for action in action_group._group_actions:
opts = action.option_strings
if (opts and opts[0] == argument_name) or action.dest == argument_name:
action_group._group_actions.remove(action)
groups_w_arg.append(action_group.title)
parser._option_string_actions.pop(f"--{argument_name}", None)
return groups_w_arg
def get_argument_choices(parser: ArgumentParser, argument_name: str) -> tuple:
"""Get the choices of an argument from an ArgumentParser."""
for action in parser._actions:
opts = action.option_strings
if (opts and opts[0] == argument_name) or action.dest == argument_name:
return tuple(action.choices or ())
return ()
def get_argument_optional_choices(parser: ArgumentParser, argument_name: str) -> bool:
"""Get the optional_choices attribute of an argument from an ArgumentParser."""
for action in parser._actions:
opts = action.option_strings
if (
(opts and opts[0] == argument_name)
or action.dest == argument_name
and hasattr(action, "optional_choices")
):
return action.optional_choices # ty: ignore[unresolved-attribute]
return False
def set_optional_choices(action: Action, optional_choices: bool):
"""Set the optional_choices attribute of an action."""
if not hasattr(action, "optional_choices") and optional_choices:
setattr(action, "optional_choices", optional_choices)