Miki_Tellurium

argumenthelper

Nov 11th, 2025
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.65 KB | Gaming | 0 0
  1. -- Argument helper v1.0
  2. -- Used to parse programs arguments
  3. -- TODO add auto help generator
  4. -- TODO add arg aliases
  5. require("luautils")
  6. argumenthelper = {}
  7. local arguments = {}
  8.  
  9. --- Define a new argument. The arguments will be parsed in the order defined
  10. --- inside the types table.  
  11. ---@param name string the argument name
  12. ---@param fun function a function to execute when the argument is called
  13. ---@param argTypes table list of argument types, in order
  14. function argumenthelper.defineArgument(name, fun, argTypes)
  15.     assert(not luautils.containsKey(arguments, name:lower()), "Argument already defined: "..name)
  16.     local newArg = {}
  17.     newArg.fun = fun
  18.     assert(type(argTypes) == "table", "Invalid argument. Not a table.", 2)
  19.     newArg.argTypes = argTypes
  20.     arguments[name:lower()] = newArg
  21. end
  22.  
  23. -- Cast string argument to actual value
  24. local function castArg(arg, argType)
  25.     local function toboolean(str)
  26.         str = str:lower()
  27.         if str == "true" or str == "1" then
  28.             return true
  29.         elseif str == "false" or str == "0" then
  30.             return false
  31.         else
  32.             return nil
  33.         end
  34.     end
  35.  
  36.     if argType == "boolean" then
  37.         return toboolean(arg)
  38.     elseif argType == "number" then
  39.         return tonumber(arg)
  40.     elseif argType == "string" or argType == "any" or argType == nil then
  41.         return arg
  42.     end
  43.     error("Invalid argument type: "..argType, 2)
  44. end
  45.  
  46. --- Parse the passed argument and its values and calls the corresponding function.
  47. --- Returns true if the function actually executed.
  48. ---@param args table the program arguments
  49. ---@return boolean true if the function executed or false if no argument is passed
  50. function argumenthelper.parseArgument(args)
  51.     if #args == 0 then return false end
  52.  
  53.     local name = args[1]
  54.     local argument = arguments[name]
  55.     if not argument then
  56.         error("Invalid or missing argument: " .. name, 2)
  57.     end
  58.  
  59.     local argTypes = argument.argTypes
  60.     if (#args - 1) < #argTypes then
  61.         error(("Missing one or more values for argument \"%s\", required %d."):format(name, #argTypes), 2)
  62.     end
  63.     local values = {}
  64.     for i, argType in ipairs(argTypes) do
  65.         local argValue = args[i + 1]
  66.         local casted = castArg(argValue, argType)
  67.         if casted == nil then
  68.             error(("Value \"%s\" is not correct type. Expected %s."):format(argValue, argType), 2)
  69.         end
  70.         values[i] = casted
  71.     end
  72.  
  73.     argument.fun(table.unpack(values))
  74.     return true
  75. end
  76.  
  77. --[[ function argumenthelper.debugList()
  78.     for key, _ in pairs(arguments) do
  79.         term.write(key..",")
  80.     end
  81.     print()
  82. end ]]
Advertisement
Add Comment
Please, Sign In to add comment