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:
@@ -0,0 +1,24 @@
|
||||
from argenta import Router, Response
|
||||
from argenta.command import Command, Flag, PossibleValues
|
||||
from argenta.command.flag import ValidationStatus
|
||||
|
||||
router = Router()
|
||||
|
||||
|
||||
@router.command(Command(
|
||||
"deploy",
|
||||
flags=Flag("verbose", possible_values=PossibleValues.NEITHER)
|
||||
))
|
||||
def deploy_handler(response: Response):
|
||||
# Check for toggle flag presence
|
||||
verbose_flag = response.input_flags.get_flag_by_name("verbose")
|
||||
|
||||
if verbose_flag and verbose_flag.status == ValidationStatus.VALID:
|
||||
print("Deploying with verbose output...")
|
||||
# Detailed logic
|
||||
elif verbose_flag and verbose_flag.status == ValidationStatus.INVALID:
|
||||
print("Incorrect flag value")
|
||||
return
|
||||
else:
|
||||
print("Deploying...")
|
||||
# Normal logic
|
||||
@@ -0,0 +1,16 @@
|
||||
from argenta import Router, Response
|
||||
from argenta.command import Command, Flag
|
||||
|
||||
router = Router()
|
||||
|
||||
|
||||
@router.command(Command("greet", flags=Flag("name")))
|
||||
def greet_handler(response: Response):
|
||||
# Get flag by name
|
||||
name_flag = response.input_flags.get_flag_by_name("name")
|
||||
|
||||
# Check if flag was passed
|
||||
if name_flag:
|
||||
print(f"Hello, {name_flag.input_value}!")
|
||||
else:
|
||||
print("Hello, stranger!")
|
||||
@@ -5,9 +5,9 @@ flag_with_value = InputFlag(
|
||||
)
|
||||
|
||||
flag_without_value = InputFlag(
|
||||
name="help", prefix="-", input_value=None, status=ValidationStatus.VALID
|
||||
name="help", prefix="-", input_value='', status=ValidationStatus.VALID
|
||||
)
|
||||
|
||||
# String representation includes value
|
||||
print(str(flag_with_value)) # --output result.txt
|
||||
print(str(flag_without_value)) # -help None
|
||||
print(str(flag_without_value)) # -help
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
import operator
|
||||
import re
|
||||
|
||||
from argenta import App, Orchestrator, Response, Router
|
||||
from argenta.app import DynamicDividingLine
|
||||
from argenta.command import Command, Flag, Flags
|
||||
from argenta.response.status import ResponseStatus
|
||||
|
||||
router = Router("Calculator")
|
||||
|
||||
operations = {
|
||||
'mul': operator.mul,
|
||||
'sub': operator.sub,
|
||||
'add': operator.add
|
||||
}
|
||||
|
||||
@router.command(
|
||||
Command(
|
||||
"calc",
|
||||
description="Calculator with two numbers",
|
||||
flags=Flags(
|
||||
[
|
||||
Flag("a", possible_values=re.compile(r"^\d{,5}$")), # First number
|
||||
Flag("b", possible_values=re.compile(r"^\d{,5}$")), # Second number
|
||||
Flag("operation", possible_values=["add", "sub", "mul"]), # Operation: add, sub, mul
|
||||
]
|
||||
),
|
||||
)
|
||||
)
|
||||
def calc_handler(response: Response):
|
||||
# Get flag values
|
||||
a_flag = response.input_flags.get_flag_by_name("a")
|
||||
b_flag = response.input_flags.get_flag_by_name("b")
|
||||
op_flag = response.input_flags.get_flag_by_name("op")
|
||||
|
||||
# Check that all flags are provided
|
||||
if response.status != ResponseStatus.ALL_FLAGS_VALID or not all([a_flag, b_flag, op_flag]):
|
||||
print("Error: must specify --a, --b and --op")
|
||||
return
|
||||
|
||||
a = float(a_flag.input_value)
|
||||
b = float(b_flag.input_value)
|
||||
operation = op_flag.input_value
|
||||
|
||||
try:
|
||||
result = operations[operation](a, b)
|
||||
except ZeroDivisionError:
|
||||
print("Can't divide by zero")
|
||||
else:
|
||||
print(f"Result: {result}")
|
||||
|
||||
|
||||
app = App(
|
||||
initial_message="Calculator",
|
||||
repeat_command_groups_printing=False,
|
||||
prompt=">> ",
|
||||
dividing_line=DynamicDividingLine("~"),
|
||||
)
|
||||
orchestrator = Orchestrator()
|
||||
|
||||
|
||||
def main():
|
||||
app.include_router(router)
|
||||
orchestrator.start_polling(app)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user