mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-01-19 17:58:26 +08:00
Improve ArgumentParser class
This commit is contained in:
parent
be2e44bc96
commit
70098556e8
@ -1,5 +1,7 @@
|
||||
from argparse import *
|
||||
|
||||
from .command import CommandSession
|
||||
|
||||
|
||||
class ParserExit(RuntimeError):
|
||||
def __init__(self, status=0, message=None):
|
||||
@ -13,9 +15,30 @@ class ArgumentParser(ArgumentParser):
|
||||
standard I/O.
|
||||
"""
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.session = kwargs.pop('session', None)
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def _print_message(self, *args, **kwargs):
|
||||
# do nothing
|
||||
pass
|
||||
|
||||
def exit(self, status=0, message=None):
|
||||
raise ParserExit(status=status, message=message)
|
||||
|
||||
def parse_args(self, args=None, namespace=None):
|
||||
def finish(msg):
|
||||
if self.session and isinstance(self.session, CommandSession):
|
||||
self.session.finish(msg)
|
||||
|
||||
if not args:
|
||||
finish(self.usage)
|
||||
else:
|
||||
try:
|
||||
return super().parse_args(args=args, namespace=namespace)
|
||||
except ParserExit as e:
|
||||
if e.status == 0:
|
||||
# --help
|
||||
finish(self.usage)
|
||||
else:
|
||||
finish('参数不足或不正确,请使用 --help 参数查询使用帮助')
|
||||
|
Loading…
Reference in New Issue
Block a user