Skip to content

Commit 584a6a0

Browse files
committed
Use shlex.split() for message.command
Enables easier and standard parsing, for quote wrapped args, etc. Filters.command now has a posix argument, and the separator argument was removed. shlex.split() works similar to having before separator=None.
1 parent 8cdcf90 commit 584a6a0

1 file changed

Lines changed: 9 additions & 8 deletions

File tree

pyrogram/client/filters/filters.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import re
2020
from typing import Callable
21+
from shlex import split
2122

2223
from .filter import Filter
2324
from ..types.bots_and_keyboards import InlineKeyboardMarkup, ReplyKeyboardMarkup
@@ -212,8 +213,8 @@ class Filters:
212213
def command(
213214
commands: str or list,
214215
prefix: str or list = "/",
215-
separator: str = " ",
216-
case_sensitive: bool = False
216+
case_sensitive: bool = False,
217+
posix: bool = True
217218
):
218219
"""Filter commands, i.e.: text messages starting with "/" or any other custom prefix.
219220
@@ -229,13 +230,13 @@ def command(
229230
Defaults to "/" (slash). Examples: ".", "!", ["/", "!", "."].
230231
Can be None or "" (empty string) to allow commands with no prefix at all.
231232
232-
separator (``str``, *optional*):
233-
The command arguments separator. Defaults to " " (white space).
234-
Examples: /start first second, /start-first-second, /start.first.second.
235-
236233
case_sensitive (``bool``, *optional*):
237234
Pass True if you want your command(s) to be case sensitive. Defaults to False.
238235
Examples: when True, command="Start" would trigger /Start but not /start.
236+
237+
posix (``bool``, *optional*):
238+
Pass False if you don't want to use shlex posix mode, Defaults to True.
239+
It will strip quotes, and has some other behaviors, read more https://docs.python.org/3/library/shlex.html#parsing-rules
239240
"""
240241

241242
def func(flt, message):
@@ -245,7 +246,7 @@ def func(flt, message):
245246
if text:
246247
for p in flt.p:
247248
if text.startswith(p):
248-
s = text.split(flt.s)
249+
s = split(text, posix=flt.psx)
249250
c, a = s[0][len(p):], s[1:]
250251
c = c if flt.cs else c.lower()
251252
message.command = ([c] + a) if c in flt.c else None
@@ -257,7 +258,7 @@ def func(flt, message):
257258
commands = {c if case_sensitive else c.lower() for c in commands}
258259
prefixes = set(prefix) if prefix else {""}
259260

260-
return create(func, "CommandFilter", c=commands, p=prefixes, s=separator, cs=case_sensitive)
261+
return create(func, "CommandFilter", c=commands, p=prefixes, cs=case_sensitive, psx=posix)
261262

262263
@staticmethod
263264
def regex(pattern, flags: int = 0):

0 commit comments

Comments
 (0)