Added ability to specify optional arguments in aliases with @\d+.

This commit is contained in:
Jeremy Fincher 2003-09-13 13:30:33 +00:00
parent 5a15783e7a
commit fb60da149b
2 changed files with 27 additions and 12 deletions

View File

@ -108,31 +108,41 @@ def findBiggestDollar(alias):
if dollars:
return dollars[-1]
else:
return None
return 0
atRe = re.compile(r'@(\d+)')
def findBiggestAt(alias):
ats = atRe.findall(alias)
ats = map(int, ats)
ats.sort()
if ats:
return ats[-1]
else:
return 0
def makeNewAlias(name, alias):
if findAliasCommand(name, alias):
raise RecursiveAlias
doChannel = '$channel' in alias
biggestDollar = findBiggestDollar(alias)
doDollars = bool(biggestDollar)
if biggestDollar is not None:
biggestDollar = int(biggestDollar)
else:
biggestDollar = 0
biggestAt = findBiggestAt(alias)
def f(self, irc, msg, args):
alias_ = alias
if doChannel:
channel = privmsgs.getChannel(msg, args)
alias_ = alias.replace('$channel', channel)
if doDollars:
args = privmsgs.getArgs(args, needed=biggestDollar)
if biggestDollar == 1:
if biggestDollar or biggestAt:
args = privmsgs.getArgs(args, needed=biggestDollar,
optional=biggestAt)
# Gotta have a tuple.
if biggestDollar + biggestAt == 1:
args = (args,)
def replace(m):
idx = int(m.group(1))
return args[idx-1]
return utils.dqrepr(args[idx-1])
alias_ = dollarRe.sub(replace, alias_)
args = args[biggestDollar:]
alias_ = atRe.sub(replace, alias_)
self.Proxy(irc.irc, msg, callbacks.tokenize(alias_))
f.__doc__ ='<an alias, %s>\n\nAlias for %r' % \
(utils.nItems(biggestDollar, 'argument'), alias)

View File

@ -51,8 +51,8 @@ class FunctionsTest(unittest.TestCase):
self.failUnless(Alias.findAliasCommand(s, 'foo |%s' % s))
def testFindBiggestDollar(self):
self.assertEqual(Alias.findBiggestDollar(''), None)
self.assertEqual(Alias.findBiggestDollar('foo'), None)
self.assertEqual(Alias.findBiggestDollar(''), 0)
self.assertEqual(Alias.findBiggestDollar('foo'), 0)
self.assertEqual(Alias.findBiggestDollar('$0'), 0)
self.assertEqual(Alias.findBiggestDollar('$1'), 1)
self.assertEqual(Alias.findBiggestDollar('$2'), 2)
@ -109,6 +109,11 @@ class AliasTestCase(PluginTestCase, PluginDocumentation):
self.assertRaises(Alias.AliasError, cb.removeAlias, 'foobar')
cb.removeAlias('foobar', evenIfFrozen=True)
self.assertNoResponse('foobar', 2)
def testOptionalArgs(self):
self.assertNotError('alias myrepr "repr @1"')
self.assertResponse('myrepr foo', '"foo"')
self.assertResponse('myrepr ""', '""')