From af402fd60aca75187e4bffc27294f3e49114241b Mon Sep 17 00:00:00 2001 From: Jeremy Fincher Date: Fri, 24 Oct 2003 11:31:09 +0000 Subject: [PATCH] Added a configuration option to show only syntax on ArgumentError. --- scripts/supybot-wizard | 10 ++++++++++ src/Misc.py | 19 ++++--------------- src/callbacks.py | 26 +++++++++++++++++++++++--- src/conf.py | 9 +++++++++ 4 files changed, 46 insertions(+), 18 deletions(-) diff --git a/scripts/supybot-wizard b/scripts/supybot-wizard index 6d2bee22f..f3b742a87 100755 --- a/scripts/supybot-wizard +++ b/scripts/supybot-wizard @@ -498,6 +498,16 @@ def main(): myPrint("""Here's some stuff you only get to choose if you're an advanced user :)""") + # showOnlySyntax + myPrint("""By default, when the bot receives a message with invalid + arguments, the bot returns the full help (syntax and description) of + the command. If showOnlySyntax is set to True, though, the bot will + return just the syntax of the command. Of course, the help will still + be available via the help command.""") + if yn('Would you like to show only the syntax of commands when ' + 'they\'re given invalid arguments?') == 'y': + configVariables['showOnlySyntax'] = True + # replyWhenNotCommand myPrint("""By default, when people address your bot but don't give it a valid command, it'll respond with a message saying that they didn't diff --git a/src/Misc.py b/src/Misc.py index 39ba26fa9..7f228c7d2 100755 --- a/src/Misc.py +++ b/src/Misc.py @@ -180,26 +180,15 @@ class Misc(callbacks.Privmsg): This command gives a useful description of what does. is only necessary if the command is in more than one plugin. """ - def helpFor(method): - doclines = method.__doc__.splitlines() - simplehelp = '(%s %s)' % (method.__name__, doclines.pop(0)) - if doclines: - doclines = filter(None, doclines) - doclines = map(str.strip, doclines) - help = ' '.join(doclines) - s = '%s -- %s' % (ircutils.bold(simplehelp), help) - return s - else: - return 'That command has no help. The syntax is: %s' % \ - simplehelp[1:-1] if len(args) > 1: cb = irc.getCallback(args[0]) if cb is not None: command = callbacks.canonicalName(privmsgs.getArgs(args[1:])) + command = command.lstrip(conf.prefixChars) if hasattr(cb, 'isCommand') and cb.isCommand(command): method = getattr(cb, command) if hasattr(method, '__doc__') and method.__doc__ != None: - irc.reply(msg, helpFor(method)) + irc.reply(msg, callbacks.getHelp(method)) else: irc.error(msg, 'That command has no help.') else: @@ -224,9 +213,9 @@ class Misc(callbacks.Privmsg): cb = cbs[0] method = getattr(cb, command) if hasattr(method, '__doc__') and method.__doc__ is not None: - irc.reply(msg, helpFor(method)) + irc.reply(msg, callbacks.getHelp(method)) else: - irc.error(msg, '%s has no help or syntax description.'%command) + irc.error(msg, '%s has no help.' % command) def hostmask(self, irc, msg, args): """[] diff --git a/src/callbacks.py b/src/callbacks.py index 1c7afbc10..95cfe816f 100644 --- a/src/callbacks.py +++ b/src/callbacks.py @@ -51,6 +51,7 @@ import string import inspect import textwrap import threading +from itertools import imap, ifilter from cStringIO import StringIO import conf @@ -117,6 +118,23 @@ def error(msg, s): """Makes an error reply to msg with the appropriate error payload.""" return reply(msg, 'Error: ' + s) +def getHelp(method, name=None): + if name is None: + name = method.__name__ + doclines = method.__doc__.splitlines() + s = '(%s %s)' % (name, doclines.pop(0)) + if doclines: + doclines = imap(str.strip, ifilter(None, doclines)) + help = ' '.join(doclines) + s = '%s -- %s' % (ircutils.bold(s), help) + return s + +def getSyntax(method, name=None): + if name is None: + name = method.__name__ + doclines = method.__doc__.splitlines() + return '%s %s' % (name, doclines[0]) + class RateLimiter: """This class is used to rate limit replies to certain people, in order to prevent abuse of the bot. Basically, you put messages in with the .put @@ -312,10 +330,12 @@ def formatArgumentError(method, name=None): if name is None: name = method.__name__ if hasattr(method, '__doc__') and method.__doc__: - s = '%s %s' % (method.__name__, method.__doc__.splitlines()[0]) + if conf.showOnlySyntax: + return getSyntax(method, name=name) + else: + return getHelp(method, name=name) else: - s = 'Invalid arguments for %s.' % method.__name__ - return s + return 'Invalid arguments for %s.' % method.__name__ class IrcObjectProxy: "A proxy object to allow proper nested of commands (even threaded ones)." diff --git a/src/conf.py b/src/conf.py index abab52392..beab5b672 100644 --- a/src/conf.py +++ b/src/conf.py @@ -119,6 +119,14 @@ requireRegistration = False ### enablePipeSyntax = False +### +# showOnlySyntax : Supybot normally returns the full help whenever a user +# misuses a command. If this option is set to True, the bot +# will only return the syntax of the command (the first line +# of the docstring) rather than the full help. +### +showOnlySyntax = False + ### # defaultCapabilities: Capabilities allowed to everyone by default. You almost # certainly want to have !owner and !admin in here. @@ -288,6 +296,7 @@ types = { 'prefixChars': mystr, 'detailedTracebacks': mybool, 'driverModule': mystr, + 'showOnlySyntax': mybool, }