mirror of
https://github.com/koloideal/Argenta.git
synced 2026-06-10 10:05:28 +03:00
Update documentation and code snippets
This commit is contained in:
@@ -6,6 +6,7 @@ app = App(
|
||||
prompt=">> ",
|
||||
initial_message="Simple App",
|
||||
farewell_message="Goodbye!",
|
||||
repeat_command_groups_printing=False
|
||||
)
|
||||
orchestrator = Orchestrator()
|
||||
|
||||
@@ -14,11 +15,11 @@ main_router = Router(title="Main commands")
|
||||
|
||||
|
||||
# 3. Define command and its handler
|
||||
@main_router.command(
|
||||
Command(
|
||||
"hello", description="Prints greeting message", flags=Flag("name")
|
||||
)
|
||||
)
|
||||
@main_router.command(Command(
|
||||
"hello",
|
||||
description="Prints greeting message",
|
||||
flags=Flag("name")
|
||||
))
|
||||
def hello_handler(response: Response):
|
||||
"""This handler will be called for 'hello' command."""
|
||||
name = response.input_flags.get_flag_by_name("name")
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from typing import cast
|
||||
|
||||
from argenta import Command, Response, Router
|
||||
from argenta.command.flag import ValidationStatus, Flag, Flags
|
||||
from argenta.command.flag import Flag, Flags, ValidationStatus
|
||||
from argenta.di import FromDishka
|
||||
|
||||
from .repository import Priority, Task, TaskRepository
|
||||
@@ -9,26 +9,25 @@ from .repository import Priority, Task, TaskRepository
|
||||
router = Router(title="Task Manager")
|
||||
|
||||
|
||||
@router.command(
|
||||
Command(
|
||||
@router.command(Command(
|
||||
"add-task",
|
||||
description="Add a new task",
|
||||
flags=Flags(
|
||||
[
|
||||
flags=Flags([
|
||||
Flag("description"),
|
||||
Flag("priority", possible_values=["low", "medium", "high"]),
|
||||
]
|
||||
),
|
||||
)
|
||||
)
|
||||
]),
|
||||
))
|
||||
def add_task(response: Response, repo: FromDishka[TaskRepository]):
|
||||
description_flag = response.input_flags.get_flag_by_name("description")
|
||||
|
||||
if not description_flag or not description_flag.status == ValidationStatus.VALID:
|
||||
print("Error: --description flag is required.")
|
||||
return
|
||||
|
||||
task_description = description_flag.input_value or ""
|
||||
|
||||
priority_flag = response.input_flags.get_flag_by_name("priority")
|
||||
|
||||
if priority_flag and priority_flag.status == ValidationStatus.VALID:
|
||||
priority_value = priority_flag.input_value
|
||||
else:
|
||||
@@ -38,12 +37,14 @@ def add_task(response: Response, repo: FromDishka[TaskRepository]):
|
||||
|
||||
task = Task(description=task_description, priority=priority)
|
||||
repo.add_task(task)
|
||||
|
||||
print(f"Added task: '{task.description}' with priority '{task.priority}'")
|
||||
|
||||
|
||||
@router.command(Command("list-tasks", description="List all tasks"))
|
||||
def list_tasks(response: Response, repo: FromDishka[TaskRepository]):
|
||||
tasks = repo.get_all_tasks()
|
||||
|
||||
if not tasks:
|
||||
print("No tasks found.")
|
||||
return
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
|
||||
**Результат**
|
||||
|
||||
.. image:: https://i.ibb.co/JwK9Vv4j/2025-11-03-135118.png
|
||||
.. image:: https://i.ibb.co/35q24Bh8/image.png
|
||||
:alt: Simple App Example
|
||||
|
||||
-----
|
||||
@@ -43,7 +43,7 @@
|
||||
|
||||
2. **Определение моделей данных и репозитория**
|
||||
|
||||
Сначала определим модели данных для задачи и репозиторий для их хранения. Это будет наша "бизнес-логика".
|
||||
Сначала определим модели данных для задачи и репозиторий для их хранения.
|
||||
|
||||
.. literalinclude:: ../code_snippets/quickstart/task_manager/repository.py
|
||||
:language: python
|
||||
|
||||
+10
-10
@@ -1,11 +1,11 @@
|
||||
from argenta.orchestrator.argparser import ArgSpace, BooleanArgument, ValueArgument
|
||||
from argenta.orchestrator.argparser.arguments import InputArgument
|
||||
from argenta import Router, Command
|
||||
|
||||
argspace = ArgSpace([
|
||||
InputArgument(name="arg1", value="val1", founder_class=ValueArgument),
|
||||
InputArgument(name="arg2", value=True, founder_class=BooleanArgument),
|
||||
InputArgument(name="arg3", value="val3", founder_class=ValueArgument),
|
||||
])
|
||||
|
||||
print(argspace._name_object_paired_args)
|
||||
print(argspace.get_by_type(ValueArgument))
|
||||
router = Router()
|
||||
@router.command(Command('some', aliases=['test', 'case']))
|
||||
def handler(response): # pyright: ignore[reportUnusedFunction]
|
||||
pass
|
||||
@router.command(Command('ext', aliases=['more', 'foo']))
|
||||
def handler2(response): # pyright: ignore[reportUnusedFunction]
|
||||
pass
|
||||
|
||||
print(router.aliases)
|
||||
@@ -43,14 +43,14 @@ class Flag:
|
||||
"""
|
||||
if self.possible_values == PossibleValues.NEITHER:
|
||||
return input_flag_value is None
|
||||
|
||||
if self.possible_values == PossibleValues.ALL:
|
||||
return input_flag_value is not None
|
||||
|
||||
if isinstance(self.possible_values, Pattern):
|
||||
return isinstance(input_flag_value, str) and bool(self.possible_values.match(input_flag_value))
|
||||
|
||||
if isinstance(self.possible_values, list):
|
||||
return input_flag_value in self.possible_values
|
||||
|
||||
return True
|
||||
return input_flag_value in self.possible_values
|
||||
|
||||
@property
|
||||
def string_entity(self) -> str:
|
||||
|
||||
@@ -39,7 +39,7 @@ class Command:
|
||||
self.registered_flags: Flags = flags if isinstance(flags, Flags) else Flags([flags])
|
||||
self.trigger: str = trigger
|
||||
self.description: str = description
|
||||
self.aliases: list[str] = []
|
||||
self.aliases: list[str] = aliases
|
||||
|
||||
def validate_input_flag(self, flag: InputFlag) -> ValidationStatus:
|
||||
"""
|
||||
|
||||
@@ -26,13 +26,13 @@ class TestInputCommand(unittest.TestCase):
|
||||
with self.assertRaises(EmptyInputCommandException):
|
||||
InputCommand.parse('')
|
||||
|
||||
def test_validate_valid_input_flag1(self):
|
||||
def test_validate_invalid_input_flag1(self):
|
||||
command = Command('some', flags=Flag('test'))
|
||||
self.assertEqual(command.validate_input_flag(InputFlag('test', input_value=None, status=None)), ValidationStatus.VALID)
|
||||
self.assertEqual(command.validate_input_flag(InputFlag('test', input_value=None, status=None)), ValidationStatus.INVALID)
|
||||
|
||||
def test_validate_valid_input_flag2(self):
|
||||
command = Command('some', flags=Flags([Flag('test'), Flag('more')]))
|
||||
self.assertEqual(command.validate_input_flag(InputFlag('more', input_value=None, status=None)), ValidationStatus.VALID)
|
||||
self.assertEqual(command.validate_input_flag(InputFlag('more', input_value='random-value', status=None)), ValidationStatus.VALID)
|
||||
|
||||
def test_validate_undefined_input_flag1(self):
|
||||
command = Command('some', flags=Flag('test'))
|
||||
@@ -61,4 +61,3 @@ class TestInputCommand(unittest.TestCase):
|
||||
def test_isinstance_parse_correct_raw_command(self):
|
||||
cmd = InputCommand.parse('ssh --host 192.168.0.3')
|
||||
self.assertIsInstance(cmd, InputCommand)
|
||||
|
||||
|
||||
@@ -7,8 +7,7 @@ from argenta.command.flag.flags import Flags, InputFlags
|
||||
from argenta.command.flag.models import PossibleValues, ValidationStatus
|
||||
from argenta.response.entity import Response
|
||||
from argenta.router import Router
|
||||
from argenta.router.entity import ( # pyright: ignore[reportPrivateUsage]
|
||||
_structuring_input_flags, _validate_command, _validate_func_args)
|
||||
from argenta.router.entity import _structuring_input_flags, _validate_command, _validate_func_args # pyright: ignore[reportPrivateUsage]
|
||||
from argenta.router.exceptions import (RepeatedFlagNameException,
|
||||
RequiredArgumentNotPassedException,
|
||||
TriggerContainSpacesException)
|
||||
|
||||
Reference in New Issue
Block a user