diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c6dda18d..edcb9e74 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -3,7 +3,7 @@ on: push: branches: - dev - - v2dev + - v1.8 - main pull_request: diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c62d3dc..2cfbb376 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,16 @@ # 更新日志 +## 1.8.34 + +### 改进 + +- 允许为特定命名空间设置特定的 Argv 类型 + ## 1.8.33 ### 改进 -- 若快捷指令的 key 以 ^ 为前缀,则 发出警告,然后`^` 会被去除,并且对应的command会添加命令前缀之一 +- 若快捷指令的 key 以 `^` 为前缀,则发出警告,然后 `^` 会被去除,并且对应的command会添加命令前缀之一 ### 修复 diff --git a/pyproject.toml b/pyproject.toml index 51383223..b7ce1ad7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,7 +10,7 @@ authors = [ ] dependencies = [ "typing-extensions>=4.5.0", - "nepattern<1.0.0,>=0.7.6", + "nepattern<1.0.0,>=0.7.7", "tarina<0.7.0,>=0.6.1", ] dynamic = ["version"] diff --git a/src/arclet/alconna/__init__.py b/src/arclet/alconna/__init__.py index 0a6210c0..b3dae20d 100644 --- a/src/arclet/alconna/__init__.py +++ b/src/arclet/alconna/__init__.py @@ -16,6 +16,7 @@ from .argv import Argv as Argv from .argv import argv_config as argv_config from .argv import set_default_argv_type as set_default_argv_type +from .argv import set_namespace_argv_type as set_namespace_argv_type from .arparma import Arparma as Arparma from .arparma import ArparmaBehavior as ArparmaBehavior from .base import Option as Option @@ -53,7 +54,7 @@ from .typing import Up as Up from .typing import StrMulti as StrMulti -__version__ = "1.8.33" +__version__ = "1.8.34" # backward compatibility AnyOne = ANY diff --git a/src/arclet/alconna/argv.py b/src/arclet/alconna/argv.py index eba69598..73e2bf46 100644 --- a/src/arclet/alconna/argv.py +++ b/src/arclet/alconna/argv.py @@ -1,17 +1,21 @@ from __future__ import annotations -from contextvars import ContextVar from typing import Any, Callable from ._internal._argv import Argv as Argv from .typing import TDC -__argv_type__: ContextVar[type[Argv]] = ContextVar("argv_type", default=Argv) +__argv_type__: dict[str, type[Argv]] = {"_": Argv} def set_default_argv_type(argv_type: type[Argv]): """设置默认的命令行参数类型""" - __argv_type__.set(argv_type) + __argv_type__["_"] = argv_type + + +def set_namespace_argv_type(namespace: str, argv_type: type[Argv]): + """设置命名空间的命令行参数类型""" + __argv_type__[namespace] = argv_type def argv_config( @@ -32,6 +36,6 @@ def argv_config( checker (Callable[[Any], bool] | None, optional): 检查传入命令. converter (Callable[[str | list], TDC] | None, optional): 将字符串或列表转为目标命令类型. """ - Argv._cache.setdefault(target or __argv_type__.get(), {}).update( + Argv._cache.setdefault(target or __argv_type__["_"], {}).update( {k: v for k, v in locals().items() if v is not None} ) diff --git a/src/arclet/alconna/manager.py b/src/arclet/alconna/manager.py index 80f0db8b..ca9b52e0 100644 --- a/src/arclet/alconna/manager.py +++ b/src/arclet/alconna/manager.py @@ -143,7 +143,7 @@ def register(self, command: Alconna) -> None: raise ExceedMaxCount cmd_hash = command._hash self.__argv.pop(cmd_hash, None) - argv = self.__argv[cmd_hash] = __argv_type__.get()(command.meta, command.namespace_config, command.separators) # type: ignore + argv = self.__argv[cmd_hash] = __argv_type__.get(command.namespace, __argv_type__["_"])(command.meta, command.namespace_config, command.separators) # type: ignore self.__analysers.pop(cmd_hash, None) self.__analysers[cmd_hash] = command.compile(param_ids=argv.param_ids) namespace = self.__commands.setdefault(command.namespace, WeakValueDictionary())