Engine 0.0.6
Game engine in lua
Loading...
Searching...
No Matches
argparse.lua
Go to the documentation of this file.
1local zeebo_args = require('src/lib/common/args')
2
3local function add_next_value(self, param, opt)
4 local index = #self.param_list_value[self.current]
5 self.param_list_value[self.current][index + 1] = param
6 self.param_dict_value_alias[self.current][param] = opt.alias
7 self.param_dict_value_default[self.current][param] = opt and opt.default
8 self.param_dict_value_required[self.current][param] = (opt and opt.required) == true
9 return self
10end
12local function add_option_get(self, param, opt)
13 local index = #self.param_list_option_get[self.current]
14 self.param_list_option_get[self.current][index + 1] = param
15 self.param_dict_option_get_alias[self.current][param] = opt.alias
16 self.param_dict_option_get_default[self.current][param] = opt and opt.default
17 self.param_dict_option_get_required[self.current][param] = (opt and opt.required) == true
18 return self
19end
20
21local function add_option_has(self, param)
22 local index = #self.param_list_option_has[self.current]
23 self.param_list_option_has[self.current][index + 1] = param
24 return self
25end
26
27local function add_subcommand(self, cmd_name, cmd_collection)
28 self.param_dict_option_get_required[cmd_name] = {}
29 self.param_dict_option_get_default[cmd_name] = {}
30 self.param_dict_option_get_alias[cmd_name] = {}
31 self.param_list_option_get[cmd_name] = {}
32 self.param_list_option_has[cmd_name] = {}
33 self.param_dict_value_required[cmd_name] = {}
34 self.param_dict_value_default[cmd_name] = {}
35 self.param_dict_value_alias[cmd_name] = {}
36 self.param_list_value[cmd_name] = {}
37 self.cmd_execution[cmd_name] = cmd_collection[cmd_name]
38 self.commands[#self.commands + 1] = cmd_name
39 self.current = cmd_name
40 return self
41end
42
43local function run(self, host_args)
44 if not host_args then
45 return true
46 end
47
48 local command = host_args[1]
49 local args = {
50 [0] = host_args[0],
52 }
53
54 if not command then
55 command = self.help
56 end
57
58 if not self.cmd_execution[command] then
59 command = self.error_not_found
60 end
61
62 do
63 local index = 1
64 while index <= #self.param_list_option_has[command] do
65 local param = self.param_list_option_has[command][index]
66 args[param] = zeebo_args.has(host_args, param)
67 index = index + 1
68 end
69 end
70
71 do
72 local index = 1
73 while index <= #self.param_list_option_get[command] and command ~= self.error_usage do
74 local param = self.param_list_option_get[command][index]
75 local value = zeebo_args.get(host_args, param)
76 local alias = self.param_dict_option_get_alias[command][param]
77 local required = self.param_dict_option_get_required[command][param]
78 local default_value = self.param_dict_option_get_default[command][param]
79 if alias and (value or ''):sub(1, 1) == alias:sub(1, 1) then
80 value = self.param_dict_value_alias[command][param]:sub(2):gsub('{{'..param..'}}', value:sub(2))
81 end
82 if required and not value then
83 command = self.error_usage
84 end
85 args[param] = value or default_value
86 index = index + 1
87 end
88 end
89
90 do
91 local index = 1
92 while index <= #self.param_list_value[command] and command ~= self.error_usage do
93 local param = self.param_list_value[command][index]
94 local value = zeebo_args.param(host_args, self.param_list_option_get[command], index + 1)
95 local alias = self.param_dict_value_alias[command][param]
96 local required = self.param_dict_value_required[command][param]
97 local default_value = self.param_dict_value_default[command][param]
98 if alias and (value or ''):sub(1, 1) == alias:sub(1, 1) then
99 value = self.param_dict_value_alias[command][param]:sub(2):gsub('{{'..param..'}}', value:sub(2))
100 end
101 if required and not value then
102 command = self.error_usage
103 end
104 args[param] = value or default_value
105 index = index + 1
106 end
107 end
108
109 local usage = command == self.help and args[self.param_list_value[command][1]]
110 if usage then
111 args.command = usage
112 if self.cmd_execution[usage] then
113 command = self.error_usage
114 else
115 command = self.error_not_found
116 end
117 end
118
119 if command == self.error_usage then
120 args.params = self.param_list_value[args.command]
121 args.option_get = self.param_list_option_get[args.command]
122 args.option_has = self.param_list_option_has[args.command]
123 end
124
125 return self.cmd_execution[command](args)
126end
127
128local function from(host_args)
129 local cmd = {
130 help = 'help',
131 error_usage = nil,
132 error_not_found = nil,
133 param_dict_option_get_required = {},
134 param_dict_option_get_default = {},
135 param_dict_option_get_alias = {},
136 param_list_option_get = {},
137 param_list_option_has = {},
138 param_dict_value_required = {},
139 param_dict_value_default = {},
140 param_dict_value_alias = {},
141 param_list_value = {},
142 cmd_execution = {},
143 commands = {},
144 current = nil
145 }
146
147 cmd.add_next_value = function(param, opt)
148 return add_next_value(cmd, param, opt)
149 end
150
151 cmd.add_option_get = function(param, opt)
152 return add_option_get(cmd, param, opt)
153 end
154
155 cmd.add_option_has = function(param, opt)
156 return add_option_has(cmd, param, opt)
157 end
158
159 cmd.add_subcommand = function(cmd_name, cmd_collection)
160 return add_subcommand(cmd, cmd_name, cmd_collection)
161 end
162
163 cmd.add_help_subcommand = function(cmd_name, cmd_collection)
164 cmd.help = cmd_name
165 return add_subcommand(cmd, cmd_name, cmd_collection)
166 end
167
168 cmd.add_error_cmd_usage = function(cmd_name, cmd_collection)
169 cmd.error_usage = cmd_name
170 return add_subcommand(cmd, cmd_name, cmd_collection)
171 end
172
173 cmd.add_error_cmd_not_found = function(cmd_name, cmd_collection)
174 cmd.error_not_found = cmd_name
175 return add_subcommand(cmd, cmd_name, cmd_collection)
176 end
177
178 cmd.run = function()
179 return run(cmd, host_args)
180 end
181
182 return cmd
183end
184
185local P = {
186 from=from
187}
188
189return P
local function help()
local zeebo_args
Definition argparse.lua:1
local function run(self, host_args)
local function from(host_args)
local function add_subcommand(self, cmd_name, cmd_collection)
local function add_next_value(self, param, opt)
local function add_option_has(self, param)
local function add_option_get(self, param, opt)
local function current(game, application)
local function require(std, game, application)
local function param(self, name, value)
local command
Definition main.lua:12