f
This commit is contained in:
@@ -280,11 +280,87 @@ def get_cmdlet_arg_flags(cmd_name: str, config: Optional[Dict[str, Any]] = None)
|
||||
def get_cmdlet_arg_choices(
|
||||
cmd_name: str, arg_name: str, config: Optional[Dict[str, Any]] = None
|
||||
) -> List[str]:
|
||||
"""Return declared choices for a cmdlet argument."""
|
||||
"""Return declared choices for a cmdlet argument.
|
||||
|
||||
Special-cases dynamic choices for certain arguments (e.g., Matrix -room)
|
||||
which may be populated from configuration or provider queries.
|
||||
"""
|
||||
meta = get_cmdlet_metadata(cmd_name, config=config)
|
||||
if not meta:
|
||||
return []
|
||||
target = arg_name.lstrip("-")
|
||||
|
||||
# Dynamic handling for Matrix room choices
|
||||
try:
|
||||
canonical = (meta.get("name") or str(cmd_name)).replace("_", "-")
|
||||
except Exception:
|
||||
canonical = str(cmd_name)
|
||||
|
||||
if target == "room" and canonical in (".matrix", "matrix"):
|
||||
# Load default room IDs from configuration and attempt to resolve display names
|
||||
try:
|
||||
if config is None:
|
||||
from SYS.config import load_config
|
||||
|
||||
config = load_config()
|
||||
except Exception:
|
||||
config = config or {}
|
||||
|
||||
matrix_conf = {}
|
||||
try:
|
||||
providers = config.get("provider") or {}
|
||||
matrix_conf = providers.get("matrix") or {}
|
||||
except Exception:
|
||||
matrix_conf = {}
|
||||
|
||||
raw = None
|
||||
for key in ("room", "room_id", "rooms", "room_ids"):
|
||||
if key in matrix_conf:
|
||||
raw = matrix_conf.get(key)
|
||||
break
|
||||
ids: List[str] = []
|
||||
try:
|
||||
if isinstance(raw, (list, tuple, set)):
|
||||
ids = [str(v).strip() for v in raw if str(v).strip()]
|
||||
else:
|
||||
text = str(raw or "").strip()
|
||||
if text:
|
||||
import re
|
||||
|
||||
ids = [p.strip() for p in re.split(r"[,\s]+", text) if p and p.strip()]
|
||||
except Exception:
|
||||
ids = []
|
||||
|
||||
if ids:
|
||||
# Try to resolve names via Provider.matrix if config provides auth info
|
||||
try:
|
||||
hs = matrix_conf.get("homeserver")
|
||||
token = matrix_conf.get("access_token")
|
||||
if hs and token:
|
||||
try:
|
||||
from Provider.matrix import Matrix
|
||||
|
||||
try:
|
||||
m = Matrix(config)
|
||||
rooms = m.list_rooms(room_ids=ids)
|
||||
choices = []
|
||||
for r in rooms or []:
|
||||
name = str(r.get("name") or "").strip()
|
||||
rid = str(r.get("room_id") or "").strip()
|
||||
choices.append(name or rid)
|
||||
if choices:
|
||||
return choices
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# Fallback: return raw ids as choices
|
||||
return ids
|
||||
|
||||
# Default static choices from metadata
|
||||
for arg in meta.get("args", []):
|
||||
if arg.get("name") == target:
|
||||
return list(arg.get("choices", []) or [])
|
||||
|
||||
Reference in New Issue
Block a user