Skip to content

Commit

Permalink
🍻 improve Args' repr
Browse files Browse the repository at this point in the history
  • Loading branch information
RF-Tar-Railt committed Sep 22, 2024
1 parent 4e018fe commit 3a36d6f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
15 changes: 9 additions & 6 deletions src/arclet/alconna/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ class Field(Generic[_T]):
"""参数单元的默认值"""
alias: str | None = dc.field(default=None)
"""参数单元默认值的别名"""
completion: Callable[[], str | list[str] | None] | None = dc.field(default=None)
completion: Callable[[], str | list[str] | None] | None = dc.field(default=None, repr=False)
"""参数单元的补全"""
unmatch_tips: Callable[[Any], str] | None = dc.field(default=None)
unmatch_tips: Callable[[Any], str] | None = dc.field(default=None, repr=False)
"""参数单元的错误提示"""
missing_tips: Callable[[], str] | None = dc.field(default=None)
missing_tips: Callable[[], str] | None = dc.field(default=None, repr=False)
"""参数单元的缺失提示"""

@property
Expand Down Expand Up @@ -84,7 +84,7 @@ class Arg(Generic[_T]):
"""参数单元的字段"""
notice: str | None = dc.field(compare=False, hash=False)
"""参数单元的注释"""
flag: set[ArgFlag] = dc.field(compare=False, hash=False)
flag: set[ArgFlag] = dc.field(compare=False, hash=False, repr=False)
"""参数单元的标识"""
separators: tuple[str, ...] = dc.field(compare=False, hash=False)
"""参数单元使用的分隔符"""
Expand Down Expand Up @@ -138,7 +138,7 @@ def __init__(
if ArgFlag.ANTI in self.flag and self.value not in (ANY, AllParam):
self.value = AntiPattern(self.value) # type: ignore

def __repr__(self):
def __str__(self):
n, v = f"'{self.name}'", str(self.value)
return (n if n == v else f"{n}: {v}") + (f" = '{self.field.display}'" if self.field.display is not Empty else "")

Expand Down Expand Up @@ -408,9 +408,12 @@ def __truediv__(self, other) -> Self:
def __eq__(self, other):
return self.argument == other.argument

def __repr__(self):
def __str__(self):
return f"Args({', '.join([f'{arg}' for arg in self.argument])})" if self.argument else "Empty"

def __repr__(self):
return repr(self.argument)

@property
def empty(self) -> bool:
"""判断当前参数集合是否为空"""
Expand Down
2 changes: 1 addition & 1 deletion src/arclet/alconna/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ def shortcut(self, key: str | TPattern, args: ShortcutArgs | None = None, delete
return str(e)

def __repr__(self):
return f"{self.namespace}::{self.name}(args={self.args}, options={self.options})"
return f"{self.namespace}::{self.name}(args={self.args!r}, options={self.options})"

def add(self, opt: Option | Subcommand) -> Self:
"""添加选项或子命令
Expand Down

0 comments on commit 3a36d6f

Please sign in to comment.