Skip to content

Commit

Permalink
🐛 version 1.7.39
Browse files Browse the repository at this point in the history
fix ensure_node
  • Loading branch information
RF-Tar-Railt committed Dec 12, 2023
1 parent cfb5d9b commit ab4a600
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 15 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# 更新日志

## Alconna 1.7.39

### 修复

- 修复 `Formatter` 未能正确遍历节点的 bug

## Alconna 1.7.38

### 修复
Expand Down
2 changes: 1 addition & 1 deletion src/arclet/alconna/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 9 additions & 5 deletions src/arclet/alconna/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
40 changes: 31 additions & 9 deletions tests/core_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <IP: ip>
测试指令1
Expand All @@ -66,6 +68,7 @@ def test_alconna_multi_match():
* 输入需要At的用户
-u <id: int>
"""
)
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
Expand Down Expand Up @@ -497,18 +500,15 @@ def wrapper(slot, content):
if content == "help":
return "--help"
return content

alc16_6 = Alconna("core16_6", Args["bar", str])
alc16_6.shortcut("test(?P<bar>.+)?", wrapper=wrapper, arguments=["{bar}"])
assert alc16_6.parse("testabc").bar == "abc"

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 <bar: str> \nUnknown"
)
assert cap["output"] == "core16_6 <bar: str> \nUnknown"


def test_help():
Expand All @@ -518,25 +518,47 @@ 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 <bar: str> \n可用的选项有:\n* Foo bar\n foo <bar: str> \n"
assert cap["output"] == (
"core17 \n"
"Unknown\n"
"\n"
"可用的子命令有:\n"
"* Add bar\n"
" add <bar: str> \n"
"* Del bar\n"
" del <bar: str> \n"
"可用的选项有:\n"
"* Foo bar\n"
" foo <bar: str> \n"
"* Baz qux\n"
" baz <qux: str> \n"
)
with output_manager.capture("core17") as cap:
alc17.parse("core17 --help foo")
assert cap["output"] == "foo <bar: str> \nFoo bar"
with output_manager.capture("core17") as cap:
alc17.parse("core17 foo --help")
assert cap["output"] == "foo <bar: str> \nFoo bar"
with output_manager.capture("core17") as cap:
alc17.parse("core17 --help baz")
assert cap["output"] == "baz <qux: str> \nBaz qux"
with output_manager.capture("core17") as cap:
alc17.parse("core17 baz --help")
assert cap["output"] == "baz <qux: str> \nBaz qux"
with output_manager.capture("core17") as cap:
alc17.parse("core17 add --help")
assert cap["output"] == "add <bar: str> \nAdd bar"
with output_manager.capture("core17") as cap:
alc17.parse("core17 del --help")
assert cap["output"] == "del <bar: str> \nDel bar"
alc17_1 = Alconna(
"core17_1",
Option("foo bar abc baz", Args["qux", int]),
Expand Down

0 comments on commit ab4a600

Please sign in to comment.