Software / code / prosody
Comparison
core/moduleapi.lua @ 13203:aa6c2692a4be
core.moduleapi: Allow specifying an acceptable range for number options
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Tue, 05 Oct 2021 15:36:38 +0200 |
| parent | 13201:65fb0d7a2312 |
| child | 13204:c9ef35fab0b1 |
comparison
equal
deleted
inserted
replaced
| 13202:173038306750 | 13203:aa6c2692a4be |
|---|---|
| 230 return nil; | 230 return nil; |
| 231 end | 231 end |
| 232 return tostring(value); | 232 return tostring(value); |
| 233 end | 233 end |
| 234 | 234 |
| 235 function api:get_option_number(name, ...) | 235 function api:get_option_number(name, default_value, min, max) |
| 236 local value = self:get_option_scalar(name, ...); | 236 local value = self:get_option_scalar(name, default_value); |
| 237 local ret = tonumber(value); | 237 local ret = tonumber(value); |
| 238 if value ~= nil and ret == nil then | 238 if value ~= nil and ret == nil then |
| 239 self:log("error", "Config option '%s' not understood, expecting a number", name); | 239 self:log("error", "Config option '%s' not understood, expecting a number", name); |
| 240 end | |
| 241 if ret == default_value then | |
| 242 -- skip interval checks for default or nil | |
| 243 return ret; | |
| 244 end | |
| 245 if min and ret < min then | |
| 246 self:log("warn", "Config option '%s' out of bounds %g < %g", name, ret, min); | |
| 247 return min; | |
| 248 end | |
| 249 if max and ret > max then | |
| 250 self:log("warn", "Config option '%s' out of bounds %g > %g", name, ret, max); | |
| 251 return max; | |
| 240 end | 252 end |
| 241 return ret; | 253 return ret; |
| 242 end | 254 end |
| 243 | 255 |
| 244 function api:get_option_boolean(name, ...) | 256 function api:get_option_boolean(name, ...) |