From 197f93133efe22765cdd3b377a4245e88598894f Mon Sep 17 00:00:00 2001 From: RF-Tar-Railt <3165388245@qq.com> Date: Tue, 1 Mar 2022 10:24:37 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=200.7.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- arclet/alconna/base.py | 2 +- arclet/alconna/builtin/construct.py | 6 ++++-- arclet/alconna/component.py | 2 +- arclet/alconna/manager.py | 19 +++++++------------ arclet/alconna/types.py | 14 ++++++++++++++ 5 files changed, 27 insertions(+), 16 deletions(-) diff --git a/arclet/alconna/base.py b/arclet/alconna/base.py index 74fb7c1b..bb2947a2 100644 --- a/arclet/alconna/base.py +++ b/arclet/alconna/base.py @@ -73,7 +73,7 @@ def from_string_list(cls, args: List[List[str]], custom_types: Dict) -> "Args": if custom_types and custom_types.get(value) and not inspect.isclass(custom_types[value]): raise InvalidParam(f"自定义参数类型传入的不是类型而是 {custom_types[value]}, 这是有意而为之的吗?") try: - value = eval(value, custom_types.copy()) + value = eval(value, custom_types) except NameError: pass _args.__getitem__([(name, value, default)]) diff --git a/arclet/alconna/builtin/construct.py b/arclet/alconna/builtin/construct.py index a2babf39..dc66fde6 100644 --- a/arclet/alconna/builtin/construct.py +++ b/arclet/alconna/builtin/construct.py @@ -324,7 +324,9 @@ def _from_string( help_string = headers if not custom_types: custom_types = Alconna.custom_types - _args = Args.from_string_list(args, custom_types) + else: + custom_types.update(Alconna.custom_types) + _args = Args.from_string_list(args, custom_types.copy()) for opt in option: if opt.startswith("--"): opt_head, opt_others = split_once(opt, sep) @@ -333,7 +335,7 @@ def _from_string( except ValueError: opt_alias = opt_head opt_args = [re.split("[:|=]", p) for p in re.findall(r"<(.+?)>", opt_others)] - _opt_args = Args.from_string_list(opt_args, custom_types) + _opt_args = Args.from_string_list(opt_args, custom_types.copy()) opt_action_value = re.findall(r"\[(.+?)]$", opt_others) if not (opt_help_string := re.findall(r"#(.+)", opt_others)): opt_help_string = [opt_head] diff --git a/arclet/alconna/component.py b/arclet/alconna/component.py index 57010fda..c0505099 100644 --- a/arclet/alconna/component.py +++ b/arclet/alconna/component.py @@ -216,7 +216,7 @@ def get_first_arg(self, option_name: str) -> Any: def has(self, name: str) -> bool: """判断 Arpamar 是否有对应的选项/子命令的解析结果""" - return any([name in self._other_args, name in self._options]) + return any([name in self._other_args, name in self._options, name in self._main_args]) def __getitem__(self, item: Union[str, Type[NonTextElement]]): return self.get(item) diff --git a/arclet/alconna/manager.py b/arclet/alconna/manager.py index 09acdb78..6ccde35a 100644 --- a/arclet/alconna/manager.py +++ b/arclet/alconna/manager.py @@ -61,19 +61,14 @@ def register(self, command: "Alconna") -> None: else: raise DuplicateCommand("命令已存在") - def require(self, command: Union["Alconna", str]) -> "Analyser": + def require(self, command: str) -> "Analyser": """获取解析器""" - if isinstance(command, str): - namespace, name = self._command_part(command) - try: - ana = self.__commands[namespace][name] - except KeyError: - raise ValueError("命令不存在") - return ana - else: - cid = command.name.replace(self.sign, "") - namespace = command.namespace - return self.__commands[namespace][cid] + namespace, name = self._command_part(command) + try: + ana = self.__commands[namespace][name] + except KeyError: + raise ValueError("命令不存在") + return ana def delete(self, command: Union["Alconna", str]) -> None: """删除命令""" diff --git a/arclet/alconna/types.py b/arclet/alconna/types.py index a4cd4606..8a9ef0a9 100644 --- a/arclet/alconna/types.py +++ b/arclet/alconna/types.py @@ -122,15 +122,29 @@ def __setstate__(self, state): AnyStr = ArgPattern(r"(.+?)", token=PatternToken.DIRECT, type_mark=str) +"""任意字符串""" + AnyDigit = ArgPattern(r"(\-?\d+)", token=PatternToken.REGEX_TRANSFORM, type_mark=int) +"""任意数字""" + AnyFloat = ArgPattern(r"(\-?\d+\.?\d*)", token=PatternToken.REGEX_TRANSFORM, type_mark=float) +"""任意浮点数""" + + Bool = ArgPattern( r"(True|False|true|false)", token=PatternToken.REGEX_TRANSFORM, type_mark=bool, transform_action=lambda x: eval(x, {"true": True, "false": False}) ) +"""布尔值""" + Email = ArgPattern(r"([\w\.+-]+)@([\w\.-]+)\.([\w\.-]+)", type_mark=tuple) +"""邮箱""" + AnyIP = ArgPattern(r"(\d+)\.(\d+)\.(\d+)\.(\d+):?(\d*)", type_mark=tuple) +"""任意IP地址""" + AnyUrl = ArgPattern(r"[\w]+://[^/\s?#]+[^\s?#]+(?:\?[^\s#]*)?(?:#[^\s]*)?", type_mark=str) +"""任意URL""" class MultiArg(ArgPattern):