mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-11-27 18:45:05 +08:00
✨ add argparse help message
This commit is contained in:
parent
534b51bc73
commit
b861149e0b
@ -32,7 +32,7 @@ class HandlerMeta(type):
|
||||
f"matcher: {self.matcher_type})>")
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.__repr__()
|
||||
return repr(self)
|
||||
|
||||
|
||||
class Handler(metaclass=HandlerMeta):
|
||||
@ -57,25 +57,20 @@ class Handler(metaclass=HandlerMeta):
|
||||
f"matcher: {self.matcher_type})>")
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.__repr__()
|
||||
return repr(self)
|
||||
|
||||
async def __call__(self, matcher: "Matcher", bot: "Bot", event: "Event",
|
||||
state: T_State):
|
||||
params = {
|
||||
param.name: param.annotation
|
||||
for param in self.signature.parameters.values()
|
||||
}
|
||||
|
||||
BotType = ((params["bot"] is not inspect.Parameter.empty) and
|
||||
inspect.isclass(params["bot"]) and params["bot"])
|
||||
BotType = ((self.bot_type is not inspect.Parameter.empty) and
|
||||
inspect.isclass(self.bot_type) and self.bot_type)
|
||||
if BotType and not isinstance(bot, BotType):
|
||||
logger.debug(
|
||||
f"Matcher {matcher} bot type {type(bot)} not match annotation {BotType}, ignored"
|
||||
)
|
||||
return
|
||||
|
||||
EventType = ((params["event"] is not inspect.Parameter.empty) and
|
||||
inspect.isclass(params["event"]) and params["event"])
|
||||
EventType = ((self.event_type is not inspect.Parameter.empty) and
|
||||
inspect.isclass(self.event_type) and self.event_type)
|
||||
if EventType and not isinstance(event, EventType):
|
||||
logger.debug(
|
||||
f"Matcher {matcher} event type {type(event)} not match annotation {EventType}, ignored"
|
||||
@ -84,7 +79,11 @@ class Handler(metaclass=HandlerMeta):
|
||||
|
||||
args = {"bot": bot, "event": event, "state": state, "matcher": matcher}
|
||||
await self.func(
|
||||
**{k: v for k, v in args.items() if params[k] is not None})
|
||||
**{
|
||||
k: v
|
||||
for k, v in args.items()
|
||||
if self.signature.parameters.get(k, None) is not None
|
||||
})
|
||||
|
||||
@property
|
||||
def bot_type(self) -> Union[Type["Bot"], inspect.Parameter.empty]:
|
||||
|
@ -51,7 +51,7 @@ class MatcherMeta(type):
|
||||
f"temp={self.temp}>")
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.__repr__()
|
||||
return repr(self)
|
||||
|
||||
|
||||
class Matcher(metaclass=MatcherMeta):
|
||||
@ -140,7 +140,7 @@ class Matcher(metaclass=MatcherMeta):
|
||||
f"priority={self.priority}, temp={self.temp}>")
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.__repr__()
|
||||
return repr(self)
|
||||
|
||||
@classmethod
|
||||
def new(cls,
|
||||
|
@ -287,7 +287,11 @@ class ArgumentParser(ArgParser):
|
||||
"""
|
||||
|
||||
def _print_message(self, message, file=None):
|
||||
pass
|
||||
old_message: str = getattr(self, "message", "")
|
||||
if old_message:
|
||||
old_message += "\n"
|
||||
old_message += message
|
||||
setattr(self, "message", old_message)
|
||||
|
||||
def exit(self, status=0, message=None):
|
||||
raise ParserExit(status=status, message=message)
|
||||
@ -365,7 +369,7 @@ def shell_command(*cmds: Union[str, Tuple[str, ...]],
|
||||
args = parser.parse_args(state["argv"])
|
||||
state["args"] = args
|
||||
except ParserExit as e:
|
||||
state["args"] = e
|
||||
state["args"] = getattr(parser, "message", None) or e
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
@ -24,7 +24,7 @@ async def test_b(bot: Bot, event: Event, state: T_State):
|
||||
print("======== B Running Completed ========")
|
||||
|
||||
|
||||
c = on_message(priority=0, permission=USER(1111111111))
|
||||
c = on_message(priority=0, permission=USER("1111111111"))
|
||||
|
||||
|
||||
@c.handle()
|
||||
|
Loading…
Reference in New Issue
Block a user