2021-03-04 07:18:36 +01:00
|
|
|
from cpl.application.application_runtime_abc import ApplicationRuntimeABC
|
2021-03-03 19:37:35 +01:00
|
|
|
from cpl.dependency_injection.service_abc import ServiceABC
|
2021-03-04 19:06:53 +01:00
|
|
|
from cpl.dependency_injection.service_provider_base import ServiceProviderABC
|
|
|
|
from cpl_cli.command_model import CommandModel
|
2021-03-03 19:37:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
class CommandHandler(ServiceABC):
|
|
|
|
|
2021-03-04 19:06:53 +01:00
|
|
|
def __init__(self, runtime: ApplicationRuntimeABC, services: ServiceProviderABC):
|
2021-03-03 19:37:35 +01:00
|
|
|
ServiceABC.__init__(self)
|
|
|
|
|
2021-03-04 07:18:36 +01:00
|
|
|
self._runtime = runtime
|
2021-03-04 19:06:53 +01:00
|
|
|
self._services = services
|
2021-03-04 07:18:36 +01:00
|
|
|
|
2021-03-04 19:06:53 +01:00
|
|
|
self._commands: list[CommandModel] = []
|
2021-03-03 19:37:35 +01:00
|
|
|
|
2021-03-04 19:06:53 +01:00
|
|
|
def add_command(self, cmd: CommandModel):
|
2021-03-03 19:37:35 +01:00
|
|
|
self._commands.append(cmd)
|
|
|
|
|
2021-03-04 19:06:53 +01:00
|
|
|
def remove_command(self, cmd: CommandModel):
|
2021-03-03 19:37:35 +01:00
|
|
|
self._commands.remove(cmd)
|
|
|
|
|
|
|
|
def handle(self, cmd: str, args: list[str]):
|
|
|
|
for command in self._commands:
|
|
|
|
if cmd == command.name or cmd in command.aliases:
|
2021-03-04 19:06:53 +01:00
|
|
|
self._services.get_service(command.command).run(args)
|