update refactoring and add new features
This commit is contained in:
+9
-85
@@ -8,7 +8,7 @@ running from a development checkout (by importing the top-level
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Optional, List, Tuple
|
||||
from typing import Optional, List
|
||||
import importlib
|
||||
import importlib.util
|
||||
import os
|
||||
@@ -64,91 +64,32 @@ def _ensure_repo_root_on_sys_path(pkg_file: Optional[Path] = None) -> Optional[P
|
||||
return None
|
||||
|
||||
|
||||
def _parse_mode_and_strip_args(args: List[str]) -> Tuple[Optional[str], List[str]]:
|
||||
"""Parse --gui/--cli/--mode flags and return (mode, cleaned_args).
|
||||
def _strip_mode_args(args: List[str]) -> List[str]:
|
||||
"""Strip --gui/--cli/--mode flags from argument list.
|
||||
|
||||
The function removes any mode flags from the argument list so the selected
|
||||
runner can receive the remaining arguments untouched.
|
||||
|
||||
Supported forms:
|
||||
--gui, -g, --gui=true
|
||||
--cli, -c, --cli=true
|
||||
--mode=gui|cli
|
||||
--mode gui|cli
|
||||
|
||||
Raises ValueError on conflicting or invalid flags.
|
||||
The GUI/TUI mode has been discontinued. Any mode flags are silently
|
||||
consumed so they don't interfere with remaining argument parsing.
|
||||
"""
|
||||
mode: Optional[str] = None
|
||||
out: List[str] = []
|
||||
i = 0
|
||||
while i < len(args):
|
||||
a = args[i]
|
||||
la = a.lower()
|
||||
|
||||
# --gui / -g
|
||||
if la in ("--gui", "-g"):
|
||||
if mode and mode != "gui":
|
||||
raise ValueError("Conflicting mode flags: found both 'gui' and 'cli'")
|
||||
mode = "gui"
|
||||
if la in ("--gui", "-g", "--cli", "-c"):
|
||||
i += 1
|
||||
continue
|
||||
if la.startswith("--gui="):
|
||||
val = la.split("=", 1)[1]
|
||||
if val and val not in ("0", "false", "no", "off"):
|
||||
if mode and mode != "gui":
|
||||
raise ValueError(
|
||||
"Conflicting mode flags: found both 'gui' and 'cli'"
|
||||
)
|
||||
mode = "gui"
|
||||
if la.startswith("--gui=") or la.startswith("--cli="):
|
||||
i += 1
|
||||
continue
|
||||
|
||||
# --cli / -c
|
||||
if la in ("--cli", "-c"):
|
||||
if mode and mode != "cli":
|
||||
raise ValueError("Conflicting mode flags: found both 'gui' and 'cli'")
|
||||
mode = "cli"
|
||||
i += 1
|
||||
continue
|
||||
if la.startswith("--cli="):
|
||||
val = la.split("=", 1)[1]
|
||||
if val and val not in ("0", "false", "no", "off"):
|
||||
if mode and mode != "cli":
|
||||
raise ValueError(
|
||||
"Conflicting mode flags: found both 'gui' and 'cli'"
|
||||
)
|
||||
mode = "cli"
|
||||
i += 1
|
||||
continue
|
||||
|
||||
# --mode
|
||||
if la.startswith("--mode="):
|
||||
val = la.split("=", 1)[1]
|
||||
val = val.lower()
|
||||
if val not in ("gui", "cli"):
|
||||
raise ValueError("--mode must be 'gui' or 'cli'")
|
||||
if mode and mode != val:
|
||||
raise ValueError("Conflicting mode flags: found both 'gui' and 'cli'")
|
||||
mode = val
|
||||
i += 1
|
||||
continue
|
||||
if la == "--mode":
|
||||
if i + 1 >= len(args):
|
||||
raise ValueError("--mode requires a value ('gui' or 'cli')")
|
||||
val = args[i + 1].lower()
|
||||
if val not in ("gui", "cli"):
|
||||
raise ValueError("--mode must be 'gui' or 'cli'")
|
||||
if mode and mode != val:
|
||||
raise ValueError("Conflicting mode flags: found both 'gui' and 'cli'")
|
||||
mode = val
|
||||
i += 2
|
||||
continue
|
||||
|
||||
# Not a mode flag; keep it
|
||||
out.append(a)
|
||||
i += 1
|
||||
|
||||
return mode, out
|
||||
return out
|
||||
|
||||
|
||||
def _run_cli(clean_args: List[str]) -> int:
|
||||
@@ -221,15 +162,6 @@ def _run_cli(clean_args: List[str]) -> int:
|
||||
return int(getattr(exc, "code", 0) or 0)
|
||||
|
||||
|
||||
def _run_gui(clean_args: List[str]) -> int:
|
||||
"""Report that the discontinued GUI/TUI mode is no longer available."""
|
||||
_ = clean_args
|
||||
print(
|
||||
"Error: GUI/TUI mode has been discontinued and is no longer available.",
|
||||
file=sys.stderr,
|
||||
)
|
||||
return 2
|
||||
|
||||
|
||||
def main(argv: Optional[List[str]] = None) -> int:
|
||||
"""Entry point for console_scripts.
|
||||
@@ -239,11 +171,7 @@ def main(argv: Optional[List[str]] = None) -> int:
|
||||
"""
|
||||
args = list(argv) if argv is not None else list(sys.argv[1:])
|
||||
|
||||
try:
|
||||
mode, clean_args = _parse_mode_and_strip_args(args)
|
||||
except ValueError as exc:
|
||||
print(f"Error parsing mode flags: {exc}", file=sys.stderr)
|
||||
return 2
|
||||
clean_args = _strip_mode_args(args)
|
||||
|
||||
# Early environment sanity check to detect urllib3/urllib3-future conflicts.
|
||||
# When a broken urllib3 is detected we print an actionable message and
|
||||
@@ -262,10 +190,6 @@ def main(argv: Optional[List[str]] = None) -> int:
|
||||
# startup; we'll continue and let normal import errors surface.
|
||||
pass
|
||||
|
||||
# If GUI requested, delegate directly (GUI may decide to honor any args itself)
|
||||
if mode == "gui":
|
||||
return _run_gui(clean_args)
|
||||
|
||||
# Support quoting a pipeline (or even a single full command) on the command line.
|
||||
#
|
||||
# - If the user provides a single argument that contains a pipe character,
|
||||
|
||||
Reference in New Issue
Block a user