From ab4a600f3fa86f3e827b3578c5b62f916378e8c3 Mon Sep 17 00:00:00 2001 From: RF-Tar-Railt <3165388245@qq.com> Date: Tue, 12 Dec 2023 14:07:17 +0800 Subject: [PATCH] :bug: version 1.7.39 fix ensure_node --- CHANGELOG.md | 6 +++++ src/arclet/alconna/__init__.py | 2 +- src/arclet/alconna/formatter.py | 14 +++++++----- tests/core_test.py | 40 +++++++++++++++++++++++++-------- 4 files changed, 47 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0feaf48e..1ca1e126 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # 更新日志 +## Alconna 1.7.39 + +### 修复 + +- 修复 `Formatter` 未能正确遍历节点的 bug + ## Alconna 1.7.38 ### 修复 diff --git a/src/arclet/alconna/__init__.py b/src/arclet/alconna/__init__.py index 78c73a4e..f42fa0c9 100644 --- a/src/arclet/alconna/__init__.py +++ b/src/arclet/alconna/__init__.py @@ -50,7 +50,7 @@ from .typing import UnpackVar as UnpackVar from .typing import Up as Up -__version__ = "1.7.38" +__version__ = "1.7.39" # backward compatibility Arpamar = Arparma diff --git a/src/arclet/alconna/formatter.py b/src/arclet/alconna/formatter.py index 1705edf3..7665c35d 100644 --- a/src/arclet/alconna/formatter.py +++ b/src/arclet/alconna/formatter.py @@ -47,14 +47,18 @@ def _u(target, source): def ensure_node(targets: list[str], options: list[Option | Subcommand]): + if not targets: + return None + pf = targets.pop(0) for opt in options: - if isinstance(opt, Option) and targets[0] in opt.aliases: + if isinstance(opt, Option) and pf in opt.aliases: return opt - if isinstance(opt, Subcommand): - if targets[0] == opt.name and not targets[1:]: + if isinstance(opt, Subcommand) and pf == opt.name: + if not targets: return opt - if sub := ensure_node(targets[1:], opt.options): - return sub + return sub if (sub := ensure_node(targets, opt.options)) else opt + return ensure_node(targets, options) + @dataclass(eq=True) diff --git a/tests/core_test.py b/tests/core_test.py index e466883d..52575bc9 100644 --- a/tests/core_test.py +++ b/tests/core_test.py @@ -50,7 +50,9 @@ def test_alconna_multi_match(): meta=CommandMeta(description="测试指令1"), ) assert len(alc1.options) == 6 - assert alc1.get_help() == """\ + assert ( + alc1.get_help() + == """\ [/│!]core1 测试指令1 @@ -66,6 +68,7 @@ def test_alconna_multi_match(): * 输入需要At的用户 -u """ + ) res1 = alc1.parse(["/core1 -u", 123, "test Test -u AAA --num 222 127.0.0.1"]) assert res1.matched is True assert res1.query("num.count") == 222 @@ -497,7 +500,7 @@ def wrapper(slot, content): if content == "help": return "--help" return content - + alc16_6 = Alconna("core16_6", Args["bar", str]) alc16_6.shortcut("test(?P.+)?", wrapper=wrapper, arguments=["{bar}"]) assert alc16_6.parse("testabc").bar == "abc" @@ -505,10 +508,7 @@ def wrapper(slot, content): with output_manager.capture("core16_6") as cap: output_manager.set_action(lambda x: x, "core16_6") alc16_6.parse("testhelp") - assert ( - cap["output"] - == "core16_6 \nUnknown" - ) + assert cap["output"] == "core16_6 \nUnknown" def test_help(): @@ -518,15 +518,28 @@ def test_help(): alc17 = Alconna( "core17", Option("foo", Args["bar", str], help_text="Foo bar"), + Option("baz", Args["qux", str], help_text="Baz qux"), Subcommand("add", Args["bar", str], help_text="Add bar"), + Subcommand("del", Args["bar", str], help_text="Del bar"), ) with output_manager.capture("core17") as cap: output_manager.set_action(lambda x: x, "core17") res = alc17.parse("core17 --help") assert isinstance(res.error_info, SpecialOptionTriggered) - assert ( - cap["output"] - == "core17 \nUnknown\n\n可用的子命令有:\n* Add bar\n add \n可用的选项有:\n* Foo bar\n foo \n" + assert cap["output"] == ( + "core17 \n" + "Unknown\n" + "\n" + "可用的子命令有:\n" + "* Add bar\n" + " add \n" + "* Del bar\n" + " del \n" + "可用的选项有:\n" + "* Foo bar\n" + " foo \n" + "* Baz qux\n" + " baz \n" ) with output_manager.capture("core17") as cap: alc17.parse("core17 --help foo") @@ -534,9 +547,18 @@ def test_help(): with output_manager.capture("core17") as cap: alc17.parse("core17 foo --help") assert cap["output"] == "foo \nFoo bar" + with output_manager.capture("core17") as cap: + alc17.parse("core17 --help baz") + assert cap["output"] == "baz \nBaz qux" + with output_manager.capture("core17") as cap: + alc17.parse("core17 baz --help") + assert cap["output"] == "baz \nBaz qux" with output_manager.capture("core17") as cap: alc17.parse("core17 add --help") assert cap["output"] == "add \nAdd bar" + with output_manager.capture("core17") as cap: + alc17.parse("core17 del --help") + assert cap["output"] == "del \nDel bar" alc17_1 = Alconna( "core17_1", Option("foo bar abc baz", Args["qux", int]),