From 93829027afd46f6ab3d41d39712b3eb5d25f7634 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 11 Apr 2021 16:36:59 +0200 Subject: [PATCH] Added help message to all cli commands --- src/cpl_cli/command/add_service.py | 8 ++++++- src/cpl_cli/command/build_service.py | 7 ++++++ src/cpl_cli/command/generate_service.py | 6 +++++ src/cpl_cli/command/help_service.py | 29 +++++++++++++++++++++++- src/cpl_cli/command/install_service.py | 6 +++++ src/cpl_cli/command/new_service.py | 6 +++++ src/cpl_cli/command/publish_service.py | 7 ++++++ src/cpl_cli/command/remove_service.py | 6 +++++ src/cpl_cli/command/start_service.py | 7 ++++++ src/cpl_cli/command/uninstall_service.py | 6 +++++ src/cpl_cli/command/update_service.py | 6 +++++ src/cpl_cli/command/version_service.py | 6 +++++ src/cpl_cli/command_abc.py | 4 ++++ src/cpl_cli/startup.py | 4 +++- 14 files changed, 105 insertions(+), 3 deletions(-) diff --git a/src/cpl_cli/command/add_service.py b/src/cpl_cli/command/add_service.py index ea4c8738..612038a0 100644 --- a/src/cpl_cli/command/add_service.py +++ b/src/cpl_cli/command/add_service.py @@ -1,5 +1,6 @@ import json -import os.path +import os +import textwrap from typing import Optional from cpl.configuration.configuration_abc import ConfigurationABC @@ -24,6 +25,11 @@ class AddService(CommandABC): self._workspace = workspace + @property + def help_message(self) -> str: + return textwrap.dedent("""\ + """) + @staticmethod def _edit_project_file(source: str, project_settings: ProjectSettings, build_settings: BuildSettings): with open(source, 'w') as file: diff --git a/src/cpl_cli/command/build_service.py b/src/cpl_cli/command/build_service.py index 18ce4dbd..703b54ac 100644 --- a/src/cpl_cli/command/build_service.py +++ b/src/cpl_cli/command/build_service.py @@ -1,3 +1,5 @@ +import textwrap + from cpl_cli.command_abc import CommandABC from cpl_cli.publish.publisher_abc import PublisherABC @@ -13,6 +15,11 @@ class BuildService(CommandABC): self._publisher = publisher + @property + def help_message(self) -> str: + return textwrap.dedent("""\ + """) + def run(self, args: list[str]): """ Entry point of command diff --git a/src/cpl_cli/command/generate_service.py b/src/cpl_cli/command/generate_service.py index 3358d681..5078a23a 100644 --- a/src/cpl_cli/command/generate_service.py +++ b/src/cpl_cli/command/generate_service.py @@ -1,4 +1,5 @@ import os +import textwrap from collections import Callable from cpl.configuration.configuration_abc import ConfigurationABC @@ -55,6 +56,11 @@ class GenerateService(CommandABC): self._config = configuration self._env = self._config.environment + @property + def help_message(self) -> str: + return textwrap.dedent("""\ + """) + @staticmethod def _help(message: str): """ diff --git a/src/cpl_cli/command/help_service.py b/src/cpl_cli/command/help_service.py index fb96a419..a93f8de2 100644 --- a/src/cpl_cli/command/help_service.py +++ b/src/cpl_cli/command/help_service.py @@ -1,22 +1,49 @@ +import textwrap +from typing import Optional + from cpl.console.console import Console from cpl.console.foreground_color_enum import ForegroundColorEnum +from cpl.dependency_injection.service_provider_abc import ServiceProviderABC +from cpl_cli.command_handler_service import CommandHandler from cpl_cli.command_abc import CommandABC class HelpService(CommandABC): - def __init__(self): + def __init__(self, services: ServiceProviderABC, cmd_handler: CommandHandler): """ Service for CLI command help """ CommandABC.__init__(self) + self._services = services + self._commands = cmd_handler.commands + + @property + def help_message(self) -> str: + return textwrap.dedent("""\ + """) + def run(self, args: list[str]): """ Entry point of command :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: + command = self._services.get_service(cmd.command) + + if command is None: + Console.error(f'Invalid argument: {command_name}') + + Console.write_line(command.help_message) + + return + Console.write_line('Available Commands:') commands = [ ['add (a|a)', 'Adds a project reference to given project.'], diff --git a/src/cpl_cli/command/install_service.py b/src/cpl_cli/command/install_service.py index 2178e735..282d0562 100644 --- a/src/cpl_cli/command/install_service.py +++ b/src/cpl_cli/command/install_service.py @@ -1,6 +1,7 @@ import json import os import subprocess +import textwrap from packaging import version @@ -39,6 +40,11 @@ class InstallService(CommandABC): self._project_file = f'{self._config.get_configuration("ProjectName")}.json' + @property + def help_message(self) -> str: + return textwrap.dedent("""\ + """) + def _install_project(self): """ Installs dependencies of CPl project diff --git a/src/cpl_cli/command/new_service.py b/src/cpl_cli/command/new_service.py index 20c3bb22..9a7c9b19 100644 --- a/src/cpl_cli/command/new_service.py +++ b/src/cpl_cli/command/new_service.py @@ -1,5 +1,6 @@ import os import sys +import textwrap from typing import Optional from packaging import version @@ -46,6 +47,11 @@ class NewService(CommandABC): self._use_startup: bool = False self._use_service_providing: bool = False + @property + def help_message(self) -> str: + return textwrap.dedent("""\ + """) + @staticmethod def _help(message: str): """ diff --git a/src/cpl_cli/command/publish_service.py b/src/cpl_cli/command/publish_service.py index b46474cb..6f1e2c5f 100644 --- a/src/cpl_cli/command/publish_service.py +++ b/src/cpl_cli/command/publish_service.py @@ -1,3 +1,5 @@ +import textwrap + from cpl_cli.command_abc import CommandABC from cpl_cli.publish.publisher_abc import PublisherABC @@ -13,6 +15,11 @@ class PublishService(CommandABC): self._publisher = publisher + @property + def help_message(self) -> str: + return textwrap.dedent("""\ + """) + def run(self, args: list[str]): """ Entry point of command diff --git a/src/cpl_cli/command/remove_service.py b/src/cpl_cli/command/remove_service.py index 89457e57..378237d5 100644 --- a/src/cpl_cli/command/remove_service.py +++ b/src/cpl_cli/command/remove_service.py @@ -1,6 +1,7 @@ import os import shutil import json +import textwrap from cpl.configuration.configuration_abc import ConfigurationABC from cpl.console.console import Console @@ -25,6 +26,11 @@ class RemoveService(CommandABC): self._workspace: WorkspaceSettings = self._config.get_configuration(WorkspaceSettings) + @property + def help_message(self) -> str: + return textwrap.dedent("""\ + """) + @staticmethod def _create_file(file_name: str, content: dict): if not os.path.isabs(file_name): diff --git a/src/cpl_cli/command/start_service.py b/src/cpl_cli/command/start_service.py index 388846f1..4e42fea6 100644 --- a/src/cpl_cli/command/start_service.py +++ b/src/cpl_cli/command/start_service.py @@ -1,3 +1,5 @@ +import textwrap + from cpl_cli.command_abc import CommandABC from cpl_cli.live_server.live_server_service import LiveServerService @@ -13,6 +15,11 @@ class StartService(CommandABC): self._live_server = live_server + @property + def help_message(self) -> str: + return textwrap.dedent("""\ + """) + def run(self, args: list[str]): """ Entry point of command diff --git a/src/cpl_cli/command/uninstall_service.py b/src/cpl_cli/command/uninstall_service.py index 8e091766..ade17dd6 100644 --- a/src/cpl_cli/command/uninstall_service.py +++ b/src/cpl_cli/command/uninstall_service.py @@ -1,6 +1,7 @@ import json import os import subprocess +import textwrap from cpl.configuration.configuration_abc import ConfigurationABC from cpl.console.console import Console @@ -31,6 +32,11 @@ class UninstallService(CommandABC): self._build_settings = build_settings self._project_settings = project_settings + @property + def help_message(self) -> str: + return textwrap.dedent("""\ + """) + def run(self, args: list[str]): """ Entry point of command diff --git a/src/cpl_cli/command/update_service.py b/src/cpl_cli/command/update_service.py index eef18f80..c5a31ba4 100644 --- a/src/cpl_cli/command/update_service.py +++ b/src/cpl_cli/command/update_service.py @@ -1,6 +1,7 @@ import json import os import subprocess +import textwrap from cpl.configuration.configuration_abc import ConfigurationABC from cpl.console.console import Console @@ -38,6 +39,11 @@ class UpdateService(CommandABC): self._project_settings = project_settings self._cli_settings = cli_settings + @property + def help_message(self) -> str: + return textwrap.dedent("""\ + """) + def _collect_project_dependencies(self) -> list[tuple]: """ Collects project dependencies diff --git a/src/cpl_cli/command/version_service.py b/src/cpl_cli/command/version_service.py index 0046a3f9..cde683e9 100644 --- a/src/cpl_cli/command/version_service.py +++ b/src/cpl_cli/command/version_service.py @@ -2,6 +2,7 @@ import pkgutil import sys import platform import pkg_resources +import textwrap import cpl import cpl_cli @@ -18,6 +19,11 @@ class VersionService(CommandABC): """ CommandABC.__init__(self) + @property + def help_message(self) -> str: + return textwrap.dedent("""\ + """) + def run(self, args: list[str]): """ Entry point of command diff --git a/src/cpl_cli/command_abc.py b/src/cpl_cli/command_abc.py index 6ac6376f..9cdfd84b 100644 --- a/src/cpl_cli/command_abc.py +++ b/src/cpl_cli/command_abc.py @@ -7,5 +7,9 @@ class CommandABC(ABC): def __init__(self): ABC.__init__(self) + @property + @abstractmethod + def help_message(self) -> str: pass + @abstractmethod def run(self, args: list[str]): pass diff --git a/src/cpl_cli/startup.py b/src/cpl_cli/startup.py index 84e77c8d..f380b050 100644 --- a/src/cpl_cli/startup.py +++ b/src/cpl_cli/startup.py @@ -53,7 +53,9 @@ class Startup(StartupABC): ConsoleArgument('', 'settings', ['st', 'ST'], ' '), ConsoleArgument('', 'thread', ['t', 't'], ' ') ])) - self._configuration.add_console_argument(ConsoleArgument('', 'help', ['h', 'H'], '')) + self._configuration.add_console_argument( + ConsoleArgument('', 'help', ['h', 'H'], ' ', is_value_token_optional=True) + ) self._configuration.add_console_argument( ConsoleArgument('', 'install', ['i', 'I'], ' ', is_value_token_optional=True) )