Merge pull request #3743 from AndrielChaoti/andriel_argparse

Add `boolean` function for argparse
develop
Myk 2023-09-09 19:13:20 -07:00 committed by GitHub
commit 8a0a2f5630
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 0 deletions

@ -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

@ -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
======

@ -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