Skip to content

Commit 87bc315

Browse files
author
Pietro Albini
committed
Fix GH-42 -- Remove empty items from the commands’ arguments
Before this commit, if you sent a command like this "/cmd a b c", the arguments list will be ["a", "b", "", "c"]. This commit removes every blank item from that list.
1 parent 38341eb commit 87bc315

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

botogram/hooks.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ def _call(self, bot, update):
153153
return found
154154

155155

156+
_command_args_split_re = re.compile(r' +')
157+
158+
156159
class CommandHook(Hook):
157160
"""Underlying hook for @bot.command"""
158161

@@ -178,7 +181,7 @@ def _call(self, bot, update):
178181
if match.group(1) and match.group(1) != "@"+bot.itself.username:
179182
return
180183

181-
args = text.split(" ")[1:]
184+
args = _command_args_split_re.split(text)[1:]
182185
bot._call(self.func, self.component_id, chat=message.chat,
183186
message=message, args=args)
184187
return True

docs/changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Bug fixes
5555
* Fix inability to download stickers (`issue 36`_)
5656
* Fix commands with newlines in the arguments not recognized as such (`issue
5757
41`_)
58+
* Remove empty items from the commands' arguments (`issue 42`_)
5859

5960
Deprecated features
6061
-------------------
@@ -69,6 +70,7 @@ Deprecated features will be removed in botogram 1.0!
6970
.. _issue 36: https://github.com/pietroalbini/botogram/issues/36
7071
.. _issue 40: https://github.com/pietroalbini/botogram/issues/40
7172
.. _issue 41: https://github.com/pietroalbini/botogram/issues/41
73+
.. _issue 42: https://github.com/pietroalbini/botogram/issues/42
7274

7375
.. _changelog-0.1.2:
7476

tests/test_components.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ def test_add_command(bot, sample_update):
222222
sample1_processed = False
223223
sample2_processed = False
224224
sample3_processed = False
225+
sample4_processed = False
225226

226227
def sample1(chat, message, args):
227228
nonlocal sample1_processed
@@ -232,31 +233,42 @@ def sample2(chat, message, args):
232233
sample2_processed = True
233234

234235
assert chat.id == -1
235-
assert message.text == "/sample2 a b c\nd\te"
236+
assert message.text == "/sample2 a b c\n\nd\t\t\te"
236237
assert message.chat == chat
237238
assert args == ["a", "b", "c", "d", "e"]
238239

239240
def sample3(chat, message, args):
240241
nonlocal sample3_processed
241242
sample3_processed = True
242243

243-
assert message.text == "/sample3@test_bot a b c\nd\te"
244+
assert message.text == "/sample3@test_bot a b c\n\nd\t\t\te"
245+
246+
def sample4(chat, message, args):
247+
nonlocal sample4_processed
248+
sample4_processed = True
249+
250+
assert args == []
244251

245252

246253
comp = botogram.Component("test")
247254
comp.add_command("sample1", sample1)
248255
comp.add_command("sample2", sample2)
249256
comp.add_command("sample3", sample3)
257+
comp.add_command("sample4", sample4)
250258

251259
bot.use(comp)
252260

253261
for cmd in "sample1@another_bot", "sample2", "sample3@test_bot":
254-
sample_update.message.text = "/%s a b c\nd\te" % cmd
262+
sample_update.message.text = "/%s a b c\n\nd\t\t\te" % cmd
255263
bot.process(sample_update)
256264

265+
sample_update.message.text = "/sample4"
266+
bot.process(sample_update)
267+
257268
assert sample1_processed == False
258269
assert sample2_processed == True
259270
assert sample3_processed == True
271+
assert sample4_processed == True
260272

261273

262274
def test_add_shared_memory_initializer(bot, sample_update):

0 commit comments

Comments
 (0)