From 94c82e6701aee11e20f9b1d23616ad5e0e7ac3e6 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Thu, 19 May 2022 20:21:46 +0200 Subject: [PATCH] Improved help command --- src/cpl_cli/command/help_service.py | 25 +++++-------------- src/cpl_cli/command_abc.py | 12 +++++++++ src/cpl_cli/startup.py | 27 --------------------- src/cpl_cli/startup_argument_extension.py | 19 ++++++++++++--- src/cpl_core/configuration/configuration.py | 6 ++--- 5 files changed, 37 insertions(+), 52 deletions(-) diff --git a/src/cpl_cli/command/help_service.py b/src/cpl_cli/command/help_service.py index 4088ab54..c49f5e8f 100644 --- a/src/cpl_cli/command/help_service.py +++ b/src/cpl_cli/command/help_service.py @@ -1,5 +1,5 @@ +import sys import textwrap -from typing import Optional from cpl_core.console.console import Console from cpl_core.console.foreground_color_enum import ForegroundColorEnum @@ -21,10 +21,7 @@ class HelpService(CommandABC): def help_message(self) -> str: return textwrap.dedent("""\ Lists available command and their short descriptions. - Usage: cpl help - - Arguments: - command The command to display the help message for + Usage: cpl help """) def run(self, args: list[str]): @@ -33,20 +30,9 @@ class HelpService(CommandABC): :param args: :return: """ - # if len(args) > 0: - # command_name = args[0] - # command: Optional[CommandABC] = None - # for cmd in self._commands: - # if cmd.name == command_name or command_name in cmd.aliases: - # command = self._services.get_service(cmd.command) - # - # if command is None: - # Console.error(f'Invalid argument: {command_name}') - # return - # - # Console.write_line(command.help_message) - # - # return + if len(args) > 0: + Console.error(f'Unexpected argument(s): {", ".join(args)}') + sys.exit() Console.write_line('Available Commands:') commands = [ @@ -68,3 +54,4 @@ class HelpService(CommandABC): Console.write(f'\n\t{name} ') Console.set_foreground_color(ForegroundColorEnum.default) Console.write(f'{description}') + Console.write_line('\nRun \'cpl --help\' for command specific information\'s') diff --git a/src/cpl_cli/command_abc.py b/src/cpl_cli/command_abc.py index 1c2f72d5..900ecee9 100644 --- a/src/cpl_cli/command_abc.py +++ b/src/cpl_cli/command_abc.py @@ -1,6 +1,7 @@ from abc import abstractmethod, ABC from cpl_core.configuration.executable_argument import ExecutableArgument +from cpl_core.console import Console class CommandABC(ExecutableArgument): @@ -12,3 +13,14 @@ class CommandABC(ExecutableArgument): @property @abstractmethod def help_message(self) -> str: pass + + def execute(self, args: list[str]): + Console.write_line(args) + if 'help' in args: + Console.write_line(self.help_message) + return + + self.run(args) + + @abstractmethod + def run(self, args: list[str]): pass diff --git a/src/cpl_cli/startup.py b/src/cpl_cli/startup.py index cfae7fc6..6c90f01c 100644 --- a/src/cpl_cli/startup.py +++ b/src/cpl_cli/startup.py @@ -1,18 +1,5 @@ import os -from cpl_cli.command.add_service import AddService -from cpl_cli.command.build_service import BuildService -from cpl_cli.command.custom_script_service import CustomScriptService -from cpl_cli.command.generate_service import GenerateService -from cpl_cli.command.help_service import HelpService -from cpl_cli.command.install_service import InstallService -from cpl_cli.command.new_service import NewService -from cpl_cli.command.publish_service import PublishService -from cpl_cli.command.remove_service import RemoveService -from cpl_cli.command.start_service import StartService -from cpl_cli.command.uninstall_service import UninstallService -from cpl_cli.command.update_service import UpdateService -from cpl_cli.command.version_service import VersionService from cpl_cli.error import Error from cpl_cli.live_server.live_server_service import LiveServerService from cpl_cli.publish.publisher_abc import PublisherABC @@ -43,18 +30,4 @@ class Startup(StartupABC): services.add_transient(PublisherABC, PublisherService) services.add_transient(LiveServerService) - services.add_transient(AddService) - services.add_transient(BuildService) - services.add_transient(CustomScriptService) - services.add_transient(GenerateService) - services.add_transient(HelpService) - services.add_transient(InstallService) - services.add_transient(NewService) - services.add_transient(PublishService) - services.add_transient(RemoveService) - services.add_transient(StartService) - services.add_transient(UninstallService) - services.add_transient(UpdateService) - services.add_transient(VersionService) - return services.build_service_provider() diff --git a/src/cpl_cli/startup_argument_extension.py b/src/cpl_cli/startup_argument_extension.py index 731861ef..04a85336 100644 --- a/src/cpl_cli/startup_argument_extension.py +++ b/src/cpl_cli/startup_argument_extension.py @@ -1,6 +1,7 @@ import os from typing import Optional +from cpl_cli import CommandABC from cpl_cli.command.add_service import AddService from cpl_cli.command.build_service import BuildService from cpl_cli.command.custom_script_service import CustomScriptService @@ -87,11 +88,23 @@ class StartupArgumentExtension(StartupExtensionABC): config.create_console_argument(ArgumentTypeEnum.Executable, '', 'version', ['v', 'V'], VersionService) config.for_each_argument( - lambda a: a.add_console_argument(ArgumentTypeEnum.Executable, '--', 'help', ['h', 'H'], HelpService) + lambda a: a.add_console_argument(ArgumentTypeEnum.Flag, '--', 'help', ['h', 'H']) ) config.create_console_argument(ArgumentTypeEnum.Executable, '', 'help', ['h', 'H'], HelpService) self._read_cpl_environment(config, env) - def configure_services(self, service: ServiceCollectionABC, env: ApplicationEnvironmentABC): - pass + def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironmentABC): + services.add_transient(CommandABC, AddService) + services.add_transient(CommandABC, BuildService) + services.add_transient(CommandABC, CustomScriptService) + services.add_transient(CommandABC, GenerateService) + services.add_transient(CommandABC, HelpService) + services.add_transient(CommandABC, InstallService) + services.add_transient(CommandABC, NewService) + services.add_transient(CommandABC, PublishService) + services.add_transient(CommandABC, RemoveService) + services.add_transient(CommandABC, StartService) + services.add_transient(CommandABC, UninstallService) + services.add_transient(CommandABC, UpdateService) + services.add_transient(CommandABC, VersionService) diff --git a/src/cpl_core/configuration/configuration.py b/src/cpl_core/configuration/configuration.py index c5b7e18d..69f2aa0d 100644 --- a/src/cpl_core/configuration/configuration.py +++ b/src/cpl_core/configuration/configuration.py @@ -4,6 +4,7 @@ import sys from collections.abc import Callable from typing import Union, Type, Optional +from cpl_cli.command_abc import CommandABC from cpl_core.configuration.argument_abc import ArgumentABC from cpl_core.configuration.argument_builder import ArgumentBuilder from cpl_core.configuration.argument_type_enum import ArgumentTypeEnum @@ -281,6 +282,5 @@ class Configuration(ConfigurationABC): self._parse_arguments(executables, arg_list, self._argument_types) for exe in executables: - service: ExecutableArgument = services.get_service(exe.executable_type) - service.run(self._additional_arguments) - + cmd: CommandABC = services.get_service(exe.executable_type) + cmd.execute(self._additional_arguments)