From d170c2fd2a8ad98a6d0e7bf221b332c38d47b1bb Mon Sep 17 00:00:00 2001 From: Tarrailt <42648639+RF-Tar-Railt@users.noreply.github.com> Date: Tue, 11 Jan 2022 22:09:24 +0800 Subject: [PATCH] 0.5.1 --- arclet/alconna/util.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/arclet/alconna/util.py b/arclet/alconna/util.py index b24b48e4..18905fd2 100644 --- a/arclet/alconna/util.py +++ b/arclet/alconna/util.py @@ -1,6 +1,11 @@ """杂物堆""" +from typing import Any, Union, Type +from arclet.alconna.types import ArgPattern, _AnyParam, NonTextElement, Empty, AnyStr, AnyDigit, AnyFloat, Bool, \ + AnyUrl, AnyIP, AnyParam + + def split_once(text: str, separate: str): # 相当于另类的pop, 不会改变本来的字符串 """单次分隔字符串""" out_text = "" @@ -44,3 +49,25 @@ def split(text: str, separate: str = " ", max_split: int = -1): if text: text_list.append(text) return text_list + + +def arg_check(item: Any) -> Union[ArgPattern, _AnyParam, Type[NonTextElement], Empty]: + """对 Args 里参数类型的检查, 将一般数据类型转为 Args 使用的类型""" + _check_list = { + str: AnyStr, + int: AnyDigit, + float: AnyFloat, + bool: Bool, + Ellipsis: Empty, + "url": AnyUrl, + "ip": AnyIP, + "": AnyParam, + "...": Empty + } + if _check_list.get(item): + return _check_list.get(item) + if item is None: + return Empty + if isinstance(item, str): + return ArgPattern(item) + return item