Added version command to cli

This commit is contained in:
2020-12-16 14:35:43 +01:00
parent 88362006ef
commit 5fb20035d8
7 changed files with 87 additions and 11 deletions

View File

@@ -1,5 +1,7 @@
from abc import ABC, abstractmethod
from sh_edraft.console.console import Console
class CommandBase(ABC):

View File

@@ -1,8 +1,11 @@
import sys
import traceback
from sh_edraft.cli.cpl_cli.commands.help import Help
from sh_edraft.cli.cpl_cli.commands.new import New
from sh_edraft.cli.cpl_cli.commands.version import Version
from sh_edraft.cli.interpreter.interpreter import Interpreter
from sh_edraft.console.console import Console
class CLI:
@@ -13,14 +16,15 @@ class CLI:
def setup(self):
self._interpreter.add_command(New())
self._interpreter.add_command(Help())
self._interpreter.add_command(Version())
def main(self):
print('CPL CLI:')
string = ' '.join(sys.argv[1:])
try:
self._interpreter.interpret(string)
except Exception as e:
print(e)
tb = traceback.format_exc()
Console.error(str(e), tb)
def main():

View File

@@ -1,4 +1,5 @@
from sh_edraft.cli.command.base.command_base import CommandBase
from sh_edraft.console.console import Console
class Help(CommandBase):
@@ -7,4 +8,4 @@ class Help(CommandBase):
CommandBase.__init__(self)
def run(self, args: list[str]):
print('Commands:')
Console.write_line('Available Commands:')

View File

@@ -1,6 +1,7 @@
import os
from sh_edraft.cli.command.base.command_base import CommandBase
from sh_edraft.console.console import Console
class New(CommandBase):
@@ -10,13 +11,21 @@ class New(CommandBase):
def run(self, args: list[str]):
rel_path = f'{os.path.dirname(__file__)}/../'
if len(args) == 0:
Console.error(f'Expected arguments {args}')
return
elif len(args) != 2:
Console.error(f'Invalid arguments {args}')
return
if not os.path.isdir(f'{rel_path}/templates/{args[0]}'):
print(f'Unexpected argument {args[0]}')
Console.error(f'Unexpected argument {args[0]}')
sub_args = args[1:]
if len(sub_args) != 1:
print(f'Unexpected argument {sub_args[1]}')
Console.error(f'Unexpected argument {sub_args[1]}')
if not (sub_args[0].startswith('.') or sub_args[0].startswith('/')):
full_path = f'./{sub_args[0]}'

View File

@@ -0,0 +1,30 @@
import pkgutil
import sys
import platform
import sh_edraft
from sh_edraft import cli
from sh_edraft.cli.command.base.command_base import CommandBase
from sh_edraft.console.console import Console
class Version(CommandBase):
def __init__(self):
CommandBase.__init__(self)
def run(self, args: list[str]):
Console.set_foreground_color('yellow')
Console.banner('CPL CLI')
Console.set_foreground_color('default')
Console.write_line(f'Common Python Library CLI: {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('\nCPL:')
packages = []
for importer, modname, is_pkg in pkgutil.iter_modules(sh_edraft.__path__):
module = importer.find_module(modname).load_module(modname)
packages.append([f'{modname}:', module.__version__])
Console.table(['Name', 'Version'], packages)

View File

@@ -1,4 +1,5 @@
from sh_edraft.cli.command.base.command_base import CommandBase
from sh_edraft.console.console import Console
class Interpreter:
@@ -16,13 +17,12 @@ class Interpreter:
input_list = input_string.split(' ')
commands = [type(cmd).__name__.lower() for cmd in self._commands]
command = input_list[0]
args = input_list[1:]
print(command)
args = input_list[1:] if len(input_list) > 2 else []
if command in commands:
cmd = next((cmd for cmd in self._commands if type(cmd).__name__.lower() == command), None)
if cmd is not None:
cmd.run(args)
else:
print(f'Unexpected command {command}')
Console.error(f'Unexpected command {command}')
else:
print(f'Unexpected command {command}')
Console.error(f'Unexpected command {command}')