Added version and help cli command
This commit is contained in:
@@ -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:])
|
||||
|
21
src/cpl_cli/command.py
Normal file
21
src/cpl_cli/command.py
Normal file
@@ -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
|
22
src/cpl_cli/command_handler.py
Normal file
22
src/cpl_cli/command_handler.py
Normal file
@@ -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)
|
0
src/cpl_cli/commands/__init__.py
Normal file
0
src/cpl_cli/commands/__init__.py
Normal file
25
src/cpl_cli/commands/help.py
Normal file
25
src/cpl_cli/commands/help.py
Normal file
@@ -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')
|
43
src/cpl_cli/commands/version.py
Normal file
43
src/cpl_cli/commands/version.py
Normal file
@@ -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)
|
@@ -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
|
||||
|
Reference in New Issue
Block a user