2021-07-19 16:12:29 +00:00
|
|
|
import re
|
2021-07-18 16:17:09 +00:00
|
|
|
import shlex
|
2021-09-10 18:05:27 +00:00
|
|
|
from typing import Union
|
2021-08-07 07:56:48 +00:00
|
|
|
|
2021-07-19 16:12:29 +00:00
|
|
|
from core.docopt import docopt, DocoptExit
|
2021-10-15 17:36:22 +00:00
|
|
|
from core.elements import Command, Option, Schedule, StartUp, RegexCommand, command_prefix
|
|
|
|
|
|
|
|
|
|
|
|
command_prefix_first = command_prefix[0]
|
2021-07-19 16:12:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
class InvalidHelpDocTypeError(BaseException):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
pass
|
2021-07-18 16:17:09 +00:00
|
|
|
|
|
|
|
|
2021-07-19 05:34:29 +00:00
|
|
|
class InvalidCommandFormatError(BaseException):
|
2021-07-18 16:17:09 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class CommandParser:
|
2021-10-14 14:49:12 +00:00
|
|
|
def __init__(self, args: Union[str, list, tuple, Command, Option, Schedule, StartUp, RegexCommand]):
|
2021-07-19 16:12:29 +00:00
|
|
|
"""
|
|
|
|
Format: https://github.com/jazzband/docopt-ng#usage-pattern-format
|
|
|
|
* {} - Detail help information
|
|
|
|
"""
|
2021-09-10 18:05:27 +00:00
|
|
|
self.desc = False
|
2021-10-15 17:36:22 +00:00
|
|
|
self.bind_prefix = None
|
2021-10-14 14:49:12 +00:00
|
|
|
if isinstance(args, Command):
|
2021-10-15 17:36:22 +00:00
|
|
|
self.bind_prefix = args.bind_prefix
|
2021-09-10 18:05:27 +00:00
|
|
|
if args.help_doc is not None:
|
|
|
|
args = args.help_doc
|
2021-10-15 10:21:34 +00:00
|
|
|
elif args.desc is not None:
|
2021-10-14 14:49:12 +00:00
|
|
|
args = args.desc
|
|
|
|
self.desc = True
|
2021-10-15 17:36:22 +00:00
|
|
|
else:
|
|
|
|
args = None
|
|
|
|
elif isinstance(args, (Option, Schedule, StartUp, RegexCommand)):
|
|
|
|
self.bind_prefix = args.bind_prefix
|
2021-10-14 14:49:12 +00:00
|
|
|
if args.desc is not None:
|
2021-09-10 18:05:27 +00:00
|
|
|
args = args.desc
|
|
|
|
self.desc = True
|
2021-10-15 17:36:22 +00:00
|
|
|
else:
|
|
|
|
args = None
|
|
|
|
if args is None:
|
|
|
|
self.args = args
|
|
|
|
return
|
2021-07-19 05:34:29 +00:00
|
|
|
if isinstance(args, str):
|
2021-07-19 16:12:29 +00:00
|
|
|
args = [args]
|
2021-10-15 17:36:22 +00:00
|
|
|
self.args_raw = args
|
|
|
|
if self.desc:
|
|
|
|
self.args = args
|
|
|
|
return
|
2021-07-19 16:12:29 +00:00
|
|
|
if isinstance(args, (list, tuple)):
|
2021-10-15 17:36:22 +00:00
|
|
|
arglst_raw = []
|
2021-07-19 16:12:29 +00:00
|
|
|
arglst = []
|
|
|
|
for x in args:
|
2021-10-15 17:36:22 +00:00
|
|
|
split = x.split(' ')[0]
|
|
|
|
if self.bind_prefix is not None:
|
|
|
|
if split not in [command_prefix_first + self.bind_prefix, self.bind_prefix]:
|
|
|
|
x = f'{command_prefix_first}{self.bind_prefix} {x}'
|
|
|
|
arglst_raw.append(x)
|
2021-07-19 16:12:29 +00:00
|
|
|
match_detail_help = re.match('(.*){.*}$', x)
|
|
|
|
if match_detail_help:
|
|
|
|
x = match_detail_help.group(1)
|
|
|
|
arglst.append(x)
|
2021-10-15 17:36:22 +00:00
|
|
|
self.args_raw = arglst_raw
|
2021-08-07 03:59:58 +00:00
|
|
|
self.args = 'Usage:\n ' + '\n '.join(y for y in arglst)
|
2021-07-19 05:34:29 +00:00
|
|
|
else:
|
2021-07-19 16:12:29 +00:00
|
|
|
raise InvalidHelpDocTypeError
|
|
|
|
|
2021-07-26 12:43:51 +00:00
|
|
|
def return_formatted_help_doc(self) -> str:
|
2021-10-15 17:36:22 +00:00
|
|
|
if self.args is None:
|
|
|
|
return '(此模块没有帮助信息)'
|
2021-07-19 16:12:29 +00:00
|
|
|
args_raw = self.args_raw
|
|
|
|
if isinstance(args_raw, str):
|
|
|
|
args_raw = [args_raw]
|
2021-10-15 17:36:22 +00:00
|
|
|
if self.desc:
|
|
|
|
return '说明:\n' + '\n '.join(y for y in args_raw)
|
2021-07-19 16:12:29 +00:00
|
|
|
if isinstance(args_raw, (list, tuple)):
|
|
|
|
arglst = []
|
|
|
|
for x in args_raw:
|
2021-10-15 17:36:22 +00:00
|
|
|
if x[0] not in command_prefix:
|
|
|
|
x = command_prefix_first + x
|
2021-07-19 16:12:29 +00:00
|
|
|
match_detail_help = re.match('(.*){(.*)}$', x)
|
|
|
|
if match_detail_help:
|
2021-10-15 17:36:22 +00:00
|
|
|
x = f'{match_detail_help.group(1)}- {match_detail_help.group(2)}'
|
2021-07-19 16:12:29 +00:00
|
|
|
arglst.append(x)
|
|
|
|
args = f'用法:\n ' + '\n '.join(y for y in arglst)
|
|
|
|
else:
|
|
|
|
raise InvalidHelpDocTypeError
|
|
|
|
return args
|
|
|
|
|
2021-07-18 16:17:09 +00:00
|
|
|
def parse(self, command):
|
2021-10-15 17:36:22 +00:00
|
|
|
if self.desc or self.args is None:
|
2021-09-10 18:05:27 +00:00
|
|
|
return None
|
2021-07-26 13:28:38 +00:00
|
|
|
command = re.sub('“', '"', re.sub('”', '"', command))
|
2021-09-04 13:27:23 +00:00
|
|
|
try:
|
|
|
|
split_command = shlex.split(command)
|
|
|
|
except ValueError:
|
|
|
|
split_command = command.split(' ')
|
2021-07-19 16:12:29 +00:00
|
|
|
if len(split_command) == 1:
|
|
|
|
return None
|
|
|
|
try:
|
|
|
|
return docopt(self.args, argvs=split_command[1:], default_help=False)
|
|
|
|
except DocoptExit:
|
|
|
|
raise InvalidCommandFormatError
|