Skip to content

Commit

Permalink
✨ version 1.8.34
Browse files Browse the repository at this point in the history
allow Argv for spec namespace
  • Loading branch information
RF-Tar-Railt committed Nov 21, 2024
1 parent d5d873c commit ff6b9b4
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ on:
push:
branches:
- dev
- v2dev
- v1.8
- main
pull_request:

Expand Down
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
# 更新日志

## 1.8.34

### 改进

- 允许为特定命名空间设置特定的 Argv 类型

## 1.8.33

### 改进

- 若快捷指令的 key 以 ^ 为前缀,则 发出警告,然后`^` 会被去除,并且对应的command会添加命令前缀之一
- 若快捷指令的 key 以 `^` 为前缀,则发出警告,然后 `^` 会被去除,并且对应的command会添加命令前缀之一

### 修复

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
3 changes: 2 additions & 1 deletion src/arclet/alconna/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
12 changes: 8 additions & 4 deletions src/arclet/alconna/argv.py
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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}
)
2 changes: 1 addition & 1 deletion src/arclet/alconna/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit ff6b9b4

Please sign in to comment.