kllk
Some checks failed
smoke-mm / Install & smoke test mm --help (push) Has been cancelled
Some checks failed
smoke-mm / Install & smoke test mm --help (push) Has been cancelled
This commit is contained in:
@@ -3,6 +3,23 @@ from __future__ import annotations
|
||||
from importlib import import_module
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
try:
|
||||
from config import get_local_storage_path
|
||||
except Exception:
|
||||
get_local_storage_path = None # type: ignore
|
||||
|
||||
|
||||
def _should_hide_db_args(config: Optional[Dict[str, Any]]) -> bool:
|
||||
"""Return True when the library root/local DB is not configured."""
|
||||
if not isinstance(config, dict):
|
||||
return False
|
||||
if get_local_storage_path is None:
|
||||
return False
|
||||
try:
|
||||
return not bool(get_local_storage_path(config))
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
try:
|
||||
from cmdlet import REGISTRY
|
||||
except Exception:
|
||||
@@ -60,6 +77,7 @@ def _normalize_arg(arg: Any) -> Dict[str, Any]:
|
||||
"choices": arg.get("choices", []) or [],
|
||||
"alias": arg.get("alias", ""),
|
||||
"variadic": arg.get("variadic", False),
|
||||
"requires_db": bool(arg.get("requires_db", False)),
|
||||
}
|
||||
|
||||
name = getattr(arg, "name", "") or ""
|
||||
@@ -71,10 +89,11 @@ def _normalize_arg(arg: Any) -> Dict[str, Any]:
|
||||
"choices": getattr(arg, "choices", []) or [],
|
||||
"alias": getattr(arg, "alias", ""),
|
||||
"variadic": getattr(arg, "variadic", False),
|
||||
"requires_db": bool(getattr(arg, "requires_db", False)),
|
||||
}
|
||||
|
||||
|
||||
def get_cmdlet_metadata(cmd_name: str) -> Optional[Dict[str, Any]]:
|
||||
def get_cmdlet_metadata(cmd_name: str, config: Optional[Dict[str, Any]] = None) -> Optional[Dict[str, Any]]:
|
||||
"""Return normalized metadata for a cmdlet, if available (aliases supported)."""
|
||||
ensure_registry_loaded()
|
||||
normalized = cmd_name.replace("-", "_")
|
||||
@@ -110,6 +129,9 @@ def get_cmdlet_metadata(cmd_name: str) -> Optional[Dict[str, Any]]:
|
||||
args_list = getattr(data, "arg", base.get("arg", [])) or []
|
||||
args = [_normalize_arg(arg) for arg in args_list]
|
||||
|
||||
if _should_hide_db_args(config):
|
||||
args = [a for a in args if not a.get("requires_db")]
|
||||
|
||||
return {
|
||||
"name": str(name).replace("_", "-").lower(),
|
||||
"aliases": [str(a).replace("_", "-").lower() for a in aliases if a],
|
||||
@@ -121,12 +143,12 @@ def get_cmdlet_metadata(cmd_name: str) -> Optional[Dict[str, Any]]:
|
||||
}
|
||||
|
||||
|
||||
def list_cmdlet_metadata() -> Dict[str, Dict[str, Any]]:
|
||||
def list_cmdlet_metadata(config: Optional[Dict[str, Any]] = None) -> Dict[str, Dict[str, Any]]:
|
||||
"""Collect metadata for all registered cmdlet keyed by canonical name."""
|
||||
ensure_registry_loaded()
|
||||
entries: Dict[str, Dict[str, Any]] = {}
|
||||
for reg_name in (REGISTRY or {}).keys():
|
||||
meta = get_cmdlet_metadata(reg_name)
|
||||
meta = get_cmdlet_metadata(reg_name, config=config)
|
||||
canonical = str(reg_name).replace("_", "-").lower()
|
||||
|
||||
if meta:
|
||||
@@ -167,10 +189,10 @@ def list_cmdlet_metadata() -> Dict[str, Dict[str, Any]]:
|
||||
return entries
|
||||
|
||||
|
||||
def list_cmdlet_names(include_aliases: bool = True) -> List[str]:
|
||||
def list_cmdlet_names(include_aliases: bool = True, config: Optional[Dict[str, Any]] = None) -> List[str]:
|
||||
"""Return sorted cmdlet names (optionally including aliases)."""
|
||||
ensure_registry_loaded()
|
||||
entries = list_cmdlet_metadata()
|
||||
entries = list_cmdlet_metadata(config=config)
|
||||
names = set()
|
||||
for meta in entries.values():
|
||||
names.add(meta.get("name", ""))
|
||||
@@ -180,9 +202,9 @@ def list_cmdlet_names(include_aliases: bool = True) -> List[str]:
|
||||
return sorted(n for n in names if n)
|
||||
|
||||
|
||||
def get_cmdlet_arg_flags(cmd_name: str) -> List[str]:
|
||||
def get_cmdlet_arg_flags(cmd_name: str, config: Optional[Dict[str, Any]] = None) -> List[str]:
|
||||
"""Return flag variants for cmdlet arguments (e.g., -name/--name)."""
|
||||
meta = get_cmdlet_metadata(cmd_name)
|
||||
meta = get_cmdlet_metadata(cmd_name, config=config)
|
||||
if not meta:
|
||||
return []
|
||||
|
||||
@@ -201,9 +223,9 @@ def get_cmdlet_arg_flags(cmd_name: str) -> List[str]:
|
||||
return flags
|
||||
|
||||
|
||||
def get_cmdlet_arg_choices(cmd_name: str, arg_name: str) -> List[str]:
|
||||
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."""
|
||||
meta = get_cmdlet_metadata(cmd_name)
|
||||
meta = get_cmdlet_metadata(cmd_name, config=config)
|
||||
if not meta:
|
||||
return []
|
||||
target = arg_name.lstrip("-")
|
||||
|
||||
Reference in New Issue
Block a user