Add auto_args
This commit is contained in:
40
examples/auto_args_group.py
Normal file
40
examples/auto_args_group.py
Normal file
@ -0,0 +1,40 @@
|
||||
import asyncio
|
||||
|
||||
from falyx import Action, ActionGroup, Command, Falyx
|
||||
|
||||
|
||||
# Define a shared async function
|
||||
async def say_hello(name: str, excited: bool = False):
|
||||
if excited:
|
||||
print(f"Hello, {name}!!!")
|
||||
else:
|
||||
print(f"Hello, {name}.")
|
||||
|
||||
|
||||
# Wrap the same callable in multiple Actions
|
||||
action1 = Action("say_hello_1", action=say_hello)
|
||||
action2 = Action("say_hello_2", action=say_hello)
|
||||
action3 = Action("say_hello_3", action=say_hello)
|
||||
|
||||
# Combine into an ActionGroup
|
||||
group = ActionGroup(name="greet_group", actions=[action1, action2, action3])
|
||||
|
||||
# Create the Command with auto_args=True
|
||||
cmd = Command(
|
||||
key="G",
|
||||
description="Greet someone with multiple variations.",
|
||||
action=group,
|
||||
auto_args=True,
|
||||
arg_metadata={
|
||||
"name": {
|
||||
"help": "The name of the person to greet.",
|
||||
},
|
||||
"excited": {
|
||||
"help": "Whether to greet excitedly.",
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
flx = Falyx("Test Group")
|
||||
flx.add_command_from_command(cmd)
|
||||
asyncio.run(flx.run())
|
32
examples/auto_parse_demo.py
Normal file
32
examples/auto_parse_demo.py
Normal file
@ -0,0 +1,32 @@
|
||||
import asyncio
|
||||
|
||||
from falyx import Action, Falyx
|
||||
|
||||
|
||||
async def deploy(service: str, region: str = "us-east-1", verbose: bool = False):
|
||||
if verbose:
|
||||
print(f"Deploying {service} to {region}...")
|
||||
await asyncio.sleep(2)
|
||||
if verbose:
|
||||
print(f"{service} deployed successfully!")
|
||||
|
||||
|
||||
flx = Falyx("Deployment CLI")
|
||||
|
||||
flx.add_command(
|
||||
key="D",
|
||||
aliases=["deploy"],
|
||||
description="Deploy a service to a specified region.",
|
||||
action=Action(
|
||||
name="deploy_service",
|
||||
action=deploy,
|
||||
),
|
||||
auto_args=True,
|
||||
arg_metadata={
|
||||
"service": "Service name",
|
||||
"region": {"help": "Deployment region", "choices": ["us-east-1", "us-west-2"]},
|
||||
"verbose": {"help": "Enable verbose mode"},
|
||||
},
|
||||
)
|
||||
|
||||
asyncio.run(flx.run())
|
Reference in New Issue
Block a user