diff --git a/docs/changelog.txt b/docs/changelog.txt index 7e1ec721f..0465609e9 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -70,6 +70,7 @@ Template for new versions: - `overlay`: overlay widgets can now declare a ``version`` attribute. changing the version of a widget will reset its settings to defaults. this is useful when changing the overlay layout and old saved positions will no longer be valid. ## Lua +- ``argparse.boolean``: convert arguments to lua boolean values. ## Removed diff --git a/docs/dev/Lua API.rst b/docs/dev/Lua API.rst index f6af309d4..a267545ca 100644 --- a/docs/dev/Lua API.rst +++ b/docs/dev/Lua API.rst @@ -3538,6 +3538,12 @@ parameters. ``tonumber(arg)``. If ``arg_name`` is specified, it is used to make error messages more useful. +* ``argparse.boolean(arg, arg_name)`` + Converts ``string.lower(arg)`` from "yes/no/on/off/true/false/etc..." to a lua + boolean. Throws if the value can't be converted, otherwise returns + ``true``/``false``. If ``arg_name`` is specified, it is used to make error + messages more useful. + dumper ====== diff --git a/library/lua/argparse.lua b/library/lua/argparse.lua index 36b80feb1..0a652f428 100644 --- a/library/lua/argparse.lua +++ b/library/lua/argparse.lua @@ -196,4 +196,16 @@ function coords(arg, arg_name, skip_validation) return pos end +local toBool={["true"]=true,["yes"]=true,["y"]=true,["on"]=true,["1"]=true, + ["false"]=false,["no"]=false,["n"]=false,["off"]=false,["0"]=false} +function boolean(arg, arg_name) + local arg_lower = string.lower(arg) + if toBool[arg_lower] == nil then + arg_error(arg_name, + 'unknown value: "%s"; expected "true", "yes", "false", or "no"', arg) + end + + return toBool[arg_lower] +end + return _ENV