Skip to content

Commit

Permalink
✨ 0.7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
RF-Tar-Railt committed Mar 1, 2022
1 parent 8366d63 commit 197f931
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 16 deletions.
2 changes: 1 addition & 1 deletion arclet/alconna/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)])
Expand Down
6 changes: 4 additions & 2 deletions arclet/alconna/builtin/construct.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion arclet/alconna/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 7 additions & 12 deletions arclet/alconna/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""删除命令"""
Expand Down
14 changes: 14 additions & 0 deletions arclet/alconna/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 197f931

Please sign in to comment.