from __future__ import annotations
from typing import Any, Dict, List, Optional, Union
import typer
from rich import print
from rich.markup import escape
# Shared CLI argument for specifying a study key
STUDY_KEY_ARG = typer.Argument(..., help="The key identifying the study.")
[docs]def parse_filter_args(filter_args: Optional[List[str]]) -> Optional[Dict[str, Any]]:
"""Parse a list of ``key=value`` strings into a dictionary."""
if not filter_args:
return None
filter_dict: Dict[str, Union[str, bool, int]] = {}
for arg in filter_args:
if "=" not in arg:
print(
f"[bold red]Error:[/bold red] Invalid filter format: '{escape(arg)}'. "
"Use 'key=value'."
)
raise typer.Exit(code=1)
key, value = arg.split("=", 1)
if value.lower() == "true":
filter_dict[key.strip()] = True
elif value.lower() == "false":
filter_dict[key.strip()] = False
elif value.isdigit():
filter_dict[key.strip()] = int(value)
else:
filter_dict[key.strip()] = value
return filter_dict