diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 686eb2d4..bab0c3b4 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -22,8 +22,18 @@
-
-
+
+
+
+
+
+
+
+
+
+
+
+
@@ -73,7 +83,7 @@
-
+
@@ -95,7 +105,7 @@
-
+
@@ -167,11 +177,11 @@
-
+
-
+
@@ -335,11 +345,11 @@
-
+
-
+
@@ -382,7 +392,7 @@
-
+
@@ -484,13 +494,6 @@
1605881914521
-
- 1607976456889
-
-
-
- 1607976456889
-
1607976626173
@@ -827,7 +830,14 @@
1614793074976
-
+
+ 1614793114077
+
+
+
+ 1614793114078
+
+
@@ -843,7 +853,6 @@
-
@@ -868,7 +877,8 @@
-
+
+
diff --git a/src/cpl_cli/cli.py b/src/cpl_cli/cli.py
index f156aeea..857cb039 100644
--- a/src/cpl_cli/cli.py
+++ b/src/cpl_cli/cli.py
@@ -1,5 +1,11 @@
+from typing import Optional
+
from cpl.application.application_abc import ApplicationABC
from cpl.console.console import Console
+from cpl_cli.command import Command
+from cpl_cli.command_handler import CommandHandler
+from cpl_cli.commands.help import Help
+from cpl_cli.commands.version import Version
class CLI(ApplicationABC):
@@ -7,14 +13,21 @@ class CLI(ApplicationABC):
def __init__(self):
ApplicationABC.__init__(self)
- def configure(self):
- if self._services is None:
- Console.error('Service provider is empty')
- exit()
+ self._command_handler: Optional[CommandHandler] = None
- if self._configuration is None:
- Console.error('Configuration is empty')
- exit()
+ def configure(self):
+ self._command_handler: CommandHandler = self._services.get_service(CommandHandler)
+
+ # self._command_handler.add_command(Command('build', ['b', 'B']))
+ self._command_handler.add_command(Command('help', ['h', 'H'], self._services.get_service(Help)))
+ # self._command_handler.add_command(Command('new', ['n', 'N']))
+ # self._command_handler.add_command(Command('publish', ['p', 'P']))
+ self._command_handler.add_command(Command('version', ['v', 'V'], self._services.get_service(Version)))
def main(self):
- Console.write_line(self._configuration)
+ if len(self._configuration.additional_arguments) < 1:
+ Console.error(f'Expected command')
+ Console.error('Run \'cpl help\'')
+ return
+
+ self._command_handler.handle(self._configuration.additional_arguments[0], self._configuration.additional_arguments[1:])
diff --git a/src/cpl_cli/command.py b/src/cpl_cli/command.py
new file mode 100644
index 00000000..64de906d
--- /dev/null
+++ b/src/cpl_cli/command.py
@@ -0,0 +1,21 @@
+from cpl.dependency_injection.service_abc import ServiceABC
+
+
+class Command:
+
+ def __init__(self, name: str, aliases: list[str], command: ServiceABC):
+ self._name = name
+ self._aliases = aliases
+ self._command = command
+
+ @property
+ def name(self):
+ return self._name
+
+ @property
+ def aliases(self):
+ return self._aliases
+
+ @property
+ def command(self):
+ return self._command
diff --git a/src/cpl_cli/command_handler.py b/src/cpl_cli/command_handler.py
new file mode 100644
index 00000000..7e885b3c
--- /dev/null
+++ b/src/cpl_cli/command_handler.py
@@ -0,0 +1,22 @@
+from cpl.dependency_injection.service_abc import ServiceABC
+from cpl_cli.command import Command
+
+
+class CommandHandler(ServiceABC):
+
+ def __init__(self):
+ ServiceABC.__init__(self)
+
+ self._commands: list[Command] = []
+
+ def add_command(self, cmd: Command):
+ self._commands.append(cmd)
+
+ def remove_command(self, cmd: Command):
+ 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:
+ print(command.command)
+ command.command.run(args)
diff --git a/src/cpl_cli/commands/__init__.py b/src/cpl_cli/commands/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/src/cpl_cli/commands/help.py b/src/cpl_cli/commands/help.py
new file mode 100644
index 00000000..927be3c4
--- /dev/null
+++ b/src/cpl_cli/commands/help.py
@@ -0,0 +1,25 @@
+from cpl.console.console import Console
+from cpl.dependency_injection.service_abc import ServiceABC
+
+
+class Help(ServiceABC):
+
+ def __init__(self):
+ ServiceABC.__init__(self)
+
+ def run(self, args: list[str]):
+ Console.write_line('Available Commands:')
+ commands = [
+ ['build (-b|-B)', 'Prepares files for publishing into an output directory named dist/ at the given output path. Must be executed from within a workspace directory.'],
+ ['help (-h|-H)', 'Lists available commands and their short descriptions.'],
+ ['new', 'Creates a new file or package.'],
+ ['publish (-p|-P)', 'Prepares files for publishing into an output directory named dist/ at the given output path and executes setup.py. Must be executed from within a workspace directory.'],
+ ['version (-v|-V)', 'Outputs CPL CLI version.']
+ ]
+ for name, description in commands:
+ Console.set_foreground_color('blue')
+ Console.write(f'\n\t{name} ')
+ Console.set_foreground_color('default')
+ Console.write(f'{description}')
+
+ Console.write('\n')
diff --git a/src/cpl_cli/commands/version.py b/src/cpl_cli/commands/version.py
new file mode 100644
index 00000000..8fd34be6
--- /dev/null
+++ b/src/cpl_cli/commands/version.py
@@ -0,0 +1,43 @@
+import os
+import pkgutil
+import sys
+import platform
+import pkg_resources
+
+import cpl
+import cpl_cli
+from cpl.console.console import Console
+from cpl.dependency_injection.service_abc import ServiceABC
+
+
+class Version(ServiceABC):
+
+ def __init__(self):
+ ServiceABC.__init__(self)
+
+ def run(self, args: list[str]):
+ Console.set_foreground_color('yellow')
+ Console.banner('CPL CLI')
+ Console.set_foreground_color('default')
+ if '__version__' in dir(cpl_cli):
+ Console.write_line(f'Common Python Library CLI: {cpl_cli.__version__}')
+
+ Console.write_line(f'Python: {sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}')
+ Console.write_line(f'OS: {platform.system()} {platform.processor()}')
+
+ Console.write_line('CPL:')
+ packages = []
+ for importer, modname, is_pkg in pkgutil.iter_modules(cpl.__path__):
+ module = importer.find_module(modname).load_module(modname)
+ if '__version__' in dir(module):
+ packages.append([f'{modname}', module.__version__])
+
+ Console.table(['Name', 'Version'], packages)
+
+ Console.write_line('\nPython Packages:')
+ packages = []
+ dependencies = dict(tuple(str(ws).split()) for ws in pkg_resources.working_set)
+ for p in dependencies:
+ packages.append([p, dependencies[p]])
+
+ Console.table(['Name', 'Version'], packages)
diff --git a/src/cpl_cli/startup.py b/src/cpl_cli/startup.py
index f42e54c0..fea06926 100644
--- a/src/cpl_cli/startup.py
+++ b/src/cpl_cli/startup.py
@@ -5,6 +5,9 @@ from cpl.application.application_host_abc import ApplicationHostABC
from cpl.application.startup_abc import StartupABC
from cpl.configuration.configuration_abc import ConfigurationABC
from cpl.dependency_injection.service_provider_base import ServiceProviderABC
+from cpl_cli.command_handler import CommandHandler
+from cpl_cli.commands.help import Help
+from cpl_cli.commands.version import Version
class Startup(StartupABC):
@@ -25,12 +28,16 @@ class Startup(StartupABC):
def create_configuration(self) -> ConfigurationABC:
self._configuration.add_environment_variables('PYTHON_')
self._configuration.add_environment_variables('CPL_')
+ self._configuration.add_console_argument('', 'help', ['h'], '')
self._configuration.add_console_argument('', 'version', ['v'], '')
- self._configuration.add_console_argument('-', 'test', ['t'], '')
- self._configuration.add_console_argument('-', 'var', ['v'], ':')
self._configuration.add_console_arguments()
return self._configuration
def create_services(self) -> ServiceProviderABC:
+ self._services.add_singleton(CommandHandler, CommandHandler)
+
+ self._services.add_scoped(Help, Help)
+ self._services.add_scoped(Version, Version)
+
return self._services