Tweaked utils.lua::processArgs to allow specification of a table of acceptable argument names.

develop
expwnent 2014-07-01 00:55:52 -04:00
parent f4c948054e
commit 2ed7960f96
5 changed files with 48 additions and 9 deletions

@ -548,7 +548,7 @@ function invert(tab)
return result
end
function processArgs(...)
function processArgs(args, validArgs)
--[[
standardized argument processing for scripts
-argName value
@ -558,7 +558,6 @@ function processArgs(...)
escape sequences
--]]
local result = {}
local args = {...}
local argName
local bracketDepth = 0
for i,arg in ipairs(args) do
@ -592,6 +591,9 @@ function processArgs(...)
end
elseif string.sub(arg,1,1) == '-' then
argName = string.sub(arg,2)
if validArgs and not validArgs[argName] then
error('error: invalid arg: ' .. i .. ': ' .. argName)
end
if i+1 > #args or string.sub(args[i+1],1,1) == '-' then
result[argName] = ''
argName = nil

@ -16,7 +16,13 @@ local function findCiv(arg)
return nil
end
local args = utils.processArgs(...)
validArgs = validArgs or utils.invert({
'eventType',
'help',
'civ'
})
local args = utils.processArgs({...}, validArgs)
if next(args) == nil or args.help then
print('force: -eventType [Megabeast, Migrants, Caravan, Diplomat, WildlifeCurious, WildlifeMischievous, WildlifeFlier, CivAttack, NightCreature] -civ [player,ENTITY_ID]')
return

@ -139,7 +139,19 @@ eventful.onUnitAttack.attackTrigger = function(attacker,defender,wound)
handler(table)
end
local args = utils.processArgs(...)
validArgs = validArgs or utils.invert({
'clear',
'checkAttackEvery',
'checkInventoryEvery',
'command',
'itemType',
'onStrike',
'onEquip',
'onUnequip',
'material',
'contaminant',
})
local args = utils.processArgs({...}, validArgs)
if args.clear then
itemTriggers = {}
@ -149,14 +161,14 @@ end
if args.checkAttackEvery then
if not tonumber(args.checkAttackEvery) then
error('checkEvery must be a number')
error('checkAttackEvery must be a number')
end
eventful.enableEvent(eventful.eventType.UNIT_ATTACK,tonumber(args.checkAttackEvery))
end
if args.checkInventoryEvery then
if not tonumber(args.checkInventoryEvery) then
error('checkEvery must be a number')
error('checkInventoryEvery must be a number')
end
eventful.enableEvent(eventful.eventType.INVENTORY_CHANGE,tonumber(args.checkInventoryEvery))
end
@ -182,8 +194,11 @@ if args.weaponType then
args.weaponType = temp
end
if (args.material and 1) + (args.weaponType and 1) + (args.contaminant and 1) > 1 then
local numConditions = (args.material and 1) + (args.weaponType and 1) + (args.contaminant and 1)
if numConditions > 1 then
error 'too many conditions defined: not (yet) supported (pester expwnent if you want it)'
elseif numConditions == 0 then
error 'specify a material, weaponType, or contaminant'
end
if args.material then

@ -14,6 +14,7 @@ eventful.enableEvent(eventful.eventType.UNLOAD,1)
eventful.onUnload.outsideOnly = function()
registeredBuildings = {}
checkEvery = 100
timeoutId = nil
end
local function destroy(building)
@ -78,9 +79,17 @@ eventful.onBuildingCreatedDestroyed.outsideOnly = function(buildingId)
checkBuildings()
end
local args = utils.processArgs(...)
validArgs = validArgs or utils.invert({
'help',
'clear',
'checkEvery',
'building',
'type'
})
local args = utils.processArgs({...}, validArgs)
if args.help then
--print help message
return
end
if args.clear then

@ -141,7 +141,14 @@ eventful.onJobCompleted.reactionTrigger = function(job)
end
eventful.enableEvent(eventful.eventType.JOB_COMPLETED,0)
local args = utils.processArgs(...)
validArgs = utils.invert({
'help',
'clear',
'reactionName',
'syndrome',
'command'
})
local args = utils.processArgs({...}, validArgs)
if args.clear then
reactionHooks = {}
end