From a24633fe61a8e3f2327d573a6a89e1dce7dcdb76 Mon Sep 17 00:00:00 2001 From: rlaphoenix Date: Thu, 25 May 2023 04:37:17 +0100 Subject: [PATCH] List available Services on error This is mainly to lessen confusion on service name typo's or new users getting used to the CLI. It also changed the Exceptions on the methods of Service from ClickException to a KeyError since they are intended to be used on the core codebase outside of the context of Click. --- devine/core/services.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/devine/core/services.py b/devine/core/services.py index 16a2a96..10acb6d 100644 --- a/devine/core/services.py +++ b/devine/core/services.py @@ -37,7 +37,15 @@ class Services(click.MultiCommand): def get_command(self, ctx: click.Context, name: str) -> click.Command: """Load the Service and return the Click CLI method.""" tag = Services.get_tag(name) - service = Services.load(tag) + try: + service = Services.load(tag) + except KeyError as e: + available_services = self.list_commands(ctx) + if not available_services: + raise click.ClickException( + f"There are no Services added yet, therefore the '{name}' Service could not be found." + ) + raise click.ClickException(f"{e}. Available Services: {', '.join(available_services)}") if hasattr(service, "cli"): return service.cli @@ -58,7 +66,7 @@ class Services(click.MultiCommand): for service in _SERVICES: if service.parent.stem == tag: return service.parent - raise click.ClickException(f"Unable to find service by the name '{name}'") + raise KeyError(f"There is no Service added by the Tag '{name}'") @staticmethod def get_tag(value: str) -> str: @@ -80,7 +88,7 @@ class Services(click.MultiCommand): """Load a Service module by Service tag.""" module = _MODULES.get(tag) if not module: - raise click.ClickException(f"Unable to find Service by the tag '{tag}'") + raise KeyError(f"There is no Service added by the Tag '{tag}'") return module