Skip to content

Commit

Permalink
🔖 version 1.8.24
Browse files Browse the repository at this point in the history
  • Loading branch information
RF-Tar-Railt committed Aug 11, 2024
1 parent 058adb2 commit 919241e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# 更新日志

## 1.8.24

### 新增

- `AllParam` 现在可以指定其允许的参数类型:`AllParam(str)`,同时增加参数 `ignore` 用于决定是否忽略允许类型之外的参数被收集

### 改进

- 命令头的模糊匹配检查现在会在快捷指令检查后进行

### 修复

- 主命令/子命令在缺失主参数时的错误提示现在会先使用主参数自定义的错误提示

## 1.8.23

### 改进
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 @@ -52,7 +52,7 @@
from .typing import Up as Up
from .typing import StrMulti as StrMulti

__version__ = "1.8.23"
__version__ = "1.8.24"

# backward compatibility
AnyOne = ANY
17 changes: 7 additions & 10 deletions src/arclet/alconna/_internal/_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,10 @@ def handle_option(argv: Argv, opt: Option) -> tuple[str, OptionResult]:
error = True
name, _ = argv.next(opt.separators)
if opt.compact:
for al in opt.aliases:
if mat := re.fullmatch(f"{al}(?P<rest>.*?)", name):
argv.rollback(mat["rest"], replace=True)
error = False
break
mat = next(filter(None, (re.fullmatch(f"{al}(?P<rest>.*?)", name) for al in opt.aliases)), None)
if mat:
argv.rollback(mat["rest"], replace=True)
error = False
elif opt.action.type == 2:
for al in opt.aliases:
if name.startswith(al) and (cnt := (len(name.lstrip("-")) / len(al.lstrip("-")))).is_integer():
Expand All @@ -314,11 +313,9 @@ def handle_option(argv: Argv, opt: Option) -> tuple[str, OptionResult]:
raise FuzzyMatchSuccess(lang.require("fuzzy", "matched").format(source=al, target=name))
raise InvalidParam(lang.require("option", "name_error").format(source=opt.dest, target=name))
name = opt.dest
return (
(name, OptionResult(None, analyse_args(argv, opt.args)))
if opt.nargs
else (name, OptionResult(_cnt or opt.action.value))
)
if opt.nargs:
return name, OptionResult(None, analyse_args(argv, opt.args))
return name, OptionResult(_cnt or opt.action.value)


def handle_action(param: Option, source: OptionResult, target: OptionResult):
Expand Down
8 changes: 4 additions & 4 deletions tests/core_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -977,14 +977,14 @@ def test_tips():

core27 = Alconna(
"core27",
Args["arg1", Literal["1", "2"], Field(unmatch_tips=lambda x: f"参数arg必须是1或2哦,不能是{x}")],
Args["arg2", Literal["1", "2"], Field(missing_tips=lambda: "缺少了arg参数哦")],
Args["arg1", Literal["1", "2"], Field(unmatch_tips=lambda x: f"参数arg必须是1或2哦,不能是{x}", missing_tips=lambda: "缺少了arg1参数哦")],
Args["arg2", Literal["1", "2"], Field(missing_tips=lambda: "缺少了arg2参数哦")],
)
assert core27.parse("core27 1 1").matched
assert str(core27.parse("core27 3 1").error_info) == "参数arg必须是1或2哦,不能是3"
assert str(core27.parse("core27 1").error_info) == "缺少了arg参数哦"
assert str(core27.parse("core27 1").error_info) == "缺少了arg2参数哦"
assert str(core27.parse("core27 1 3").error_info) in ("参数 '3' 不正确, 其应该符合 \"'1'|'2'\"", "参数 '3' 不正确, 其应该符合 \"'2'|'1'\"")
assert str(core27.parse("core27").error_info) == "参数 arg1 丢失"
assert str(core27.parse("core27").error_info) == "缺少了arg1参数哦"


def test_disable_builtin_option():
Expand Down

0 comments on commit 919241e

Please sign in to comment.