Improved cli

This commit is contained in:
Sven Heidemann 2020-12-16 13:38:08 +01:00
parent 97b4f516a8
commit 88362006ef
10 changed files with 146 additions and 11 deletions

View File

View File

@ -0,0 +1,10 @@
from abc import ABC, abstractmethod
class CommandBase(ABC):
@abstractmethod
def __init__(self): pass
@abstractmethod
def run(self, args: list[str]): pass

View File

@ -1,25 +1,24 @@
import sys
from sh_edraft.cli.cpl_cli.cli_commands import CLICommands
from sh_edraft.cli.cpl_cli.commands.help import Help
from sh_edraft.cli.cpl_cli.commands.new import New
from sh_edraft.cli.interpreter.interpreter import Interpreter
class CLI:
def __init__(self):
self._commands: dict = {}
self._interpreter = Interpreter()
def setup(self):
self._commands[CLICommands.new.__name__] = CLICommands.new
self._commands[CLICommands.help.__name__] = CLICommands.help
self._interpreter.add_command(New())
self._interpreter.add_command(Help())
def main(self):
args = sys.argv[1:]
print('CPL CLI:')
string = ' '.join(sys.argv[1:])
try:
cmd = self._commands[args[0]]
cmd(args[1:])
except KeyError:
CLICommands.unexpected_command(args[0])
self._interpreter.interpret(string)
except Exception as e:
print(e)

View File

@ -60,7 +60,11 @@ class CLICommands:
if name[0].islower():
name = f'{name[0].upper()}{name[1:]}'
if args[0] == 'configmodel':
if args[0] == 'base':
template_content = template_content.replace('$Name', f'{name}Base')
pyfile.write(template_content)
elif args[0] == 'configmodel':
template_content = template_content.replace('$Name', f'{name}Settings')
pyfile.write(template_content)

View File

@ -0,0 +1,10 @@
from sh_edraft.cli.command.base.command_base import CommandBase
class Help(CommandBase):
def __init__(self):
CommandBase.__init__(self)
def run(self, args: list[str]):
print('Commands:')

View File

@ -0,0 +1,84 @@
import os
from sh_edraft.cli.command.base.command_base import CommandBase
class New(CommandBase):
def __init__(self):
CommandBase.__init__(self)
def run(self, args: list[str]):
rel_path = f'{os.path.dirname(__file__)}/../'
if not os.path.isdir(f'{rel_path}/templates/{args[0]}'):
print(f'Unexpected argument {args[0]}')
sub_args = args[1:]
if len(sub_args) != 1:
print(f'Unexpected argument {sub_args[1]}')
if not (sub_args[0].startswith('.') or sub_args[0].startswith('/')):
full_path = f'./{sub_args[0]}'
else:
full_path = sub_args[0]
name = os.path.basename(full_path)
path = os.path.dirname(full_path)
if args[0] in ['base', 'class', 'configmodel', 'enum', 'service']:
if not os.path.isdir(path):
os.makedirs(path)
else:
if not os.path.isdir(full_path):
os.makedirs(full_path)
for r, d, f in os.walk(f'{rel_path}/templates/{args[0]}'):
for file in f:
template_content = ''
with open(f'{r}/{file}') as template:
template_content = template.read()
template.close()
file = file.replace('txt', 'py')
if args[0] in ['base', 'class', 'configmodel', 'enum', 'service']:
suffix = None
if args[0] == 'base':
suffix = 'base'
elif args[0] == 'configmodel':
suffix = 'settings'
elif args[0] == 'service':
suffix = 'service'
if suffix is not None:
file_path = f'{path}/{name}_{suffix}.py'
else:
file_path = f'{path}/{name}.py'
else:
file_path = f'{full_path}/{file}'
with open(file_path, 'w+') as pyfile:
if name[0].islower():
name = f'{name[0].upper()}{name[1:]}'
if args[0] == 'base':
template_content = template_content.replace('$Name', f'{name}Base')
pyfile.write(template_content)
elif args[0] == 'configmodel':
template_content = template_content.replace('$Name', f'{name}Settings')
pyfile.write(template_content)
elif args[0] == 'service':
template_content = template_content.replace('$Name', f'{name}Service')
template_content = template_content.replace('$Base', f'{name}Base')
pyfile.write(template_content)
else:
template_content = template_content.replace('$Name', name)
pyfile.write(template_content)
pyfile.close()

View File

@ -0,0 +1,28 @@
from sh_edraft.cli.command.base.command_base import CommandBase
class Interpreter:
def __init__(self):
self._commands: list[CommandBase] = []
def add_command(self, command: CommandBase):
self._commands.append(command)
def remove_command(self, command: CommandBase):
self._commands.remove(command)
def interpret(self, input_string: str):
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)
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}')
else:
print(f'Unexpected command {command}')