Added a configuration option to show only syntax on ArgumentError.

This commit is contained in:
Jeremy Fincher 2003-10-24 11:31:09 +00:00
parent 660ba14e96
commit af402fd60a
4 changed files with 46 additions and 18 deletions

View File

@ -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

View File

@ -180,26 +180,15 @@ class Misc(callbacks.Privmsg):
This command gives a useful description of what <command> does.
<plugin> 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):
"""[<nick>]

View File

@ -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)."

View File

@ -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,
}