From 1adc693ef5c6e181778aa813e24191da5bfcf0a5 Mon Sep 17 00:00:00 2001 From: RF-Tar-Railt Date: Thu, 21 Nov 2024 09:40:47 +0800 Subject: [PATCH] sync: version 1.8.33 warning for incorrect shortcut key --- CHANGELOG.md | 16 ++++++++++++++++ src/arclet/alconna/__init__.py | 2 +- src/arclet/alconna/manager.py | 18 +++++++++++++++--- tests/core_test.py | 11 ++++++++--- 4 files changed, 40 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 71b52633..7a00e171 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,22 @@ - Sistana (及其上游库)仅支持 Python 3.9 及以上版本。 - Sistana 对各种特性的兼容性尚未完全测试。 +## 1.8.33 + +### 改进 + +- 若快捷指令的 key 以 `^` 为前缀,则其会被替换为命令前缀之一,并发出警告 + +### 修复 + +- 修复解析快捷命令时处理换行的行为与正常行为不一致的问题 + +## 1.8.32 + +### 修复 + +- 修复 Args 读取到的字符串参数属于某个选项/子命令的名字时的错误行为 + ## 1.8.31 ### 改进 diff --git a/src/arclet/alconna/__init__.py b/src/arclet/alconna/__init__.py index dedf77a6..5822cba0 100644 --- a/src/arclet/alconna/__init__.py +++ b/src/arclet/alconna/__init__.py @@ -39,4 +39,4 @@ from .shortcut import ShortcutArgs as ShortcutArgs from .manager import command_manager as command_manager -__version__ = "1.8.31" +__version__ = "1.8.33" diff --git a/src/arclet/alconna/manager.py b/src/arclet/alconna/manager.py index dfde0df2..ed1ce40b 100644 --- a/src/arclet/alconna/manager.py +++ b/src/arclet/alconna/manager.py @@ -5,6 +5,7 @@ import contextlib import re import shelve +import warnings import weakref from copy import copy from datetime import datetime @@ -200,24 +201,35 @@ def add_shortcut(self, target: Alconna, key: str | TPattern, source: ShortcutArg _key = key.pattern _flags = key.flags humanize = source.pop("humanized", None) + command = source.pop("command", str(target.command)) + if _key.startswith("^"): + warnings.warn( + "Shortcut Key should not start with '^', otherwise it will ignore the prefix automatically.", + UserWarning, + stacklevel=3 + ) + _key = _key[1:] + if target.prefixes and (_pf := next(filter(lambda x: isinstance(x, str), target.prefixes), None)): + command = f"{_pf}{command}" + source["prefix"] = False if source.get("prefix", False) and target.prefixes: out = [] for prefix in target.prefixes: _shortcut[1][f"{re.escape(prefix)}{_key}"] = InnerShortcutArgs( - **{**source, "command": argv.converter(prefix + source.get("command", str(target.command)))}, + **{**source, "command": argv.converter(prefix + command)}, flags=_flags, ) out.append( i18n.require("shortcut.add_success").format(shortcut=f"{prefix}{_key}", target=target.path) ) _shortcut[0][humanize or _key] = InnerShortcutArgs( - **{**source, "command": argv.converter(source.get("command", str(target.command))), "prefixes": target.prefixes}, + **{**source, "command": argv.converter(command), "prefixes": target.prefixes}, flags=_flags, ) target.formatter.update_shortcut(target) return "\n".join(out) _shortcut[0][humanize or _key] = _shortcut[1][_key] = InnerShortcutArgs( - **{**source, "command": argv.converter(source.get("command", str(target.command)))}, + **{**source, "command": argv.converter(command)}, flags=_flags, ) target.formatter.update_shortcut(target) diff --git a/tests/core_test.py b/tests/core_test.py index deb65a1f..919cbbf9 100644 --- a/tests/core_test.py +++ b/tests/core_test.py @@ -442,13 +442,13 @@ def test_shortcut(): assert not alc16_1.parse("echo 123 456").matched res6 = alc16_1.parse("echo1 123 456 789") assert res6.header_match.origin == "echo1" - assert res6["content"] == "print('123 456 789')" + assert res6["content"] == "print('123 456 789')" res7 = alc16_1.parse([123]) assert not res7.matched res8 = alc16_1.parse("echo '123'") - assert res8["content"] == "print('123')" + assert res8["content"] == "print('123')" assert not alc16_1.parse("echo").matched - assert alc16_1.parse("echo1")["content"] == "print('')" + assert alc16_1.parse("echo1")["content"] == "print('')" alc16_2 = Alconna(["/", "."], "core16_2", Args.foo(bool)) alc16_2.shortcut("test", {"command": "/core16_2 True"}) @@ -556,6 +556,11 @@ def wrapper2(slot, content): assert res.query("r") == 100 assert res.query("e") == 36 + alc16_15 = Alconna(["/"], "core16_15", Args["bar", str]) + with pytest.warns(UserWarning): + alc16_15.shortcut("^test", {"args": ["abc"]}) + assert alc16_15.parse("test").bar == "abc" + def test_help(): alc17 = Alconna(