add argparse help message

This commit is contained in:
yanyongyu 2021-03-25 18:22:56 +08:00
parent 534b51bc73
commit b861149e0b
4 changed files with 20 additions and 17 deletions

View File

@ -32,7 +32,7 @@ class HandlerMeta(type):
f"matcher: {self.matcher_type})>") f"matcher: {self.matcher_type})>")
def __str__(self) -> str: def __str__(self) -> str:
return self.__repr__() return repr(self)
class Handler(metaclass=HandlerMeta): class Handler(metaclass=HandlerMeta):
@ -57,25 +57,20 @@ class Handler(metaclass=HandlerMeta):
f"matcher: {self.matcher_type})>") f"matcher: {self.matcher_type})>")
def __str__(self) -> str: def __str__(self) -> str:
return self.__repr__() return repr(self)
async def __call__(self, matcher: "Matcher", bot: "Bot", event: "Event", async def __call__(self, matcher: "Matcher", bot: "Bot", event: "Event",
state: T_State): state: T_State):
params = { BotType = ((self.bot_type is not inspect.Parameter.empty) and
param.name: param.annotation inspect.isclass(self.bot_type) and self.bot_type)
for param in self.signature.parameters.values()
}
BotType = ((params["bot"] is not inspect.Parameter.empty) and
inspect.isclass(params["bot"]) and params["bot"])
if BotType and not isinstance(bot, BotType): if BotType and not isinstance(bot, BotType):
logger.debug( logger.debug(
f"Matcher {matcher} bot type {type(bot)} not match annotation {BotType}, ignored" f"Matcher {matcher} bot type {type(bot)} not match annotation {BotType}, ignored"
) )
return return
EventType = ((params["event"] is not inspect.Parameter.empty) and EventType = ((self.event_type is not inspect.Parameter.empty) and
inspect.isclass(params["event"]) and params["event"]) inspect.isclass(self.event_type) and self.event_type)
if EventType and not isinstance(event, EventType): if EventType and not isinstance(event, EventType):
logger.debug( logger.debug(
f"Matcher {matcher} event type {type(event)} not match annotation {EventType}, ignored" 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} args = {"bot": bot, "event": event, "state": state, "matcher": matcher}
await self.func( 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 @property
def bot_type(self) -> Union[Type["Bot"], inspect.Parameter.empty]: def bot_type(self) -> Union[Type["Bot"], inspect.Parameter.empty]:

View File

@ -51,7 +51,7 @@ class MatcherMeta(type):
f"temp={self.temp}>") f"temp={self.temp}>")
def __str__(self) -> str: def __str__(self) -> str:
return self.__repr__() return repr(self)
class Matcher(metaclass=MatcherMeta): class Matcher(metaclass=MatcherMeta):
@ -140,7 +140,7 @@ class Matcher(metaclass=MatcherMeta):
f"priority={self.priority}, temp={self.temp}>") f"priority={self.priority}, temp={self.temp}>")
def __str__(self) -> str: def __str__(self) -> str:
return self.__repr__() return repr(self)
@classmethod @classmethod
def new(cls, def new(cls,

View File

@ -287,7 +287,11 @@ class ArgumentParser(ArgParser):
""" """
def _print_message(self, message, file=None): 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): def exit(self, status=0, message=None):
raise ParserExit(status=status, message=message) raise ParserExit(status=status, message=message)
@ -365,7 +369,7 @@ def shell_command(*cmds: Union[str, Tuple[str, ...]],
args = parser.parse_args(state["argv"]) args = parser.parse_args(state["argv"])
state["args"] = args state["args"] = args
except ParserExit as e: except ParserExit as e:
state["args"] = e state["args"] = getattr(parser, "message", None) or e
return True return True
else: else:
return False return False

View File

@ -24,7 +24,7 @@ async def test_b(bot: Bot, event: Event, state: T_State):
print("======== B Running Completed ========") print("======== B Running Completed ========")
c = on_message(priority=0, permission=USER(1111111111)) c = on_message(priority=0, permission=USER("1111111111"))
@c.handle() @c.handle()