add Enum PossibleValues for bool values as values of possible_values argument in Flag

This commit is contained in:
2025-05-22 12:10:32 +03:00
parent 365347ea7f
commit bebd84969b
7 changed files with 40 additions and 35 deletions
+9 -18
View File
@@ -1,22 +1,13 @@
from argenta.router import Router from enum import Enum
from argenta.command import Command from typing import Literal
from argenta.response import Response
from argenta.metrics import get_time_of_pre_cycle_setup
from argenta.response.status import Status
from argenta.command.flag import Flag, Flags
from argenta.app import App
from argenta.orchestrator import Orchestrator
router = Router() class PossibleValues(Enum):
DISABLE: Literal[False] = False
ALL: Literal[True] = True
def __eq__(self, other: bool) -> bool:
return self.value == other
@router.command(Command("case are")) print(PossibleValues.DISABLE == False)
def handler(response: Response):
print(response.status)
app = App(repeat_command_groups=False)
app.include_router(router)
app.run_polling()
+3 -1
View File
@@ -2,7 +2,7 @@ from rich.console import Console
from argenta.command import Command from argenta.command import Command
from argenta.command.flag.defaults import PredefinedFlags from argenta.command.flag.defaults import PredefinedFlags
from argenta.command.flag import Flags from argenta.command.flag import Flags, Flag, PossibleValues
from argenta.response import Response from argenta.response import Response
from argenta.router import Router from argenta.router import Router
@@ -11,6 +11,8 @@ work_router: Router = Router(title="Work points:", disable_redirect_stdout=True)
console = Console() console = Console()
flag = Flag('csdv', possible_values=PossibleValues.DISABLE)
@work_router.command( @work_router.command(
Command( Command(
+2 -2
View File
@@ -4,11 +4,11 @@ __all__ = [
"UndefinedInputFlags", "UndefinedInputFlags",
"ValidInputFlags", "ValidInputFlags",
"InvalidValueInputFlags", "InvalidValueInputFlags",
"Flags", "Flags", "PossibleValues"
] ]
from argenta.command.flag.models import Flag, InputFlag from argenta.command.flag.models import Flag, InputFlag, PossibleValues
from argenta.command.flag.flags.models import ( from argenta.command.flag.flags.models import (
UndefinedInputFlags, UndefinedInputFlags,
ValidInputFlags, ValidInputFlags,
+8 -7
View File
@@ -1,22 +1,23 @@
from dataclasses import dataclass from dataclasses import dataclass
from argenta.command.flag.models import Flag from argenta.command.flag.models import Flag, PossibleValues
import re import re
@dataclass @dataclass
class PredefinedFlags: class PredefinedFlags:
""" """
Public. A dataclass with predefined flags and most frequently used flags for quick use Public. A dataclass with predefined flags and most frequently used flags for quick use
""" """
HELP = Flag(name="help", possible_values=False) HELP = Flag(name="help", possible_values=PossibleValues.DISABLE)
SHORT_HELP = Flag(name="H", prefix="-", possible_values=False) SHORT_HELP = Flag(name="H", prefix="-", possible_values=PossibleValues.DISABLE)
INFO = Flag(name="info", possible_values=False) INFO = Flag(name="info", possible_values=PossibleValues.DISABLE)
SHORT_INFO = Flag(name="I", prefix="-", possible_values=False) SHORT_INFO = Flag(name="I", prefix="-", possible_values=PossibleValues.DISABLE)
ALL = Flag(name="all", possible_values=False) ALL = Flag(name="all", possible_values=PossibleValues.DISABLE)
SHORT_ALL = Flag(name="A", prefix="-", possible_values=False) SHORT_ALL = Flag(name="A", prefix="-", possible_values=PossibleValues.DISABLE)
HOST = Flag( HOST = Flag(
name="host", possible_values=re.compile(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$") name="host", possible_values=re.compile(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$")
+12 -2
View File
@@ -1,6 +1,16 @@
from enum import Enum
from typing import Literal, Pattern from typing import Literal, Pattern
class PossibleValues(Enum):
DISABLE: Literal[False] = False
ALL: Literal[True] = True
def __eq__(self, other: bool) -> bool:
return self.value == other
class BaseFlag: class BaseFlag:
def __init__(self, name: str, prefix: Literal["-", "--", "---"] = "--") -> None: def __init__(self, name: str, prefix: Literal["-", "--", "---"] = "--") -> None:
""" """
@@ -43,7 +53,7 @@ class Flag(BaseFlag):
self, self,
name: str, name: str,
prefix: Literal["-", "--", "---"] = "--", prefix: Literal["-", "--", "---"] = "--",
possible_values: list[str] | Pattern[str] | bool = True, possible_values: list[str] | Pattern[str] | PossibleValues = PossibleValues.ALL,
) -> None: ) -> None:
""" """
Public. The entity of the flag being registered for subsequent processing Public. The entity of the flag being registered for subsequent processing
@@ -61,7 +71,7 @@ class Flag(BaseFlag):
:param input_flag_value: The input flag value to validate :param input_flag_value: The input flag value to validate
:return: whether the entered flag is valid as bool :return: whether the entered flag is valid as bool
""" """
if self.possible_values is False: if self.possible_values == PossibleValues.DISABLE:
if input_flag_value is None: if input_flag_value is None:
return True return True
else: else:
+1
View File
@@ -91,6 +91,7 @@ class Command(BaseCommand):
is_valid = registered_flag.validate_input_flag_value( is_valid = registered_flag.validate_input_flag_value(
flag.get_value() flag.get_value()
) )
if is_valid: if is_valid:
return "Valid" return "Valid"
else: else:
+5 -5
View File
@@ -1,4 +1,4 @@
from argenta.command.flag import Flag, InputFlag from argenta.command.flag import Flag, InputFlag, PossibleValues
from argenta.command.flag.flags import InputFlags, Flags from argenta.command.flag.flags import InputFlags, Flags
import unittest import unittest
@@ -54,19 +54,19 @@ class TestFlag(unittest.TestCase):
self.assertEqual(flag.validate_input_flag_value('192.168.9.8'), True) self.assertEqual(flag.validate_input_flag_value('192.168.9.8'), True)
def test_validate_correct_empty_flag_value_without_possible_flag_values(self): def test_validate_correct_empty_flag_value_without_possible_flag_values(self):
flag = Flag(name='test', possible_values=False) flag = Flag(name='test', possible_values=PossibleValues.DISABLE)
self.assertEqual(flag.validate_input_flag_value(None), True) self.assertEqual(flag.validate_input_flag_value(None), True)
def test_validate_correct_empty_flag_value_with_possible_flag_values(self): def test_validate_correct_empty_flag_value_with_possible_flag_values(self):
flag = Flag(name='test', possible_values=True) flag = Flag(name='test', possible_values=PossibleValues.DISABLE)
self.assertEqual(flag.validate_input_flag_value(None), True) self.assertEqual(flag.validate_input_flag_value(None), True)
def test_validate_incorrect_random_flag_value_without_possible_flag_values(self): def test_validate_incorrect_random_flag_value_without_possible_flag_values(self):
flag = Flag(name='test', possible_values=False) flag = Flag(name='test', possible_values=PossibleValues.DISABLE)
self.assertEqual(flag.validate_input_flag_value('random value'), False) self.assertEqual(flag.validate_input_flag_value('random value'), False)
def test_validate_correct_random_flag_value_with_possible_flag_values(self): def test_validate_correct_random_flag_value_with_possible_flag_values(self):
flag = Flag(name='test', possible_values=True) flag = Flag(name='test', possible_values=PossibleValues.ALL)
self.assertEqual(flag.validate_input_flag_value('random value'), True) self.assertEqual(flag.validate_input_flag_value('random value'), True)
def test_get_input_flag1(self): def test_get_input_flag1(self):