diff --git a/scripts/build.sh b/scripts/build.sh new file mode 100644 index 00000000..bccdac52 --- /dev/null +++ b/scripts/build.sh @@ -0,0 +1,11 @@ +#!/bin/bash +# activate venv +source /home/sven/Nextcloud_Sven/Schreibtisch/git_sh-edraft_de/sh_cpl/cpl-env/bin/activate + +# CLI +cd /home/sven/Nextcloud_Sven/Schreibtisch/git_sh-edraft_de/sh_cpl/ +cpl build + +# CPL +cd /home/sven/Nextcloud_Sven/Schreibtisch/git_sh-edraft_de/sh_cpl/src/cpl +cpl build \ No newline at end of file diff --git a/scripts/publish.sh b/scripts/publish.sh new file mode 100644 index 00000000..341a279c --- /dev/null +++ b/scripts/publish.sh @@ -0,0 +1,11 @@ +#!/bin/bash +# activate venv +source /home/sven/Nextcloud_Sven/Schreibtisch/git_sh-edraft_de/sh_cpl/cpl-env/bin/activate + +# CLI +cd /home/sven/Nextcloud_Sven/Schreibtisch/git_sh-edraft_de/sh_cpl/ +cpl publish + +# CPL +cd /home/sven/Nextcloud_Sven/Schreibtisch/git_sh-edraft_de/sh_cpl/src/cpl +cpl publish \ No newline at end of file diff --git a/src/cpl/configuration/configuration.py b/src/cpl/configuration/configuration.py index 72959a94..4c7edc7b 100644 --- a/src/cpl/configuration/configuration.py +++ b/src/cpl/configuration/configuration.py @@ -104,32 +104,6 @@ class Configuration(ConfigurationABC): else: self._config[name] = value - def _validate_argument_child(self, argument: str, argument_type: ConsoleArgument, - next_arguments: Optional[list[str]]) -> bool: - """ - Validates the child arguments of argument - :param argument: - :param argument_type: - :param next_arguments: - :return: - """ - if argument_type.console_arguments is not None and len(argument_type.console_arguments) > 0: - found = False - for child_argument_type in argument_type.console_arguments: - found = self._validate_argument_by_argument_type(argument, child_argument_type, next_arguments) - if found and child_argument_type.name not in self._additional_arguments: - self._additional_arguments.append(child_argument_type.name) - - if found: - break - - if not found: - raise Exception(f'Invalid argument: {argument}') - - return found - - return True - def _validate_argument_by_argument_type(self, argument: str, argument_type: ConsoleArgument, next_arguments: list[str] = None) -> bool: """ @@ -246,7 +220,23 @@ class Configuration(ConfigurationABC): next_args = [] if len(next_arguments) > 1: next_args = next_arguments[1:] - result = self._validate_argument_child(next_arguments[0], argument_type, next_args) + + if argument_type.console_arguments is not None and len(argument_type.console_arguments) > 0: + found_child = False + for child_argument_type in argument_type.console_arguments: + found_child = self._validate_argument_by_argument_type( + next_arguments[0], + child_argument_type, + next_args + ) + if found_child and child_argument_type.name not in self._additional_arguments: + self._additional_arguments.append(child_argument_type.name) + + if found_child: + break + + if not found_child: + result = self._validate_argument_by_argument_type(next_arguments[0], argument_type, next_args) return result diff --git a/src/cpl_cli/cli.py b/src/cpl_cli/cli.py index 145abc12..a4e029f7 100644 --- a/src/cpl_cli/cli.py +++ b/src/cpl_cli/cli.py @@ -30,6 +30,7 @@ class CLI(ApplicationABC): ApplicationABC.__init__(self, config, services) self._command_handler: Optional[CommandHandler] = None + self._options: list[str] = [] def configure(self): self._command_handler: CommandHandler = self._services.get_service(CommandHandler) @@ -47,6 +48,9 @@ class CLI(ApplicationABC): self._command_handler.add_command(CommandModel('update', ['u', 'U'], UpdateService, False, True, True)) self._command_handler.add_command(CommandModel('version', ['v', 'V'], VersionService, False, False, False)) + self._command_handler.add_command(CommandModel('--help', ['-h', '-H'], HelpService, False, False, False)) + self._options.append('--help') + def main(self): """ Entry point of the CPL CLI @@ -56,14 +60,40 @@ class CLI(ApplicationABC): command = None args = [] if len(self._configuration.additional_arguments) > 0: - command = self._configuration.additional_arguments[0] - if len(self._configuration.additional_arguments) > 1: - args = self._configuration.additional_arguments[1:] + is_option = False + for opt in self._options: + if opt in self._configuration.additional_arguments: + is_option = True + command = opt + args = self._configuration.additional_arguments + args.remove(opt) + + if not is_option: + command = self._configuration.additional_arguments[0] + if len(self._configuration.additional_arguments) > 1: + args = self._configuration.additional_arguments[1:] else: for cmd in self._command_handler.commands: result = self._configuration.get_configuration(cmd.name) - result_args = self._configuration.get_configuration(f'{cmd.name}AdditionalArguments') - if result is not None: + result_args: list[str] = self._configuration.get_configuration(f'{cmd.name}AdditionalArguments') + is_option = False + for opt in self._options: + if opt == result: + is_option = True + command = opt + + elif result_args is not None and opt in result_args: + is_option = True + command = opt + result_args.remove(opt) + + if is_option: + args.append(cmd.name) + if result_args is not None: + for arg in result_args: + args.append(arg) + + elif result is not None: command = cmd.name args.append(result) diff --git a/src/cpl_cli/command/add_service.py b/src/cpl_cli/command/add_service.py index ea4c8738..30d2dd17 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,17 @@ class AddService(CommandABC): self._workspace = workspace + @property + def help_message(self) -> str: + return textwrap.dedent("""\ + Adds a project reference to given project. + Usage: cpl add + + Arguments: + source-project: Name of the project to which the reference has to be + target-project: Name of the project to be referenced + """) + @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..5fe1bc72 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,13 @@ class BuildService(CommandABC): self._publisher = publisher + @property + def help_message(self) -> str: + return textwrap.dedent("""\ + Copies an python app into an output directory named build/ at the given output path. Must be executed within a CPL workspace or project directory + Usage: cpl build + """) + 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..0b7503be 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,25 @@ class GenerateService(CommandABC): self._config = configuration self._env = self._config.environment + @property + def help_message(self) -> str: + return textwrap.dedent("""\ + Generate a file based on schematic. + Usage: cpl generate + + Arguments: + schematic: The schematic to generate. + name: The name of the generated file + + Schematics: + abc + class + enum + service + settings + thread + """) + @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..50e98921 100644 --- a/src/cpl_cli/command/help_service.py +++ b/src/cpl_cli/command/help_service.py @@ -1,22 +1,55 @@ +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("""\ + Lists available command and their short descriptions. + Usage: cpl help + + Arguments: + command The command to display the help message for + """) + 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 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:') 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..7f733e2f 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,16 @@ class InstallService(CommandABC): self._project_file = f'{self._config.get_configuration("ProjectName")}.json' + @property + def help_message(self) -> str: + return textwrap.dedent("""\ + Installs given package via pip + Usage: cpl install + + Arguments: + package The package to install + """) + 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..8c19b346 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,21 @@ class NewService(CommandABC): self._use_startup: bool = False self._use_service_providing: bool = False + @property + def help_message(self) -> str: + return textwrap.dedent("""\ + Generates a workspace and initial project or add a project to workspace. + Usage: cpl new + + Arguments: + type The project type of the initial project + name Name of the workspace or the project + + Types: + console + library + """) + @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..7a6403a4 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,13 @@ class PublishService(CommandABC): self._publisher = publisher + @property + def help_message(self) -> str: + return textwrap.dedent("""\ + Prepares files for publish into an output directory named dist/ at the given output path and executes setup.py. + Usage: cpl publish + """) + 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..d826d907 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,16 @@ class RemoveService(CommandABC): self._workspace: WorkspaceSettings = self._config.get_configuration(WorkspaceSettings) + @property + def help_message(self) -> str: + return textwrap.dedent("""\ + Removes a project from workspace. + Usage: cpl remove + + Arguments: + project The name of the project to delete + """) + @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..48b853b4 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,13 @@ class StartService(CommandABC): self._live_server = live_server + @property + def help_message(self) -> str: + return textwrap.dedent("""\ + Starts your application, restarting on file changes. + Usage: cpl start + """) + 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..4c9944c6 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,16 @@ class UninstallService(CommandABC): self._build_settings = build_settings self._project_settings = project_settings + @property + def help_message(self) -> str: + return textwrap.dedent("""\ + Uninstalls given package via pip + Usage: cpl uninstall + + Arguments: + package The package to uninstall + """) + 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..5cfb52de 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,13 @@ class UpdateService(CommandABC): self._project_settings = project_settings self._cli_settings = cli_settings + @property + def help_message(self) -> str: + return textwrap.dedent("""\ + Updates the CPL and project dependencies. + Usage: cpl update + """) + 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..5afadbd2 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,13 @@ class VersionService(CommandABC): """ CommandABC.__init__(self) + @property + def help_message(self) -> str: + return textwrap.dedent("""\ + Lists the version of CPL, CPL CLI and all installed packages from pip. + Usage: cpl version + """) + 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..3e0cf9c9 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) ) @@ -67,6 +69,9 @@ class Startup(StartupABC): self._configuration.add_console_argument(ConsoleArgument('', 'uninstall', ['ui', 'UI'], ' ')) self._configuration.add_console_argument(ConsoleArgument('', 'update', ['u', 'U'], '')) self._configuration.add_console_argument(ConsoleArgument('', 'version', ['v', 'V'], '')) + + self._configuration.add_console_argument(ConsoleArgument('', '--help', ['-h', '-H'], '')) + self._configuration.add_console_arguments(error=False) return self._configuration