Skip to content

Commit b7fff91

Browse files
committed
🐛 version 1.8.3
fix shortcut regex wrapper
1 parent 9636e7d commit b7fff91

File tree

6 files changed

+22
-22
lines changed

6 files changed

+22
-22
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# 更新日志
22

3+
## Alconna 1.8.3
4+
5+
### 修复
6+
7+
- 修复 `shortcut.wrapper` 的处理逻辑
8+
39
## Alconna 1.8.2
410

511
### 修复

src/arclet/alconna/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
from .typing import UnpackVar as UnpackVar
5151
from .typing import Up as Up
5252

53-
__version__ = "1.8.2"
53+
__version__ = "1.8.3"
5454

5555
# backward compatibility
5656
AnyOne = ANY

src/arclet/alconna/_internal/_analyser.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -298,17 +298,21 @@ def shortcut(
298298
if self.command.meta.raise_exception:
299299
raise exc
300300
return self.export(argv, True, exc)
301-
argv.addon(short.args)
301+
argv.addon(short.args, merge_str=False)
302302
data = _handle_shortcut_data(argv, data)
303303
if not data and argv.raw_data and any(isinstance(i, str) and bool(re.search(r"\{%(\d+)|\*(.*?)\}", i)) for i in argv.raw_data):
304304
exc = ArgumentMissing(lang.require("analyser", "param_missing"))
305305
if self.command.meta.raise_exception:
306306
raise exc
307307
return self.export(argv, True, exc)
308308
argv.bak_data = argv.raw_data.copy()
309-
argv.addon(data)
309+
argv.addon(data, merge_str=False)
310310
if reg:
311-
argv.raw_data = _handle_shortcut_reg(argv, reg.groups(), reg.groupdict(), short.wrapper)
311+
data = _handle_shortcut_reg(argv, reg.groups(), reg.groupdict(), short.wrapper)
312+
argv.raw_data.clear()
313+
argv.ndata = 0
314+
argv.current_index = 0
315+
argv.addon(data)
312316
argv.bak_data = argv.raw_data.copy()
313317
if argv.message_cache:
314318
argv.token = argv.generate_token(argv.raw_data)
@@ -350,7 +354,6 @@ def process(self, argv: Argv[TDC]) -> Arparma[TDC]:
350354
argv.context[SHORTCUT_REST] = rest
351355
argv.context[SHORTCUT_REGEX_MATCH] = mat
352356
self.reset()
353-
argv.reset()
354357
return self.shortcut(argv, rest, short, mat)
355358

356359
except FuzzyMatchSuccess as Fuzzy:

src/arclet/alconna/_internal/_argv.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,12 @@ def build(self, data: TDC) -> Self:
142142
self.token = self.generate_token(raw_data)
143143
return self
144144

145-
def addon(self, data: Iterable[str | Any]) -> Self:
145+
def addon(self, data: Iterable[str | Any], merge_str: bool = True) -> Self:
146146
"""添加命令元素
147147
148148
Args:
149149
data (Iterable[str | Any]): 命令元素
150+
merge_str (bool, optional): 是否合并前后字符串
150151
151152
Returns:
152153
Self: 自身
@@ -158,7 +159,7 @@ def addon(self, data: Iterable[str | Any]) -> Self:
158159
d = res
159160
if isinstance(d, str) and not (d := d.strip()):
160161
continue
161-
if isinstance(d, str) and i > 0 and isinstance(self.raw_data[-1], str):
162+
if merge_str and isinstance(d, str) and i > 0 and isinstance(self.raw_data[-1], str):
162163
self.raw_data[-1] += f"{self.separators[0]}{d}"
163164
else:
164165
self.raw_data.append(d)

src/arclet/alconna/_internal/_handlers.py

+4-14
Original file line numberDiff line numberDiff line change
@@ -661,39 +661,29 @@ def _handle_shortcut_reg(argv: Argv, groups: tuple[str, ...], gdict: dict[str, s
661661
if index >= len(groups):
662662
continue
663663
slot = groups[index]
664-
if slot is None:
665-
continue
666-
data.append(wrapper(index, slot) or slot)
664+
data.append(wrapper(index, slot))
667665
continue
668666
if mat := KEY_REG_SLOT.fullmatch(unit):
669667
key = mat[1]
670668
if key not in gdict:
671669
continue
672670
slot = gdict[key]
673-
if slot is None:
674-
continue
675-
data.append(wrapper(key, slot) or slot)
671+
data.append(wrapper(key, slot))
676672
continue
677673
if mat := INDEX_REG_SLOT.findall(unit):
678674
for index in map(int, mat):
679675
if index >= len(groups):
680676
unit = unit.replace(f"{{{index}}}", "")
681677
continue
682678
slot = groups[index]
683-
if slot is None:
684-
unit = unit.replace(f"{{{index}}}", "")
685-
continue
686-
unit = unit.replace(f"{{{index}}}", str(wrapper(index, slot) or slot))
679+
unit = unit.replace(f"{{{index}}}", str(wrapper(index, slot) or ""))
687680
if mat := KEY_REG_SLOT.findall(unit):
688681
for key in mat:
689682
if key not in gdict:
690683
unit = unit.replace(f"{{{key}}}", "")
691684
continue
692685
slot = gdict[key]
693-
if slot is None:
694-
unit = unit.replace(f"{{{key}}}", "")
695-
continue
696-
unit = unit.replace(f"{{{key}}}", str(wrapper(key, slot) or slot))
686+
unit = unit.replace(f"{{{key}}}", str(wrapper(key, slot) or ""))
697687
if unit:
698688
data.append(unescape(unit))
699689
return data

src/arclet/alconna/typing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626

2727
class ShortcutRegWrapper(Protocol):
28-
def __call__(self, slot: int | str, content: str) -> Any: ...
28+
def __call__(self, slot: int | str, content: str | None) -> Any: ...
2929

3030

3131
class ShortcutArgs(TypedDict):

0 commit comments

Comments
 (0)