Commit 21743ca7 authored by Maxim Nazarenko's avatar Maxim Nazarenko Committed by Sijis Aviles

fix: arg_botcmd decorator now can be used as plain method

Dynamic command creation with Command(cmd_type=arg_botcmd)
now works just like with any other cmd_type

Fixes #1081
parent f9d91a1f
...@@ -335,6 +335,9 @@ def arg_botcmd(*args, ...@@ -335,6 +335,9 @@ def arg_botcmd(*args,
The `unpack_args=False` only needs to be specified once, on the bottom `@args_botcmd` The `unpack_args=False` only needs to be specified once, on the bottom `@args_botcmd`
statement. statement.
""" """
argparse_args = args
if len(args) >= 1 and callable(args[0]):
argparse_args = args[1:]
def decorator(func): def decorator(func):
...@@ -394,14 +397,14 @@ def arg_botcmd(*args, ...@@ -394,14 +397,14 @@ def arg_botcmd(*args,
# alias it so we can update it's arguments below # alias it so we can update it's arguments below
wrapper = func wrapper = func
wrapper._err_command_parser.add_argument(*args, **kwargs) wrapper._err_command_parser.add_argument(*argparse_args, **kwargs)
wrapper.__doc__ = wrapper._err_command_parser.format_help() wrapper.__doc__ = wrapper._err_command_parser.format_help()
fmt = wrapper._err_command_parser.format_usage() fmt = wrapper._err_command_parser.format_usage()
wrapper._err_command_syntax = fmt[len('usage: ') + len(wrapper._err_command_parser.prog) + 1:-1] wrapper._err_command_syntax = fmt[len('usage: ') + len(wrapper._err_command_parser.prog) + 1:-1]
return wrapper return wrapper
return decorator return decorator(args[0]) if callable(args[0]) else decorator
def _tag_webhook(func, uri_rule, methods, form_param, raw): def _tag_webhook(func, uri_rule, methods, form_param, raw):
......
from __future__ import absolute_import from __future__ import absolute_import
from errbot import BotPlugin, botcmd, Command, botmatch from errbot import BotPlugin, botcmd, Command, botmatch, arg_botcmd
def say_foo(plugin, msg, args): def say_foo(plugin, msg, args):
...@@ -7,7 +7,7 @@ def say_foo(plugin, msg, args): ...@@ -7,7 +7,7 @@ def say_foo(plugin, msg, args):
class Dyna(BotPlugin): class Dyna(BotPlugin):
"""Just a test plugin to see if synamic plugin API works. """Just a test plugin to see if dynamic plugin API works.
""" """
@botcmd @botcmd
def add_simple(self, _, _1): def add_simple(self, _, _1):
...@@ -23,6 +23,23 @@ class Dyna(BotPlugin): ...@@ -23,6 +23,23 @@ class Dyna(BotPlugin):
self.destroy_dynamic_plugin('simple with special#') self.destroy_dynamic_plugin('simple with special#')
return 'removed' return 'removed'
@botcmd
def add_arg(self, _, _1):
cmd1_name = 'echo_to_me'
cmd1 = Command(lambda plugin, msg, args: 'string to echo is %s' % args.positional_arg,
cmd_type=arg_botcmd, cmd_args=('positional_arg',),
cmd_kwargs={'unpack_args': False, 'name': cmd1_name},
name=cmd1_name)
self.create_dynamic_plugin('arg', (cmd1,), doc='documented')
return 'added'
@botcmd
def remove_arg(self, msg, args):
self.destroy_dynamic_plugin('arg')
return 'removed'
@botcmd @botcmd
def add_re(self, _, _1): def add_re(self, _, _1):
re1 = Command(lambda plugin, msg, match: 'fffound', re1 = Command(lambda plugin, msg, match: 'fffound',
......
...@@ -12,6 +12,13 @@ def test_simple(testbot): ...@@ -12,6 +12,13 @@ def test_simple(testbot):
assert 'Command "say_foo" not found' in testbot.exec_command('!say_foo') assert 'Command "say_foo" not found' in testbot.exec_command('!say_foo')
def test_arg(testbot):
assert 'added' in testbot.exec_command('!add_arg')
assert 'string to echo is string_to_echo' in testbot.exec_command('!echo_to_me string_to_echo')
assert 'removed' in testbot.exec_command('!remove_arg')
assert 'Command "echo_to_me" / "echo_to_me string_to_echo" not found' in testbot.exec_command('!echo_to_me string_to_echo')
def test_re(testbot): def test_re(testbot):
assert 'added' in testbot.exec_command('!add_re') assert 'added' in testbot.exec_command('!add_re')
assert 'fffound' in testbot.exec_command('I said cheese') assert 'fffound' in testbot.exec_command('I said cheese')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment