139
139
---
140
140
--- ### Parameters
141
141
--- 1. **key**: the argument's "name" that will be displayed to the user
142
- --- 1 . **desc**: a description of the argument
142
+ --- 2 . **desc**: a description of the argument
143
143
---
144
144
--- ### Usage example
145
145
--- The following will parse the argument (if specified) and set its value in `args["root"]`:
162
162
---
163
163
--- ### Parameters
164
164
--- 1. **key**: the argument's "name" that will be displayed to the user
165
- --- 1 . **desc**: a description of the argument
166
- --- 1 . **default**: *optional*; specify a default value (the default is "")
167
- --- 1 . **maxcount**: *optional*; specify the maximum number of occurences allowed (default is 1)
165
+ --- 2 . **desc**: a description of the argument
166
+ --- 3 . **default**: *optional*; specify a default value (the default is "")
167
+ --- 4 . **maxcount**: *optional*; specify the maximum number of occurences allowed (default is 1)
168
168
---
169
169
--- ### Usage example
170
170
--- The following will parse the argument (if specified) and set its value in `args["root"]`:
@@ -192,13 +192,14 @@ end
192
192
--- if the 2nd notation is used then a value can be defined after an `=` (`'-key, --expanded-key=VALUE'`).
193
193
--- As a final option it is possible to only use the expanded key (eg. `'--expanded-key'`) both with and
194
194
--- without a value specified.
195
- --- 1. **desc**: a description for the argument to be shown in --help
196
- --- 1. **default**: *optional*; specify a default value (the default is "")
195
+ --- 2. **desc**: a description for the argument to be shown in --help
196
+ --- 3. **default**: *optional*; specify a default value (the default is "")
197
+ --- 4. **callback**: *optional*; specify a function to call when this option is parsed (the default is nil)
197
198
---
198
199
--- ### Usage example
199
200
--- The following option will be stored in `args["i"]` and `args["input"]` with a default value of `my_file.txt`:
200
201
--- `cli:add_option("-i, --input=FILE", "path to the input file", "my_file.txt")`
201
- function cli :add_opt (key , desc , default )
202
+ function cli :add_opt (key , desc , default , callback )
202
203
-- parameterize the key if needed, possible variations:
203
204
-- 1. -key
204
205
-- 2. -key VALUE
@@ -219,6 +220,14 @@ function cli:add_opt(key, desc, default)
219
220
),
220
221
" Default argument: expected a string, nil, or {}"
221
222
)
223
+ assert (
224
+ (
225
+ type (callback ) == " function"
226
+ or callback == nil
227
+ or (getmetatable (callback ) or {}).__call
228
+ ),
229
+ " Callback argument: expected a function or nil"
230
+ )
222
231
223
232
local k , ek , v = disect (key )
224
233
@@ -244,6 +253,7 @@ function cli:add_opt(key, desc, default)
244
253
label = key ,
245
254
flag = (default == false ),
246
255
value = default ,
256
+ callback = callback ,
247
257
}
248
258
249
259
table.insert (self .optional , entry )
256
266
---
257
267
--- ### Parameters
258
268
-- 1. **key**: the argument's key
259
- -- 1. **desc**: a description of the argument to be displayed in the help listing
260
- function cli :add_flag (key , desc )
261
- self :add_opt (key , desc , false )
269
+ -- 2. **desc**: a description of the argument to be displayed in the help listing
270
+ -- 4. **callback**: *optional*; specify a function to call when this flag is parsed (the default is nil)
271
+ function cli :add_flag (key , desc , callback )
272
+ self :add_opt (key , desc , false , callback )
262
273
end
263
274
264
275
--- Parses the arguments found in #arg and returns a table with the populated values.
268
279
--- ### Parameters
269
280
--- 1. **arguments**: set this to arg
270
281
--- 1. **noprint**: set this flag to prevent any information (error or help info) from being printed
271
- --- 1 . **dump**: set this flag to dump the parsed variables for debugging purposes, alternatively
282
+ --- 2 . **dump**: set this flag to dump the parsed variables for debugging purposes, alternatively
272
283
--- set the first option to --__DUMP__ (option with 2 trailing and leading underscores) to dump at runtime.
273
284
---
274
285
--- ### Returns
@@ -372,6 +383,17 @@ function cli:parse(arguments, noprint, dump)
372
383
else
373
384
entry .value = optval
374
385
end
386
+
387
+ -- invoke the option's parse-callback, if any
388
+ if entry .callback then
389
+ local altkey = entry .key
390
+
391
+ if optkey == entry .key then
392
+ altkey = entry .expanded_key
393
+ end
394
+
395
+ entry .callback (optkey , optval , altkey )
396
+ end
375
397
end
376
398
377
399
-- missing any required arguments, or too many?
0 commit comments