Cmdr
Cmdr copied to clipboard
Variant type
The variant
type represents a value whose type is not known until further processing is done.
There will be no autocomplete (unless overridden as described in #50), and the user can type any string for an argument of this type.
The command implementation will receive an instance of a new Variant
class in the slot of the argument.
The Variant
class will have methods to further process the data with one of the built-in Cmdr data types.
RawValue: string
, the input text.
Parse(type: string | string[]): string | false, string
, parses a value
Match(arms: MatchArm[]): any
Performs pattern matching
Parse
local parsedType, value = variant:Parse("number")
parsedType
is "number"
if the parsing was successful, and value
is of the parsed type.
parsedType
is alternatively false
, in which case value
will be a string containing the error.
Parse
could also receive an array of type names, in which case it attempts to parse down the line until it finds one that is not invalid. For example:
local parsedType, value = variant:Parse({"number", "boolean"})
In this case, the input 1
will return "number", 1
, and the input text
will return false, "Not a valid boolean or whatever the error message is"
Match
Match
accepts an array of matches. Each potential match is an array with two values. The first value is a Lua string pattern. The second is a function, which will receive all captures from the pattern. String patterns which do not match are skipped. Matches are tested in order. The function should return the final value that will be the result from the Match
call.
local value = variant:Match({
{"(.+)%((.+)%)", function(outer, inner)
SomeCustomType.new(outer, inner)
end},
{"%d+", function (text)
return text, "number"
end}
})
Optionally return a type name as the second return value to have Cmdr run the first return value through that type's parser before continuing. If this parsing fails, then that pattern match is considered failed and Match will continue to move down the array.
Match
returns nil if no matches are found.
There will be no autocomplete
No custom autocomplete?
@LoganDark That is covered in #50