fix argument splitter

This commit is contained in:
Zomatree
2021-10-29 03:48:54 +01:00
parent c5a67335d2
commit b94c8cd58e
2 changed files with 28 additions and 19 deletions
+5 -1
View File
@@ -1,10 +1,11 @@
__all__ = (
"CommandNotFound",
"NoClosingQuote"
)
class CommandNotFound(Exception):
"""Raised when a command isnt found.
Parameters
-----------
command_name: :class:`str`
@@ -14,3 +15,6 @@ class CommandNotFound(Exception):
def __init__(self, command_name: str):
self.command_name = command_name
class NoClosingQuote(Exception):
"""Raised when there is no closing quote for a command argument"""
+23 -18
View File
@@ -1,18 +1,23 @@
from __future__ import annotations
from typing import Any, Callable, Union
import re
import revolt
from .command import Command
from .context import Context
from .errors import CommandNotFound
from .errors import CommandNotFound, NoClosingQuote
__all__ = (
"CommandsMeta",
"CommandsMixin"
)
quote_regex = re.compile(r"[\"']")
chunk_regex = re.compile(r"\S+")
class CommandsMeta(type):
_commands: list[Command]
@@ -108,26 +113,26 @@ class CommandsMixin(metaclass=CommandsMeta):
The arguments from the content
"""
args = []
i = 0
temp: list[str] = []
quoted = False
quote_char = ""
for char in content:
if char == " " and not quoted:
args.append("".join(temp))
temp = []
elif char in ["\"", "'"] and not quoted:
quoted = True
quote_char = char
elif char == quote_char and quoted:
quoted = False
args.append("".join(temp))
while i < len(content):
char = content[i]
if re.match(quote_regex, char):
try:
j = content.index(char, i + 1)
args.append(content[i + 1:j])
i = j
except ValueError:
raise NoClosingQuote("Missing closing quote.")
else:
temp.append(char)
match = chunk_regex.match(content, i)
if match:
args.append(match.group())
i = match.end()
i += 1
return args