From 5611cdd99900007c05e5a0a4d76379f4eef6420e Mon Sep 17 00:00:00 2001 From: Andriel Chaoti <3628387+AndrielChaoti@users.noreply.github.com> Date: Wed, 6 Sep 2023 16:42:05 -0600 Subject: [PATCH 1/3] add `boolean` function for argparse implements a test for boolean values into the argparse utility that checks for truthy style values and converts them to a lua boolean. --- library/lua/argparse.lua | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/library/lua/argparse.lua b/library/lua/argparse.lua index 36b80feb1..8da11cb28 100644 --- a/library/lua/argparse.lua +++ b/library/lua/argparse.lua @@ -196,4 +196,17 @@ function coords(arg, arg_name, skip_validation) return pos end +function boolean(arg, arg_name) + local toBool={["true"]=true,["yes"]=true,["y"]=true,["on"]=true,["1"]=true, + ["false"]=false,["no"]=false,["n"]=false,["off"]=false,["0"]=false} + + arg = string.lower(arg) + if toBool[arg] == nil then + arg_error(arg_name, + 'unknown value: "%s"; expected "true", "yes", "false", or "no"') + end + + return toBool[arg] +end + return _ENV From 85cd0cd01cffd37472d56e5119e76370109b221d Mon Sep 17 00:00:00 2001 From: Andriel Chaoti <3628387+AndrielChaoti@users.noreply.github.com> Date: Sat, 9 Sep 2023 12:56:10 -0600 Subject: [PATCH 2/3] improvements to argparse remove need to reparse table constantly, included original arg for error message. --- library/lua/argparse.lua | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/library/lua/argparse.lua b/library/lua/argparse.lua index 8da11cb28..0a652f428 100644 --- a/library/lua/argparse.lua +++ b/library/lua/argparse.lua @@ -196,17 +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 toBool={["true"]=true,["yes"]=true,["y"]=true,["on"]=true,["1"]=true, - ["false"]=false,["no"]=false,["n"]=false,["off"]=false,["0"]=false} - - arg = string.lower(arg) - if toBool[arg] == nil then + 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"') + 'unknown value: "%s"; expected "true", "yes", "false", or "no"', arg) end - return toBool[arg] + return toBool[arg_lower] end return _ENV From 652349c7e7615aa7c6e6e9473e61dcfbef49d785 Mon Sep 17 00:00:00 2001 From: Andriel Chaoti <3628387+AndrielChaoti@users.noreply.github.com> Date: Sat, 9 Sep 2023 14:30:20 -0600 Subject: [PATCH 3/3] add docs for argparse.boolean added entry to changelog as well. --- docs/changelog.txt | 1 + docs/dev/Lua API.rst | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/docs/changelog.txt b/docs/changelog.txt index 9b0a1d30e..b9ceb618a 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -64,6 +64,7 @@ Template for new versions: ## API ## 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 ======