Repository navigation
Expand file tree
/
Copy pathopt_args.lua
More file actions
58 lines (37 loc) · 1.33 KB
/
Copy pathopt_args.lua
File metadata and controls
58 lines (37 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
---@module opt_args
-- @author Christopher VanZomeren
-- @copyright (c) 2014 Christopher VanZomeren
--
-- This will be replaced with a more flexible and general solution at some point
assert(..., 'Do not use as main file; use require from different file')
local _id = select(2, ...) or ...
local error = error
local select = select
local type = type
local unpack = table.unpack or unpack
local _ENV = {}
if setfenv then setfenv(1, _ENV) end
local function get_opt_args(patterns, ...)
local last_patt_index, last_arg_index, matches = 0, 0, {}
while last_arg_index < select('#', ...) and last_patt_index < #patterns do
local next_arg_index = last_arg_index + 1
local arg = select(next_arg_index, ...)
local found_match
for next_patt_index = last_patt_index+1, #patterns do
local pattern = patterns[next_patt_index]
local type_of_patt = type(pattern)
if type_of_patt == 'string' then found_match = type(pattern) == arg
elseif type_of_patt == 'function' then found_match = pattern(arg)
else return error('expected string or function in argument pattern; received '..type_of_patt)
end
if found_match then
last_patt_index = next_patt_index
break
end
end
matches[last_patt_index] = found_match and arg
last_arg_index = next_arg_index
end
return unpack(matches)
end
return get_opt_args