Alconna 隶属于 ArcletProject, 是一个简单、灵活、高效的命令参数解析器, 并且不局限于解析命令式字符串。
Alconna 拥有复杂的解析功能与命令组件,但 一般情况下请当作奇妙简易的消息链解析器/命令解析器(雾)
pip
pip install --upgrade arclet-alconnafrom arclet.alconna import Command, command, subcommand, arg
@command(help_text="Add an user")
class Add(Command):
name: str = arg(help_text="The name of the user", trigger="-n|--name")
age: int = arg(help_text="The age of the user", trigger="--age", default=18)
@command(help_text="Delete an user")
class Delete(Command):
id: int = arg(help_text="The id of the user", trigger="--id")
class List(Command, help_text="List all users"):
pass
class User(Command, help_text="User management"):
action: Add | Delete | List = subcommand()
class Example(Command):
foo: str
user: User = subcommand()
if __name__ == '__main__':
result = Example.parse()
print(f"foo: {result.foo}")
match result.user:
case User():
match result.user.action:
case Add(name=name, age=age):
print(f"Add user: {name}, age: {age}")
case Delete(id=id):
print(f"Delete user: {id}")
case List():
print("List all users")$ python example.py abc user add -n Alice
foo: abc
Add user: Alice, age: 18QQ 交流群: 链接
Alconna 采用 MIT 许可协议

