Improved help command

This commit is contained in:
Sven Heidemann 2022-05-19 20:21:46 +02:00
parent 0d37c86061
commit 94c82e6701
5 changed files with 37 additions and 52 deletions

View File

@ -1,5 +1,5 @@
import sys
import textwrap import textwrap
from typing import Optional
from cpl_core.console.console import Console from cpl_core.console.console import Console
from cpl_core.console.foreground_color_enum import ForegroundColorEnum from cpl_core.console.foreground_color_enum import ForegroundColorEnum
@ -21,10 +21,7 @@ class HelpService(CommandABC):
def help_message(self) -> str: def help_message(self) -> str:
return textwrap.dedent("""\ return textwrap.dedent("""\
Lists available command and their short descriptions. Lists available command and their short descriptions.
Usage: cpl help <command> Usage: cpl help
Arguments:
command The command to display the help message for
""") """)
def run(self, args: list[str]): def run(self, args: list[str]):
@ -33,20 +30,9 @@ class HelpService(CommandABC):
:param args: :param args:
:return: :return:
""" """
# if len(args) > 0: if len(args) > 0:
# command_name = args[0] Console.error(f'Unexpected argument(s): {", ".join(args)}')
# command: Optional[CommandABC] = None sys.exit()
# 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
Console.write_line('Available Commands:') Console.write_line('Available Commands:')
commands = [ commands = [
@ -68,3 +54,4 @@ class HelpService(CommandABC):
Console.write(f'\n\t{name} ') Console.write(f'\n\t{name} ')
Console.set_foreground_color(ForegroundColorEnum.default) Console.set_foreground_color(ForegroundColorEnum.default)
Console.write(f'{description}') Console.write(f'{description}')
Console.write_line('\nRun \'cpl <command> --help\' for command specific information\'s')

View File

@ -1,6 +1,7 @@
from abc import abstractmethod, ABC from abc import abstractmethod, ABC
from cpl_core.configuration.executable_argument import ExecutableArgument from cpl_core.configuration.executable_argument import ExecutableArgument
from cpl_core.console import Console
class CommandABC(ExecutableArgument): class CommandABC(ExecutableArgument):
@ -12,3 +13,14 @@ class CommandABC(ExecutableArgument):
@property @property
@abstractmethod @abstractmethod
def help_message(self) -> str: pass 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

View File

@ -1,18 +1,5 @@
import os 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.error import Error
from cpl_cli.live_server.live_server_service import LiveServerService from cpl_cli.live_server.live_server_service import LiveServerService
from cpl_cli.publish.publisher_abc import PublisherABC from cpl_cli.publish.publisher_abc import PublisherABC
@ -43,18 +30,4 @@ class Startup(StartupABC):
services.add_transient(PublisherABC, PublisherService) services.add_transient(PublisherABC, PublisherService)
services.add_transient(LiveServerService) 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() return services.build_service_provider()

View File

@ -1,6 +1,7 @@
import os import os
from typing import Optional from typing import Optional
from cpl_cli import CommandABC
from cpl_cli.command.add_service import AddService from cpl_cli.command.add_service import AddService
from cpl_cli.command.build_service import BuildService from cpl_cli.command.build_service import BuildService
from cpl_cli.command.custom_script_service import CustomScriptService 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.create_console_argument(ArgumentTypeEnum.Executable, '', 'version', ['v', 'V'], VersionService)
config.for_each_argument( 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) config.create_console_argument(ArgumentTypeEnum.Executable, '', 'help', ['h', 'H'], HelpService)
self._read_cpl_environment(config, env) self._read_cpl_environment(config, env)
def configure_services(self, service: ServiceCollectionABC, env: ApplicationEnvironmentABC): def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironmentABC):
pass 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)

View File

@ -4,6 +4,7 @@ import sys
from collections.abc import Callable from collections.abc import Callable
from typing import Union, Type, Optional 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_abc import ArgumentABC
from cpl_core.configuration.argument_builder import ArgumentBuilder from cpl_core.configuration.argument_builder import ArgumentBuilder
from cpl_core.configuration.argument_type_enum import ArgumentTypeEnum 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) self._parse_arguments(executables, arg_list, self._argument_types)
for exe in executables: for exe in executables:
service: ExecutableArgument = services.get_service(exe.executable_type) cmd: CommandABC = services.get_service(exe.executable_type)
service.run(self._additional_arguments) cmd.execute(self._additional_arguments)