Skip to content

Commit

Permalink
Fix parsing of click commands with extraneous spaces
Browse files Browse the repository at this point in the history
Fixes #338

(cherry picked from commit 3fac6fa6f0a782c6794f9eff20b4e6e26a12a241)
  • Loading branch information
unode committed Aug 29, 2022
1 parent 2da4b30 commit a5b254f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
3 changes: 2 additions & 1 deletion mmpy_bot/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import inspect
import logging
import re
import shlex
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING, Callable, Optional, Sequence, Union

Expand Down Expand Up @@ -154,7 +155,7 @@ def __call__(self, message: Message, *args):
assert len(args) <= 1 # There is only one group, (.*)?
if len(args) == 1:
# Turn space-separated string into list
args = args[0].strip(" ").split(" ")
args = tuple(shlex.split(args[0]))
try:
ctx = self.function.make_context(
info_name=self.plugin.__class__.__name__, args=list(args)
Expand Down
5 changes: 5 additions & 0 deletions tests/unit_tests/function_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ def wrapped(self, message, arg1, arg2, flag):
assert f(create_message(), "--arg1=yes --arg2=no") == ("yes", "no", False)
assert f(create_message(), "-f --arg2=no") == ("nothing", "no", True)

# Test that click can handle arguments with unexpected spacing
# See GitHub issue #338
assert f(create_message(), "--arg1 'yes' --arg2 no") == ("yes", "no", False)
assert f(create_message(), "-f --arg2=nop --arg1=yes") == ("yes", "nop", True)

# If an incorrect argument is passed, the error and help string should be returned.
def mocked_reply(message, response):
assert "no such option: --nonexistent-arg" in response.lower()
Expand Down

0 comments on commit a5b254f

Please sign in to comment.