Engine 0.0.4
Game engine in lua
Loading...
Searching...
No Matches
args.lua
Go to the documentation of this file.
1--! @brief get value of a compound flag
2--! @param[in] args list of arguments
3--! @param[in] key single char flag
4--! @return string
5--! @retval value when found
6--! @retval default when not found
7--! @retval NULL when found and not set default
8local function shared_args_get(args, key, default)
9 if not args then return default end
10 local index = 1
11 local value = default
12 while index <= #args do
13 local param = args[index]
14 local nextparam = args[index + 1]
15 if param:sub(1, 2) == '--' and param:sub(3, #param) == key and nextparam and nextparam:sub(1, 2) ~= '--' then
16 value = nextparam
17 end
18 index = index + 1
19 end
20 return value
21end
22
23--! @brief verify if exist a flag
24--! @param[in] args list of arguments
25--! @param[in] key single char flag
26--! @return boolean
27--! @retval true when found
28--! @retval false when not found
29local function shared_args_has(args, key)
30 if not args then return false end
31 local index = 1
32 while index <= #args do
33 local param = args[index]
34 if param:sub(1, 2) == '--' and param:sub(3, #param) == key then
35 return true
36 end
37 index = index + 1
38 end
39 return false
40end
41
42--! @brief get a ordered param
43--! @see take care when using @ref shared_args_get
44--! @param[in] args list of arguments
45--! @param[in] ignore_keys a list of compound flags to ignore
46--! @param[in] position order of parameter started by 1
47--! @return string
48--! @retval value when found
49--! @retval default when not found
50--! @retval NULL when found and not set default
51local function shared_args_param(args, ignore_keys, position, default)
52 if not args then return default end
53 local index = 1
54 local count = 1
55 local ignore_keys2 = {}
56 local value = default
57
58 while index <= #ignore_keys do
59 ignore_keys2[ignore_keys[index]] = true
60 index = index + 1
61 end
62
63 index = 1
64 while index <= #args do
65 local arg = args[index]
66 local param = args[index - 1]
67 if arg:sub(1, 2) ~= '--' then
68 if index <= 1 or (param and not (param:sub(1, 2) == '--' and ignore_keys2[param:sub(3, #param)])) then
69 if position == count then
70 value = arg
71 end
72 count = count + 1
73 end
74 end
75 index = index + 1
76 end
77
78 return value
79end
80
81local P = {
82 has = shared_args_has,
83 get = shared_args_get,
85}
86
87return P
local function shared_args_has(args, key)
verify if exist a flag
local function shared_args_param(args, ignore_keys, position, default)
get a ordered param
local function shared_args_get(args, key, default)
get value of a compound flag
local function param(self, name, value)