Added version and help cli command

This commit is contained in:
Sven Heidemann 2021-03-03 19:37:35 +01:00
parent 7a2f4584b0
commit 2174cf3701
8 changed files with 170 additions and 29 deletions

View File

@ -22,8 +22,18 @@
<select /> <select />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="7e2256bc-a6b8-4880-83a6-8b0e3372d0a4" name="Default Changelist" comment="Added cli application"> <list default="true" id="7e2256bc-a6b8-4880-83a6-8b0e3372d0a4" name="Default Changelist" comment="Improved gitignore">
<change beforePath="$PROJECT_DIR$/.gitignore" beforeDir="false" afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" /> <change afterPath="$PROJECT_DIR$/src/cpl_cli/command.py" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/cpl_cli/command_handler.py" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/cpl_cli/commands/__init__.py" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/cpl_cli/commands/help.py" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/cpl_cli/commands/version.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/cpl/application/application_abc.py" beforeDir="false" afterPath="$PROJECT_DIR$/src/cpl/application/application_abc.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/cpl_cli/cli.py" beforeDir="false" afterPath="$PROJECT_DIR$/src/cpl_cli/cli.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/cpl_cli/startup.py" beforeDir="false" afterPath="$PROJECT_DIR$/src/cpl_cli/startup.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/tests/Application.py" beforeDir="false" afterPath="$PROJECT_DIR$/src/tests/application.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/tests/main.py" beforeDir="false" afterPath="$PROJECT_DIR$/src/tests/main.py" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -73,7 +83,7 @@
<component name="PropertiesComponent"> <component name="PropertiesComponent">
<property name="RunOnceActivity.OpenProjectViewOnStart" value="true" /> <property name="RunOnceActivity.OpenProjectViewOnStart" value="true" />
<property name="RunOnceActivity.ShowReadmeOnStart" value="true" /> <property name="RunOnceActivity.ShowReadmeOnStart" value="true" />
<property name="last_opened_file_path" value="$PROJECT_DIR$/src/cpl_cli" /> <property name="last_opened_file_path" value="$PROJECT_DIR$/../sh_python_line_counter" />
<property name="settings.editor.selected.configurable" value="com.jetbrains.python.configuration.PyActiveSdkModuleConfigurable" /> <property name="settings.editor.selected.configurable" value="com.jetbrains.python.configuration.PyActiveSdkModuleConfigurable" />
</component> </component>
<component name="ReaderModeSettings"> <component name="ReaderModeSettings">
@ -95,7 +105,7 @@
<recent name="$PROJECT_DIR$/src/cpl/common/application" /> <recent name="$PROJECT_DIR$/src/cpl/common/application" />
</key> </key>
</component> </component>
<component name="RunManager" selected="Python.main cli"> <component name="RunManager" selected="Python.cli version">
<configuration name="cli build" type="PythonConfigurationType" factoryName="Python"> <configuration name="cli build" type="PythonConfigurationType" factoryName="Python">
<module name="sh_common_py_lib" /> <module name="sh_common_py_lib" />
<option name="INTERPRETER_OPTIONS" value="" /> <option name="INTERPRETER_OPTIONS" value="" />
@ -167,11 +177,11 @@
<env name="PYTHONUNBUFFERED" value="1" /> <env name="PYTHONUNBUFFERED" value="1" />
</envs> </envs>
<option name="SDK_HOME" value="" /> <option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/src/sh_edraft/cli/cpl_cli" /> <option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/src/cpl_cli" />
<option name="IS_MODULE_SDK" value="true" /> <option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" /> <option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" /> <option name="ADD_SOURCE_ROOTS" value="true" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/src/sh_edraft/cli/cpl_cli/cli.py" /> <option name="SCRIPT_NAME" value="$PROJECT_DIR$/src/cpl_cli/main.py" />
<option name="PARAMETERS" value="help" /> <option name="PARAMETERS" value="help" />
<option name="SHOW_COMMAND_LINE" value="false" /> <option name="SHOW_COMMAND_LINE" value="false" />
<option name="EMULATE_TERMINAL" value="true" /> <option name="EMULATE_TERMINAL" value="true" />
@ -335,11 +345,11 @@
<env name="PYTHONUNBUFFERED" value="1" /> <env name="PYTHONUNBUFFERED" value="1" />
</envs> </envs>
<option name="SDK_HOME" value="" /> <option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/src/sh_edraft/cli/cpl_cli" /> <option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/src/cpl_cli" />
<option name="IS_MODULE_SDK" value="true" /> <option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" /> <option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" /> <option name="ADD_SOURCE_ROOTS" value="true" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/src/sh_edraft/cli/cpl_cli/cli.py" /> <option name="SCRIPT_NAME" value="$PROJECT_DIR$/src/cpl_cli/main.py" />
<option name="PARAMETERS" value="version" /> <option name="PARAMETERS" value="version" />
<option name="SHOW_COMMAND_LINE" value="false" /> <option name="SHOW_COMMAND_LINE" value="false" />
<option name="EMULATE_TERMINAL" value="true" /> <option name="EMULATE_TERMINAL" value="true" />
@ -382,7 +392,7 @@
<option name="ADD_CONTENT_ROOTS" value="true" /> <option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" /> <option name="ADD_SOURCE_ROOTS" value="true" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/src/cpl_cli/main.py" /> <option name="SCRIPT_NAME" value="$PROJECT_DIR$/src/cpl_cli/main.py" />
<option name="PARAMETERS" value="v -test -v:test" /> <option name="PARAMETERS" value="" />
<option name="SHOW_COMMAND_LINE" value="false" /> <option name="SHOW_COMMAND_LINE" value="false" />
<option name="EMULATE_TERMINAL" value="false" /> <option name="EMULATE_TERMINAL" value="false" />
<option name="MODULE_MODE" value="false" /> <option name="MODULE_MODE" value="false" />
@ -484,13 +494,6 @@
<option name="presentableId" value="Default" /> <option name="presentableId" value="Default" />
<updated>1605881914521</updated> <updated>1605881914521</updated>
</task> </task>
<task id="LOCAL-00035" summary="Added console module">
<created>1607976456889</created>
<option name="number" value="00035" />
<option name="presentableId" value="LOCAL-00035" />
<option name="project" value="LOCAL" />
<updated>1607976456889</updated>
</task>
<task id="LOCAL-00036" summary="Added console module"> <task id="LOCAL-00036" summary="Added console module">
<created>1607976626173</created> <created>1607976626173</created>
<option name="number" value="00036" /> <option name="number" value="00036" />
@ -827,7 +830,14 @@
<option name="project" value="LOCAL" /> <option name="project" value="LOCAL" />
<updated>1614793074976</updated> <updated>1614793074976</updated>
</task> </task>
<option name="localTasksCounter" value="84" /> <task id="LOCAL-00084" summary="Improved gitignore">
<created>1614793114077</created>
<option name="number" value="00084" />
<option name="presentableId" value="LOCAL-00084" />
<option name="project" value="LOCAL" />
<updated>1614793114078</updated>
</task>
<option name="localTasksCounter" value="85" />
<servers /> <servers />
</component> </component>
<component name="Vcs.Log.Tabs.Properties"> <component name="Vcs.Log.Tabs.Properties">
@ -843,7 +853,6 @@
<option name="oldMeFiltersMigrated" value="true" /> <option name="oldMeFiltersMigrated" value="true" />
</component> </component>
<component name="VcsManagerConfiguration"> <component name="VcsManagerConfiguration">
<MESSAGE value="Improved help command" />
<MESSAGE value="Changed deps" /> <MESSAGE value="Changed deps" />
<MESSAGE value="Removed publisher.py" /> <MESSAGE value="Removed publisher.py" />
<MESSAGE value="Added first steps of database command" /> <MESSAGE value="Added first steps of database command" />
@ -868,7 +877,8 @@
<MESSAGE value="Refactored code" /> <MESSAGE value="Refactored code" />
<MESSAGE value="Improved configuration" /> <MESSAGE value="Improved configuration" />
<MESSAGE value="Added cli application" /> <MESSAGE value="Added cli application" />
<option name="LAST_COMMIT_MESSAGE" value="Added cli application" /> <MESSAGE value="Improved gitignore" />
<option name="LAST_COMMIT_MESSAGE" value="Improved gitignore" />
</component> </component>
<component name="XDebuggerManager"> <component name="XDebuggerManager">
<breakpoint-manager> <breakpoint-manager>

