From 20628b8ea776f1df094223ca515ecc4e527ba6b8 Mon Sep 17 00:00:00 2001 From: qwint Date: Mon, 29 Jul 2024 20:26:14 -0500 Subject: [PATCH] adds handling for the `--` cli arg by having launcher capture, ignore, and pass through all of the values after it, while only processing (and validating) the values before it updates text client and its components to allow for args to be passed through, captured in run_as_textclient, and used in parse_args if present --- CommonClient.py | 4 ++-- Launcher.py | 21 +++++++++++++++++---- worlds/LauncherComponents.py | 8 ++++---- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/CommonClient.py b/CommonClient.py index 09937e4b9ab8..e8332f813f59 100644 --- a/CommonClient.py +++ b/CommonClient.py @@ -994,7 +994,7 @@ def get_base_parser(description: typing.Optional[str] = None): return parser -def run_as_textclient(): +def run_as_textclient(*args): class TextContext(CommonContext): # Text Mode to use !hint and such with games that have no text entry tags = CommonContext.tags | {"TextOnly"} @@ -1033,7 +1033,7 @@ async def main(args): parser = get_base_parser(description="Gameless Archipelago Client, for text interfacing.") parser.add_argument('--name', default=None, help="Slot Name to connect as.") parser.add_argument("url", nargs="?", help="Archipelago connection url") - args = parser.parse_args() + args = parser.parse_args(args if args else None) if args.url: url = urllib.parse.urlparse(args.url) diff --git a/Launcher.py b/Launcher.py index e4b65be93a68..c55abd9061d7 100644 --- a/Launcher.py +++ b/Launcher.py @@ -322,15 +322,28 @@ def main(args: Optional[Union[argparse.Namespace, dict]] = None): init_logging('Launcher') Utils.freeze_support() multiprocessing.set_start_method("spawn") # if launched process uses kivy, fork won't work - parser = argparse.ArgumentParser(description='Archipelago Launcher') + parser = argparse.ArgumentParser( + description='Archipelago Launcher', + usage="[-h] [--update_settings] [Patch|Game|Component] [-- component args here]" + ) run_group = parser.add_argument_group("Run") run_group.add_argument("--update_settings", action="store_true", help="Update host.yaml and exit.") run_group.add_argument("Patch|Game|Component", type=str, nargs="?", help="Pass either a patch file, a generated game or the name of a component to run.") - run_group.add_argument("args", nargs="*", - help="Arguments to pass to component.") - main(parser.parse_args()) + run_group.add_argument("--", + help="Arguments to pass to component.", + dest="args", default=[]) + args = sys.argv[1:] + if "--" in args: + i = args.index("--") + passthrough_args = args[i+1:] + args = args[:i] + args = parser.parse_args(args) + args.args = passthrough_args + else: + args = parser.parse_args() + main(args) from worlds.LauncherComponents import processes for process in processes: diff --git a/worlds/LauncherComponents.py b/worlds/LauncherComponents.py index 18c1a1661ef0..a21df9d7dec8 100644 --- a/worlds/LauncherComponents.py +++ b/worlds/LauncherComponents.py @@ -56,10 +56,10 @@ def __repr__(self): processes = weakref.WeakSet() -def launch_subprocess(func: Callable, name: str = None): +def launch_subprocess(func: Callable, name: str = None, args=()): global processes import multiprocessing - process = multiprocessing.Process(target=func, name=name) + process = multiprocessing.Process(target=func, name=name, args=args) process.start() processes.add(process) @@ -78,9 +78,9 @@ def __call__(self, path: str) -> bool: return False -def launch_textclient(): +def launch_textclient(*args): import CommonClient - launch_subprocess(CommonClient.run_as_textclient, name="TextClient") + launch_subprocess(CommonClient.run_as_textclient, name="TextClient", args=args) def _install_apworld(apworld_src: str = "") -> Optional[Tuple[pathlib.Path, pathlib.Path]]: