From d2d1073ed97a2eaaa78990cea1ef5085a7867e76 Mon Sep 17 00:00:00 2001 From: Ioan Ferencik Date: Mon, 31 Aug 2026 18:25:18 +0200 Subject: [PATCH 1/2] better click cli context in connectivity click cmd def --- rapida/cli/connectivity.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rapida/cli/connectivity.py b/rapida/cli/connectivity.py index 383bc8a..a3d76cb 100644 --- a/rapida/cli/connectivity.py +++ b/rapida/cli/connectivity.py @@ -170,7 +170,7 @@ async def connectivity(ctx, bbox:tuple[float, float, float, float]=None, travel_ disjoint:bool=False, smooth:bool=True ): logger.info(f'Running connectivity analysis') - progress = ctx.obj.get('progress') + progress = ctx.obj.get('progress') if ctx.obj else None with progress: return await run_connectivity_analysis( bbox=bbox, dst_dir=dst_dir, travel_mode=travel_mode, time_intervals=time_intervals, From 48873e4058dbbec3a8a95f785c0064f28242ea6b Mon Sep 17 00:00:00 2001 From: Ioan Ferencik Date: Mon, 31 Aug 2026 18:30:35 +0200 Subject: [PATCH 2/2] improved run_cmd --- rapida/cli/run.py | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/rapida/cli/run.py b/rapida/cli/run.py index fdd7c86..79ee2ce 100644 --- a/rapida/cli/run.py +++ b/rapida/cli/run.py @@ -3,7 +3,7 @@ import click from rapida.cli import cli -def run_cmd(rapida_command_name:str = None, command_string: str = None): +def run_cmd_jin(rapida_command_name:str = None, command_string: str = None): """ Invoke a Click entry‐point programmatically using a shell‐style string. @@ -29,3 +29,42 @@ def run_cmd(rapida_command_name:str = None, command_string: str = None): raise click.ClickException( f"Command `{command_string}` exited with status {e.code}." ) + + +def run_cmd(rapida_command_name: str = None, command_string: str = None): + """ + Invoke a Click entry-point programmatically in Jupyter. + Handles no-arg help triggers, parameter errors, and async execution safely. + """ + if not rapida_command_name: + raise ValueError("A command name must be provided (e.g., run_cmd('connectivity')).") + + # Guard against None for command_string + args = shlex.split(command_string) if command_string else [] + rapida_command_names = list(cli.list_commands(None)) + + if "auth" in rapida_command_names: + rapida_command_names.remove("auth") + + assert rapida_command_name in rapida_command_names, ( + f"Invalid command '{rapida_command_name}'. Valid options are: {', '.join(rapida_command_names)}" + ) + + full_args = [rapida_command_name] + args + + try: + # Run through root group so context (ctx.obj['progress']) is initialized + return cli.main(args=full_args, standalone_mode=False) + + except SystemExit as e: + # Exit code 0 indicates normal termination (e.g., --help or no_args_is_help) + if e.code != 0: + raise click.ClickException( + f"Command `{rapida_command_name} {command_string or ''}` exited with status {e.code}." + ) + return None + + except click.ClickException as e: + # Formats Click usage/validation errors (e.g., missing required options) cleanly + e.show() + return None \ No newline at end of file