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