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

@ -16,7 +16,13 @@ local function findCiv(arg)
return nil return nil
end 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 if next(args) == nil or args.help then
print('force: -eventType [Megabeast, Migrants, Caravan, Diplomat, WildlifeCurious, WildlifeMischievous, WildlifeFlier, CivAttack, NightCreature] -civ [player,ENTITY_ID]') print('force: -eventType [Megabeast, Migrants, Caravan, Diplomat, WildlifeCurious, WildlifeMischievous, WildlifeFlier, CivAttack, NightCreature] -civ [player,ENTITY_ID]')
return return

@ -139,7 +139,19 @@ eventful.onUnitAttack.attackTrigger = function(attacker,defender,wound)
handler(table) handler(table)
end 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 if args.clear then
itemTriggers = {} itemTriggers = {}
@ -149,14 +161,14 @@ end
if args.checkAttackEvery then if args.checkAttackEvery then
if not tonumber(args.checkAttackEvery) then if not tonumber(args.checkAttackEvery) then
error('checkEvery must be a number') error('checkAttackEvery must be a number')
end end
eventful.enableEvent(eventful.eventType.UNIT_ATTACK,tonumber(args.checkAttackEvery)) eventful.enableEvent(eventful.eventType.UNIT_ATTACK,tonumber(args.checkAttackEvery))
end end
if args.checkInventoryEvery then if args.checkInventoryEvery then
if not tonumber(args.checkInventoryEvery) then if not tonumber(args.checkInventoryEvery) then
error('checkEvery must be a number') error('checkInventoryEvery must be a number')
end end
eventful.enableEvent(eventful.eventType.INVENTORY_CHANGE,tonumber(args.checkInventoryEvery)) eventful.enableEvent(eventful.eventType.INVENTORY_CHANGE,tonumber(args.checkInventoryEvery))
end end
@ -182,8 +194,11 @@ if args.weaponType then
args.weaponType = temp args.weaponType = temp
end 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)' 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 end
if args.material then if args.material then

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

@ -141,7 +141,14 @@ eventful.onJobCompleted.reactionTrigger = function(job)
end end
eventful.enableEvent(eventful.eventType.JOB_COMPLETED,0) 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 if args.clear then
reactionHooks = {} reactionHooks = {}
end end