mirror of
https://github.com/koloideal/Argenta.git
synced 2026-06-10 10:05:28 +03:00
fix
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
---
|
||||
|
||||
## Описание
|
||||
**Argenta** — это библиотека для создания CLI-приложений на Python. Она предоставляет удобные инструменты для маршрутизации команд и обработки пользовательского ввода.
|
||||
**Argenta** — Python library for creating custom shells
|
||||
|
||||
---
|
||||
|
||||
@@ -19,19 +19,37 @@ poetry add argenta
|
||||
---
|
||||
|
||||
# Быстрый старт
|
||||
Пример базового CLI-приложения с Argenta:
|
||||
|
||||
```python
|
||||
# routers.py
|
||||
import re
|
||||
from argenta.router import Router
|
||||
from argenta.command import Command
|
||||
from argenta.command.params.flag import FlagsGroup, Flag
|
||||
|
||||
|
||||
router = Router()
|
||||
|
||||
@router.command("hello")
|
||||
def hello():
|
||||
|
||||
list_of_flags = [
|
||||
Flag(flag_name='host',
|
||||
flag_prefix='--',
|
||||
possible_flag_values=re.compile(r'^192.168.\d{1,3}.\d{1,3}$')),
|
||||
Flag(flag_name='port',
|
||||
flag_prefix='---',
|
||||
possible_flag_values=re.compile(r'^[0-9]{1,4}$'))
|
||||
]
|
||||
|
||||
|
||||
@router.command(Command("hello"))
|
||||
def handler():
|
||||
print("Hello, world!")
|
||||
|
||||
@router.unknown_command
|
||||
def unlnown_command(command):
|
||||
|
||||
@router.command(Command(command="ssh",
|
||||
description='connect via ssh',
|
||||
flags=FlagsGroup(list_of_flags)))
|
||||
def handler_with_flags(args: FlagsGroup):
|
||||
print(f'Command "{command}" undefined')
|
||||
```
|
||||
```python
|
||||
|
||||
@@ -11,7 +11,7 @@ class Flag:
|
||||
self.possible_flag_values = possible_flag_values
|
||||
self.ignore_flag_value_register = ignore_flag_value_register
|
||||
|
||||
self._value = None
|
||||
self._flag_value = None
|
||||
|
||||
def get_string_entity(self):
|
||||
string_entity: str = self._flag_prefix + self._flag_name
|
||||
@@ -24,10 +24,10 @@ class Flag:
|
||||
return self._flag_prefix
|
||||
|
||||
def get_value(self):
|
||||
return self._value
|
||||
return self._flag_value
|
||||
|
||||
def set_value(self, value):
|
||||
self._value = value
|
||||
self._flag_value = value
|
||||
|
||||
def validate_input_flag_value(self, input_flag_value: str):
|
||||
if isinstance(self.possible_flag_values, Pattern):
|
||||
|
||||
@@ -14,6 +14,17 @@ class FlagsGroup:
|
||||
def add_flags(self, flags: list[Flag]):
|
||||
self._flags.extend(flags)
|
||||
|
||||
def unparse_to_dict(self):
|
||||
result_dict: dict[str, dict] = {}
|
||||
for flag in self._flags:
|
||||
result_dict[flag.get_flag_name()] = {
|
||||
'name': flag.get_flag_name(),
|
||||
'string_entity': flag.get_string_entity(),
|
||||
'prefix': flag.get_flag_prefix(),
|
||||
'value': flag.get_value()
|
||||
}
|
||||
return result_dict
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self._flags)
|
||||
|
||||
|
||||
@@ -49,6 +49,7 @@ class Router:
|
||||
def input_command_handler(self, input_command: Command):
|
||||
input_command_name: str = input_command.get_string_entity()
|
||||
input_command_flags: FlagsGroup = input_command.get_input_flags()
|
||||
input_command_flags_dict: dict = input_command_flags.unparse_to_dict()
|
||||
for command_entity in self._command_entities:
|
||||
if input_command_name.lower() == command_entity['command'].get_string_entity().lower():
|
||||
if command_entity['command'].get_registered_flags():
|
||||
@@ -58,9 +59,9 @@ class Router:
|
||||
if not is_valid:
|
||||
self._not_valid_flag_handler(flag)
|
||||
return
|
||||
return command_entity['handler_func'](input_command_flags)
|
||||
return command_entity['handler_func'](input_command_flags_dict)
|
||||
else:
|
||||
return command_entity['handler_func'](FlagsGroup(None))
|
||||
return command_entity['handler_func']({})
|
||||
else:
|
||||
if input_command_flags:
|
||||
self._not_valid_flag_handler(input_command_flags[0])
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import re
|
||||
from pprint import pprint
|
||||
|
||||
from rich.console import Console
|
||||
|
||||
@@ -32,11 +33,9 @@ def command_help():
|
||||
|
||||
|
||||
@work_router.command(Command(command='P', description='Start Solving', flags=flagi))
|
||||
def command_start_solving(argrrtrts: FlagsGroup | None):
|
||||
def command_start_solving(argrrtrts: dict):
|
||||
print('Solving...')
|
||||
flags = argrrtrts.get_flags()
|
||||
for flag in flags:
|
||||
print(f'name: "{flag.get_string_entity()}", value: "{flag.get_value()}"')
|
||||
pprint(argrrtrts)
|
||||
#start_solving_command()
|
||||
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from tests.mock_app.handlers.routers import work_router, settings_router
|
||||
from mock.mock_app.handlers.routers import work_router, settings_router
|
||||
from art import text2art
|
||||
from rich.console import Console
|
||||
|
||||
|
||||
Reference in New Issue
Block a user