From b94c8cd58e575a2e2b216d00ced1f02eac73e04d Mon Sep 17 00:00:00 2001 From: Zomatree Date: Fri, 29 Oct 2021 03:48:54 +0100 Subject: [PATCH] fix argument splitter --- revolt/ext/commands/errors.py | 6 ++++- revolt/ext/commands/mixin.py | 41 ++++++++++++++++++++--------------- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/revolt/ext/commands/errors.py b/revolt/ext/commands/errors.py index 25fbe4a..c85ccb1 100755 --- a/revolt/ext/commands/errors.py +++ b/revolt/ext/commands/errors.py @@ -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""" diff --git a/revolt/ext/commands/mixin.py b/revolt/ext/commands/mixin.py index 0e16409..9a82b53 100755 --- a/revolt/ext/commands/mixin.py +++ b/revolt/ext/commands/mixin.py @@ -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