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.
This commit is contained in:
rlaphoenix 2023-05-25 04:37:17 +01:00
parent df2f9b85ae
commit a24633fe61
1 changed files with 11 additions and 3 deletions

View File

@ -37,7 +37,15 @@ class Services(click.MultiCommand):
def get_command(self, ctx: click.Context, name: str) -> click.Command: def get_command(self, ctx: click.Context, name: str) -> click.Command:
"""Load the Service and return the Click CLI method.""" """Load the Service and return the Click CLI method."""
tag = Services.get_tag(name) tag = Services.get_tag(name)
try:
service = Services.load(tag) 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"): if hasattr(service, "cli"):
return service.cli return service.cli
@ -58,7 +66,7 @@ class Services(click.MultiCommand):
for service in _SERVICES: for service in _SERVICES:
if service.parent.stem == tag: if service.parent.stem == tag:
return service.parent 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 @staticmethod
def get_tag(value: str) -> str: def get_tag(value: str) -> str:
@ -80,7 +88,7 @@ class Services(click.MultiCommand):
"""Load a Service module by Service tag.""" """Load a Service module by Service tag."""
module = _MODULES.get(tag) module = _MODULES.get(tag)
if not module: 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 return module