Module getargs
Utility for parsing arguments in various ways.
argtypes |
A map of defined argument types. |
-
argtypes.NAME(n, args)
-
Assign or read to set or get a defined argument type.
Replace "NAME" with the name of an argument type, such as number or entity.
The signature and definition here defines the API for functions assigned into the argtypes table.
Parameters:
- n
number
The current argument index to parse (
args[n]
).
- args
table
The input arguments being parsed.
Returns:
-
The number of arguments to consume from the input, or nil if there was an error.
-
The number of arguments to add to the output, a table containing the arguments to add to the output, or a string error message if there was an error.
-
... The arguments to add to the output, if they were not returned in a table.
-
getargs(fname, ...)
-
Get an argument parsing function for a set of argument types.
Parameters:
- fname
string
The name of the function to parse arguments for.
- ...
The names of the argument types to parse.
Returns:
function(...)
A function to parse the arguments.
-
withargs(fname, args, f)
-
Get a function that parses its args then tail-calls f.
withargs(fname, args, f)
is equivalent to function(...) return f(getargs(fname, table.unpack(args))(...)) end
Parameters:
- fname
string
The name of the function to parse arguments for.
- args
table
The list of argument types to parse.
- f
function(...)
The function to call with the parsed arguments.
Returns:
function(...)
A function that parses its arguments then calls f with the parsed arguments.
-
argtypes
-
A map of defined argument types. Assign a
function(n, args)
to argtypes.NAME to define a new type.
For details of the required API, see argtypes.NAME.
-
any
-
Any value.
-
self
-
Any non-nil value. If this argument type is the first entry in an argument list, it is counted as argument zero and hidden from standard display.
-
number
-
Any number value.
-
string
-
Any string value.
-
boolean
-
Any boolean value.
-
table
-
Any table value.
-
userdata
-
Any userdata value.
-
entity
-
Any non-destroyed entity. On non-ECS, this is a SpaceObject; on ECS, an Entity.