View File

@ -1,5 +1,11 @@
from typing import Optional
from cpl.application.application_abc import ApplicationABC from cpl.application.application_abc import ApplicationABC
from cpl.console.console import Console 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): class CLI(ApplicationABC):
@ -7,14 +13,21 @@ class CLI(ApplicationABC):
def __init__(self): def __init__(self):
ApplicationABC.__init__(self) ApplicationABC.__init__(self)
def configure(self): self._command_handler: Optional[CommandHandler] = None
if self._services is None:
Console.error('Service provider is empty')
exit()
if self._configuration is None: def configure(self):
Console.error('Configuration is empty') self._command_handler: CommandHandler = self._services.get_service(CommandHandler)
exit()
# 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): 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
View 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

View 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)

View File

View 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')

View 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)

View File

@ -5,6 +5,9 @@ from cpl.application.application_host_abc import ApplicationHostABC
from cpl.application.startup_abc import StartupABC from cpl.application.startup_abc import StartupABC
from cpl.configuration.configuration_abc import ConfigurationABC from cpl.configuration.configuration_abc import ConfigurationABC
from cpl.dependency_injection.service_provider_base import ServiceProviderABC 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): class Startup(StartupABC):
@ -25,12 +28,16 @@ class Startup(StartupABC):
def create_configuration(self) -> ConfigurationABC: def create_configuration(self) -> ConfigurationABC:
self._configuration.add_environment_variables('PYTHON_') self._configuration.add_environment_variables('PYTHON_')
self._configuration.add_environment_variables('CPL_') 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('', 'version', ['v'], '')
self._configuration.add_console_argument('-', 'test', ['t'], '')
self._configuration.add_console_argument('-', 'var', ['v'], ':')
self._configuration.add_console_arguments() self._configuration.add_console_arguments()
return self._configuration return self._configuration
def create_services(self) -> ServiceProviderABC: 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 return self._services