webmcp
diff framework/env/parse/boolean.lua @ 0:9fdfb27f8e67
Version 1.0.0
author | jbe/bsw |
---|---|
date | Sun Oct 25 12:00:00 2009 +0100 (2009-10-25) |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/framework/env/parse/boolean.lua Sun Oct 25 12:00:00 2009 +0100 1.3 @@ -0,0 +1,29 @@ 1.4 +function parse.boolean(str, dest_type, options) 1.5 + if dest_type ~= atom.boolean then 1.6 + error("parse.boolean(...) can only return booleans, but a different destination type than atom.boolean was given.") 1.7 + end 1.8 + local options = options or {} 1.9 + local trimmed_str = string.match(str or "", "^%s*(.-)%s*$") 1.10 + if options.true_as or options.false_as or options.nil_as then 1.11 + if trimmed_str == options.true_as then 1.12 + return true 1.13 + elseif trimmed_str == options.false_as then 1.14 + return false 1.15 + elseif trimmed_str == options.nil_as or trimmed_str == "" then 1.16 + return nil 1.17 + else 1.18 + error("Boolean value not recognized.") 1.19 + end 1.20 + else 1.21 + local char = string.upper(string.sub(trimmed_str, 1, 1)) 1.22 + if char == "1" or char == "T" or char == "Y" then 1.23 + return true 1.24 + elseif char == "0" or char == "F" or char == "N" then 1.25 + return false 1.26 + elseif char == "" then 1.27 + return nil 1.28 + else 1.29 + error("Boolean value not recognized.") 1.30 + end 1.31 + end 1.32 +end