Skip to content

Commit

Permalink
Adds validation for command argument count
Browse files Browse the repository at this point in the history
  • Loading branch information
dalpil committed Dec 6, 2010
1 parent 01fe8d2 commit 4368cd2
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions ircbot.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import inspect

from twisted.words.protocols import irc
from twisted.internet import reactor, protocol

Expand Down Expand Up @@ -32,6 +34,29 @@ def privmsg(self, user, channel, msg):
if not method:
return

# Validate arguments
argspec = inspect.getargspec(method)

given_argument_count = len(args)
required_argument_count = len(argspec.args) - 1

if argspec.defaults:
required_argument_count -= len(argspec.defaults)

if given_argument_count < required_argument_count:
msg = '{0}{1} requires {2} argument{3} ({4}), but {5} was given.'.format(
self.factory.prefix,
cmd,
required_argument_count,
's' if required_argument_count != 1 else '',
', '.join(argspec.args[1:]),
given_argument_count
)

self.msg(channel, msg)

return

result = method(*args)

if result:
Expand Down

0 comments on commit 4368cd2

Please sign in to comment.