-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_argparse.lua
279 lines (231 loc) · 7.84 KB
/
simple_argparse.lua
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
--- A simple argument parsing library for ComputerCraft.
---@class argparse-argument
---@field name string The name of the argument.
---@field description string The description of the argument.
---@field default any The default value of the argument.
---@class argparse-flag : argparse-argument
---@field short string? The short name of the flag.
---@class argparse-option : argparse-argument
---@class argparse-parsed
---@field options table<string, any> The options passed to the program.
---@field flags table<string, boolean> The flags passed to the program.
---@field arguments table<string, any> The arguments passed to the program.
---@class arg_pattern
---@field pattern string The pattern to match against.
---@field func fun(flag_info: table<string, argparse-flag>, parsed: argparse-parsed, ...: string) The function to run when the pattern matches.
--[[
Setup should look like so:
parser = argparse.new_parser("My Program", "This program does something cool.")
parser.add_flag("h", "help", "Print this help message.") where f is the short flag, help is the long flag
parser.add_option("f", "file", "The file to read from.", "file.txt") where "file.txt" is the default value
parser.add_argument("name", "The name of the person to greet.", true) these should be added in order, with boolean required.
parser.add_argument("age", "The age of the person to greet.", false) example of a non-required argument
parser.add_argument("greeting", "The greeting to use.", false, "Hello") example of a non-required argument with a default value
parsed = parser.parse(args) where args is the table of arguments passed to the program.
If the user inputs invalid arguments, it is up to YOU to handle that.
Assuming the arguments passed were:
{
"-h",
"--file=file.txt",
"Bob",
"20",
"Hello"
}
The output would look like
parsed = {
options = {
file = "file.txt"
},
flags = {
help = true
},
arguments = {
name = "Bob",
age = 20,
greeting = "Hello"
}
}
]]
--- The patterns and functions used to parse arguments. These are used in order.
---@type table<number, arg_pattern>
local arg_patterns = {
{
pattern = "^%-%-(%w+)%=(.+)$",
func = function(flag_info, parsed, name, value)
parsed.options[name] = value
end
},
{
pattern = "^%-%-(%w+)$",
func = function(flag_info, parsed, name)
parsed.flags[name] = true
end
},
{
pattern = "^%-(%w+)$",
func = function(flag_info, parsed, name)
for i = 1, #name do
-- find what flags this short flag refers to
for _, flag in pairs(flag_info) do
if flag.short == name:sub(i, i) then
parsed.flags[flag.name] = true
break
end
end
end
end
},
{
pattern = "^(.+)$",
func = function(flag_info, parsed, value)
table.insert(parsed.arguments, value)
end
}
}
local expect = require "cc.expect".expect
---@class argparse
local argparse = {}
--- Create a new parser.
---@param program_name string The name of the program.
---@param program_description string The description of the program.
---@return argparse-parser
function argparse.new_parser(program_name, program_description)
expect(1, program_name, "string")
expect(2, program_description, "string")
---@class argparse-parser
local parser = {
program_name = program_name,
program_description = program_description,
flags = {}, ---@type table<string, argparse-flag>
options = {}, ---@type table<string, argparse-option>
arguments = {} ---@type table<string, argparse-argument>
}
--- Add a flag to the parser.
---@param short string? The short name of the flag.
---@param long string The long name of the flag.
---@param description string The description of the flag.
function parser.add_flag(short, long, description)
expect(1, short, "string", "nil")
expect(2, long, "string")
expect(3, description, "string")
parser.flags[long] = {
name = long,
short = short,
description = description
}
end
--- Add an option to the parser.
---@param long string The long name of the option.
---@param description string The description of the option.
---@param default any The default value of the option.
function parser.add_option(long, description, default)
expect(1, long, "string")
expect(2, description, "string")
parser.options[long] = {
name = long,
description = description,
default = default
}
end
--- Add an argument to the parser.
---@param name string The name of the argument.
---@param description string The description of the argument.
---@param required boolean Whether or not the argument is required.
---@param default any The default value of the argument.
function parser.add_argument(name, description, required, default)
expect(1, name, "string")
expect(2, description, "string")
expect(3, required, "boolean")
if required and type(default) ~= "nil" then
error("Required arguments cannot have a default value.", 2)
end
table.insert(parser.arguments, {
name = name,
description = description,
required = required,
default = default
})
end
--- Parse the arguments passed to the program.
---@param args table The arguments passed to the program.
---@return argparse-parsed
function parser.parse(args)
---@type argparse-parsed
local output = {
options = {},
flags = {},
arguments = {}
}
for _, arg in ipairs(args) do
for _, pattern in ipairs(arg_patterns) do
local matched = table.pack(arg:match(pattern.pattern))
if matched.n > 0 and matched[1] ~= nil then
pattern.func(parser.flags, output, table.unpack(matched, 1, matched.n))
break
end
end
end
return output
end
--- Generate a usage string given the current parser configuration.
---@return string
function parser.usage()
local output = parser.program_name .. " - " .. parser.program_description .. "\n\n"
output = output .. "Usage: " .. parser.program_name .. " [options]"
if next(parser.arguments) then
output = output .. " "
for _, argument in ipairs(parser.arguments) do
if argument.required then
output = output .. "<" .. argument.name .. "> "
else
output = output .. "[" .. argument.name .. "] "
end
end
end
output = output .. "\n\n"
if next(parser.arguments) then
output = output .. "Arguments:\n"
for _, argument in ipairs(parser.arguments) do
output = output .. " " .. argument.name .. ": " .. argument.description
if argument.default then
output = output .. " Default: " .. tostring(argument.default)
end
output = output .. "\n"
end
output = output .. "\n"
end
if next(parser.options) then
output = output .. "Options:\n"
for _, option in pairs(parser.options) do
output = output .. " "
if option.short then
output = output .. "-" .. option.short .. ", "
else
output = output .. " "
end
output = output .. "--" .. option.name .. ": " .. option.description
if option.default then
output = output .. " Default: " .. tostring(option.default)
end
output = output .. "\n"
end
output = output .. "\n"
end
if next(parser.flags) then
output = output .. "Flags:\n"
for _, flag in pairs(parser.flags) do
output = output .. " "
if flag.short then
output = output .. "-" .. flag.short .. ", "
else
output = output .. " "
end
output = output .. "--" .. flag.name .. ": " .. flag.description .. "\n"
end
output = output .. "\n"
end
return output
end
return parser
end
return argparse