This commit is contained in:
shenjack 2023-07-11 15:33:01 +08:00
parent 55e167d0c1
commit 4e9a908a97
3 changed files with 36 additions and 19 deletions

View File

@ -7,7 +7,8 @@ class CallBackDescriptor:
instance.__dict__[self.callback_name] = value instance.__dict__[self.callback_name] = value
def __get__(self, instance, owner): def __get__(self, instance, owner):
if instance is None: return (
return self self
else: if instance is None
return instance.__dict__[self.callback_name] else instance.__dict__.get(self.callback_name)
)

View File

@ -5,6 +5,7 @@
# ------------------------------- # -------------------------------
import re import re
from dataclasses import dataclass
from typing import Callable, List, Optional, Union from typing import Callable, List, Optional, Union
from .descriptor import CallBackDescriptor from .descriptor import CallBackDescriptor
@ -56,7 +57,7 @@ def check_name(name: Union[str, List[str]]) -> None:
""" """
if isinstance(name, str) and EMPTY_WORDS.search(name): if isinstance(name, str) and EMPTY_WORDS.search(name):
raise IllegalArgumentName("The name of argument must not contains empty words.") raise IllegalArgumentName("The name of argument must not contains empty words.")
elif isinstance(name, list) and all([(not isinstance(i, str)) and EMPTY_WORDS.search(i) for i in name]): elif isinstance(name, list) and all((not isinstance(i, str)) and EMPTY_WORDS.search(i) for i in name):
raise IllegalArgumentName("The name of shortcut must be 'str', and must not contains empty words.") raise IllegalArgumentName("The name of shortcut must be 'str', and must not contains empty words.")
else: else:
raise TypeError("The type of name must be 'str' or 'list[str]'.") raise TypeError("The type of name must be 'str' or 'list[str]'.")
@ -66,14 +67,18 @@ class Parsed:
... ...
@dataclass
class Argument: class Argument:
def __init__(self, name, shortcut, optional): name: str
... shortcuts: List[str]
optional: bool
type: type
@dataclass
class Flag: class Flag:
def __init__(self, name, shortcut): name: str
... shortcuts: List[str]
class Literal: class Literal:
@ -84,14 +89,22 @@ class Literal:
def __init__(self, name: str): def __init__(self, name: str):
self.name: str = name self.name: str = name
self.sub: List[Self] = [] self.sub: List[Self] = []
self._tip: Optional[str] = None
self._doc: Optional[str] = None
self._tip: Optional[CallBack] = None
self._func: Optional[CallBack] = None self._func: Optional[CallBack] = None
self._err_callback: Optional[CallBack] = None self._err_callback: Optional[CallBack] = None
self._args: List[Argument] = []
self._flags: List[Argument]
def __call__(self, *nodes) -> Self: def __call__(self, *nodes) -> Self:
self.sub += nodes self.sub += nodes
return self return self
def __repr__(self):
attrs = (k for k in self.__dict__ if not (k.startswith("__") and k.endswith("__")))
return f"{self.__class__.__name__}({', '.join(f'{k}={v!r}' for k in attrs if (v := self.__dict__[k]))})"
def arg( def arg(
self, self,
name: str, name: str,
@ -102,39 +115,43 @@ class Literal:
check_name(name) check_name(name)
if shortcuts is not None and len(shortcuts) != 0: if shortcuts is not None and len(shortcuts) != 0:
check_name(shortcuts) check_name(shortcuts)
Argument(...) Argument(name=name, shortcuts=shortcuts, optional=optional, type=type)
...
return self return self
def arg_group(self, args: List[Argument], exclusive: bool = False):
...
def flag(self, name: str, shortcuts: Optional[List[str]] = None) -> Self: def flag(self, name: str, shortcuts: Optional[List[str]] = None) -> Self:
check_name(name) check_name(name)
if shortcuts is not None and len(shortcuts) != 0: if shortcuts is not None and len(shortcuts) != 0:
check_name(shortcuts) check_name(shortcuts)
Flag(...) # Flag(...)
... ...
return self return self
def flag_group(self, flags: List[Flag], exclusive: bool = False):
...
def error(self, callback: CallBack) -> Self: def error(self, callback: CallBack) -> Self:
check_once(self, "_err_callback")
self._err_callback = callback self._err_callback = callback
return self return self
def run(self, func: CallBack) -> Self: def run(self, func: CallBack) -> Self:
check_once(self, "_func")
self._func = func self._func = func
return self return self
def tip(self, tip: CallBack) -> Self: def tip(self, tip: CallBack) -> Self:
check_once(self, "_tip")
self._tip = tip self._tip = tip
return self return self
def parse(self) -> Parsed: def parse(self, cmd: Union[str, List[str]]) -> Parsed:
... ...
def to_doc(self) -> str: def to_doc(self) -> str:
... ...
self._doc = ...
return self._doc
def builder(node: Literal) -> Literal: def builder(node: Literal) -> Literal:
... ...

View File

@ -11,7 +11,6 @@ from typing import List, Tuple, Optional
from lib_not_dr.types import Options, Version, VersionRequirement from lib_not_dr.types import Options, Version, VersionRequirement
def _add_cmd(cmd: List[str], string: Optional[str]) -> List[str]: def _add_cmd(cmd: List[str], string: Optional[str]) -> List[str]:
if string is not None and string: if string is not None and string:
cmd.append(string) cmd.append(string)