Comparison

core/moduleapi.lua @ 13205:0ccd82b965d5

core.moduleapi: Improve handling of different types in :get_option_period Pass positive numbers trough unharmed, parse strings as periods, discard anything else.
author Kim Alvefur <zash@zash.se>
date Sun, 16 Jul 2023 20:59:27 +0200
parent 13204:c9ef35fab0b1
child 13206:7435a9341bb3
comparison
equal deleted inserted replaced
13204:c9ef35fab0b1 13205:0ccd82b965d5
254 return ret; 254 return ret;
255 end 255 end
256 256
257 function api:get_option_period(name, default_value) 257 function api:get_option_period(name, default_value)
258 local value = self:get_option_scalar(name, default_value); 258 local value = self:get_option_scalar(name, default_value);
259 local num = tonumber(value); 259 if type(value) == "number" then
260 if num then
261 -- assume seconds 260 -- assume seconds
262 return num; 261 return value;
263 end 262 elseif type(value) == "string" then
264 local ret = human_io.parse_duration(value); 263 local ret = human_io.parse_duration(value);
265 if value ~= nil and ret == nil then 264 if value ~= nil and ret == nil then
266 self:log("error", "Config option '%s' not understood, expecting a period", name); 265 self:log("error", "Config option '%s' not understood, expecting a period (e.g. \"2 days\")", name);
267 end 266 end
268 return ret; 267 return ret;
268 end
269 end 269 end
270 270
271 function api:get_option_boolean(name, ...) 271 function api:get_option_boolean(name, ...)
272 local value = self:get_option_scalar(name, ...); 272 local value = self:get_option_scalar(name, ...);
273 if value == nil then 273 if value == nil then