Skip to content
Navigation Menu
{{ message }}
forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathargparse.lua
More file actions
259 lines (245 loc) · 8.36 KB
/
Copy pathargparse.lua
File metadata and controls
259 lines (245 loc) · 8.36 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
-- Docs at https://docs.dfhack.org/en/stable/docs/Lua%20API.html#argparse
local _ENV = mkmodule('argparse')
local getopt = require('3rdparty.alt_getopt')
---@nodiscard
---@param args string[] Most commonly `{ ... }`
---@param validArgs table<string, integer> Use `utils.invert`
---@return table<string, string|string[]>
function processArgs(args, validArgs)
local result = {}
local argName
local bracketDepth = 0
for i,arg in ipairs(args) do
if argName then
if arg == '[' then
if bracketDepth > 0 then
table.insert(result[argName], arg)
end
bracketDepth = bracketDepth+1
elseif arg == ']' then
bracketDepth = bracketDepth-1
if bracketDepth > 0 then
table.insert(result[argName], arg)
else
argName = nil
end
elseif arg:startswith('\\') then
if bracketDepth == 0 then
result[argName] = string.sub(arg,2)
argName = nil
else
table.insert(result[argName], string.sub(arg,2))
end
else
if bracketDepth == 0 then
result[argName] = arg
argName = nil
else
table.insert(result[argName], arg)
end
end
elseif arg:startswith('-') then
argName = string.sub(arg, arg:startswith('--') and 3 or 2)
if validArgs and not validArgs[argName] then
qerror('error: invalid arg: ' .. i .. ': ' .. argName)
end
if result[argName] then
qerror('duplicate arg: ' .. i .. ': ' .. argName)
end
if i+1 > #args or args[i+1]:startswith('-') then
result[argName] = ''
argName = nil
else
result[argName] = {}
end
else
qerror('error parsing arg ' .. i .. ': ' .. arg)
end
end
return result
end
---@class argparse.OptionAction
---@field [1] string|nil Short option (eg. "q")
---@field [2] string|nil Long option (eg. "quiet")
---@field handler fun(optarg?: string)
---@field hasArg boolean|nil
-- See online docs for full usage info.
--
-- Quick example:
--
-- local args = {...}
-- local open_readonly, filename = false, nil -- set defaults
--
-- local positionals = argparse.processArgsGetopt(args, {
-- {'r', handler=function() open_readonly = true end},
-- {'f', 'filename', hasArg=true,
-- handler=function(optarg) filename = optarg end}
-- })
--
-- In this example, if args is {'first', '-rf', 'fname', 'second'} or,
-- equivalently, {'first', '-r', '--filename', 'myfile.txt', 'second'} (note the
-- double dash in front of the long option alias), then:
-- open_readonly == true
-- filename == 'myfile.txt'
-- positionals == {'first', 'second'}.
---@param args string[] Most commonly `{ ... }`
---@param optionActions argparse.OptionAction[]
---@return string[] positionals # Positional arguments
function processArgsGetopt(args, optionActions)
local sh_opts, long_opts = '', {}
local handlers = {}
for _,optionAction in ipairs(optionActions) do
local sh_opt,long_opt = optionAction[1], optionAction[2]
if sh_opt and (type(sh_opt) ~= 'string' or #sh_opt > 1) then
error('option letter not found')
end
if long_opt and (type(long_opt) ~= 'string' or #long_opt == 0) then
error('long option name must be a string with length >0')
end
if not sh_opt then
sh_opt = ''
end
if not long_opt and #sh_opt == 0 then
error('at least one of sh_opt and long_opt must be specified')
end
if not optionAction.handler then
error(string.format('handler missing for option "%s"',
#sh_opt > 0 and sh_opt or long_opt))
end
if #sh_opt > 0 then
sh_opts = sh_opts .. sh_opt
if optionAction.hasArg then sh_opts = sh_opts .. ':' end
handlers[sh_opt] = optionAction.handler
end
if long_opt then
if #sh_opt > 0 then
long_opts[long_opt] = sh_opt
else
long_opts[long_opt] = optionAction.hasArg and 1 or 0
end
handlers[long_opt] = optionAction.handler
end
end
local opts, optargs, nonoptions =
getopt.get_ordered_opts(args, sh_opts, long_opts)
for i,v in ipairs(opts) do
handlers[v](optargs[i])
end
return nonoptions
end
---@param arg_name? string
---@param fmt string
---@param ... any
local function arg_error(arg_name, fmt, ...)
local prefix = ''
if arg_name and #arg_name > 0 then
prefix = arg_name .. ': '
end
qerror(('%s'..fmt):format(prefix, ...))
end
---@nodiscard
---@param arg string
---@param arg_name? string
---@param list_length? integer
---@return string[]
function stringList(arg, arg_name, list_length)
if not list_length then list_length = 0 end
local list = arg and (arg):split(',') or {}
if list_length > 0 and #list ~= list_length then
arg_error(arg_name,
'expected %d elements; found %d', list_length, #list)
end
for i,element in ipairs(list) do
list[i] = element:trim()
end
return list
end
---@nodiscard
---@param arg string
---@param arg_name? string
---@param list_length? integer
---@return integer[]
function numberList(arg, arg_name, list_length)
local strings = stringList(arg, arg_name, list_length)
---@type integer[]
local numbers = {}
for i,str in ipairs(strings) do
local num = tonumber(str)
if not num then
arg_error(arg_name, 'invalid number: "%s"', str)
end
numbers[i] = num
end
return numbers
end
---@nodiscard
---@param arg string|integer
---@param arg_name? string
---@return integer
function positiveInt(arg, arg_name)
local val = tonumber(arg)
if not val or val <= 0 or val ~= math.floor(val) then
arg_error(arg_name,
'expected positive integer; got "%s"', tostring(arg))
end
return val
end
---@nodiscard
---@param arg string|integer
---@param arg_name? string
---@return integer
function nonnegativeInt(arg, arg_name)
local val = tonumber(arg)
if not val or val < 0 or val ~= math.floor(val) then
arg_error(arg_name,
'expected non-negative integer; got "%s"', tostring(arg))
end
return val
end
---@nodiscard
---@param arg string|'here'
---@param arg_name? string
---@param skip_validation? boolean
---@return df.coord
function coords(arg, arg_name, skip_validation)
if arg == 'here' then
local guidm = require('gui.dwarfmode') -- globals may not be available yet
local cursor = guidm.getCursorPos()
if not cursor then
arg_error(arg_name,
'"here" was specified for coordinates, but the game' ..
' cursor is not active!')
end
if not skip_validation and not dfhack.maps.isValidTilePos(cursor) then
arg_error(arg_name, 'cursor coordinates not on current map!')
end
return cursor
end
local numbers = numberList(arg, arg_name, 3)
local pos = xyz2pos(nonnegativeInt(numbers[1]),
nonnegativeInt(numbers[2]),
nonnegativeInt(numbers[3]))
if not skip_validation and not dfhack.maps.isValidTilePos(pos) then
arg_error(arg_name,
'specified coordinates not on current map: "%s"', arg)
end
return pos
end
local toBool={["true"]=true,["yes"]=true,["y"]=true,["on"]=true,["1"]=true,["enable"]=true,["enabled"]=true,
["false"]=false,["no"]=false,["n"]=false,["off"]=false,["0"]=false,["disable"]=false,["disabled"]=false}
---@nodiscard
---@param arg string
---@param arg_name? string
---@return boolean
function boolean(arg, arg_name)
if arg == nil then
arg_error(arg_name, 'missing value; expected "true", "yes", "false", or "no"')
end
local arg_lower = string.lower(arg)
if toBool[arg_lower] == nil then
arg_error(arg_name,
'unknown value: "%s"; expected "true", "yes", "false", or "no"', arg)
end
return toBool[arg_lower]
end
return _ENV
You can’t perform that action at this time.
