Improved command handling

This commit is contained in:
2021-03-04 19:06:53 +01:00
parent 2c43e55f77
commit 700bf9c937
17 changed files with 197 additions and 19 deletions

View File

@@ -1,24 +1,26 @@
from cpl.application.application_runtime_abc import ApplicationRuntimeABC
from cpl.dependency_injection.service_abc import ServiceABC
from cpl_cli.command import Command
from cpl.dependency_injection.service_provider_base import ServiceProviderABC
from cpl_cli.command_model import CommandModel
class CommandHandler(ServiceABC):
def __init__(self, runtime: ApplicationRuntimeABC):
def __init__(self, runtime: ApplicationRuntimeABC, services: ServiceProviderABC):
ServiceABC.__init__(self)
self._runtime = runtime
self._services = services
self._commands: list[Command] = []
self._commands: list[CommandModel] = []
def add_command(self, cmd: Command):
def add_command(self, cmd: CommandModel):
self._commands.append(cmd)
def remove_command(self, cmd: Command):
def remove_command(self, cmd: CommandModel):
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:
command.command.run(args)
self._services.get_service(command.command).run(args)