/usr/share/lua/5.1/cliargs.lua is in lua-cliargs 2.5-4-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 | local cli, _
-- ------- --
-- Helpers --
-- ------- --
local split = function(str, pat)
local t = {}
local fpat = "(.-)" .. pat
local last_end = 1
local s, e, cap = str:find(fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(t,cap)
end
last_end = e+1
s, e, cap = str:find(fpat, last_end)
end
if last_end <= #str then
cap = str:sub(last_end)
table.insert(t, cap)
end
return t
end
local buildline = function(words, size, overflow)
-- if overflow is set, a word longer than size, will overflow the size
-- otherwise it will be chopped in line-length pieces
local line = ""
if string.len(words[1]) > size then
-- word longer than line
if overflow then
line = words[1]
table.remove(words, 1)
else
line = words[1]:sub(1, size)
words[1] = words[1]:sub(size + 1, -1)
end
else
while words[1] and (#line + string.len(words[1]) + 1 <= size) or (line == "" and #words[1] == size) do
if line == "" then
line = words[1]
else
line = line .. " " .. words[1]
end
table.remove(words, 1)
end
end
return line, words
end
local wordwrap = function(str, size, pad, overflow)
-- if overflow is set, then words longer than a line will overflow
-- otherwise, they'll be chopped in pieces
pad = pad or 0
local line = ""
local out = ""
local padstr = string.rep(" ", pad)
local words = split(str, ' ')
while words[1] do
line, words = buildline(words, size, overflow)
if out == "" then
out = padstr .. line
else
out = out .. "\n" .. padstr .. line
end
end
return out
end
local function disect(key)
-- characters allowed are a-z, A-Z, 0-9
-- extended + values also allow; # @ _ + -
local k, ek, v
local dummy
-- if there is no comma, between short and extended, add one
_, _, dummy = key:find("^%-([%a%d]+)[%s]%-%-")
if dummy then key = key:gsub("^%-[%a%d][%s]%-%-", "-"..dummy..", --", 1) end
-- for a short key + value, replace space by "="
_, _, dummy = key:find("^%-([%a%d]+)[%s]")
if dummy then key = key:gsub("^%-([%a%d]+)[ ]", "-"..dummy.."=", 1) end
-- if there is no "=", then append one
if not key:find("=") then key = key .. "=" end
-- get value
_, _, v = key:find(".-%=(.+)")
-- get key(s), remove spaces
key = split(key, "=")[1]:gsub(" ", "")
-- get short key & extended key
_, _, k = key:find("^%-([^-][^%s,]*)")
_, _, ek = key:find("%-%-(.+)$")
if v == "" then v = nil end
return k,ek,v
end
local function callable(fn)
return type(fn) == "function" or (getmetatable(fn) or {}).__call
end
function cli_error(msg, noprint)
local msg = cli.name .. ": error: " .. msg .. '; re-run with --help for usage.'
if not noprint then print(msg) end
return nil, msg
end
-- -------- --
-- CLI Main --
-- -------- --
cli = {
name = "",
required = {},
optional = {},
optargument = {maxcount = 0},
colsz = { 0, 0 }, -- column width, help text. Set to 0 for auto detect
maxlabel = 0,
}
--- Assigns the name of the program which will be used for logging.
function cli:set_name(name)
self.name = name
end
-- Used internally to lookup an entry using either its short or expanded keys
function cli:__lookup(k, ek, t)
t = t or self.optional
local _
for _,entry in ipairs(t) do
if k and entry.key == k then return entry end
if ek and entry.expanded_key == ek then return entry end
if entry.has_no_flag then
if ek and ("no-"..entry.expanded_key) == ek then return entry end
end
end
return nil
end
--- Defines a required argument.
--- Required arguments have no special notation and are order-sensitive.
--- *Note:* the value will be stored in `args[@key]`.
--- *Aliases: `add_argument`*
---
--- ### Parameters
--- 1. **key**: the argument's "name" that will be displayed to the user
--- 2. **desc**: a description of the argument
--- 3. **callback**: *optional*; specify a function to call when this argument is parsed (the default is nil)
---
--- ### Usage example
--- The following will parse the argument (if specified) and set its value in `args["root"]`:
--- `cli:add_arg("root", "path to where root scripts can be found")`
function cli:add_arg(key, desc, callback)
assert(type(key) == "string" and type(desc) == "string", "Key and description are mandatory arguments (Strings)")
assert(callable(callback) or callback == nil, "Callback argument: expected a function or nil")
if self:__lookup(key, nil, self.required) then
error("Duplicate argument: " .. key .. ", please rename one of them.")
end
table.insert(self.required, { key = key, desc = desc, value = nil, callback = callback })
if #key > self.maxlabel then self.maxlabel = #key end
end
--- Defines an optional argument (or more than one).
--- There can be only 1 optional argument, and it has to be the last one on the argumentlist.
--- *Note:* the value will be stored in `args[@key]`. The value will be a 'string' if 'maxcount == 1',
--- or a table if 'maxcount > 1'
---
--- ### Parameters
--- 1. **key**: the argument's "name" that will be displayed to the user
--- 2. **desc**: a description of the argument
--- 3. **default**: *optional*; specify a default value (the default is nil)
--- 4. **maxcount**: *optional*; specify the maximum number of occurences allowed (default is 1)
--- 5. **callback**: *optional*; specify a function to call when this argument is parsed (the default is nil)
---
--- ### Usage example
--- The following will parse the argument (if specified) and set its value in `args["root"]`:
--- `cli:add_arg("root", "path to where root scripts can be found", "", 2)`
--- The value returned will be a table with at least 1 entry and a maximum of 2 entries
function cli:optarg(key, desc, default, maxcount, callback)
assert(type(key) == "string" and type(desc) == "string", "Key and description are mandatory arguments (Strings)")
assert(type(default) == "string" or default == nil, "Default value must either be omitted or be a string")
maxcount = maxcount or 1
maxcount = tonumber(maxcount)
assert(maxcount and maxcount>0 and maxcount<1000,"Maxcount must be a number from 1 to 999")
assert(callable(callback) or callback == nil, "Callback argument: expected a function or nil")
self.optargument = { key = key, desc = desc, default = default, maxcount = maxcount, value = nil, callback = callback }
if #key > self.maxlabel then self.maxlabel = #key end
end
-- Used internally to add an option
function cli:__add_opt(k, ek, v, label, desc, default, callback)
local flag = (v == nil) -- no value, so it's a flag
local has_no_flag = flag and (ek and ek:find('^%[no%-]') ~= nil)
local ek = has_no_flag and ek:sub(6) or ek
-- guard against duplicates
if self:__lookup(k, ek) then
error("Duplicate option: " .. (k or ek) .. ", please rename one of them.")
end
if has_no_flag and self:__lookup(nil, "no-"..ek) then
error("Duplicate option: " .. ("no-"..ek) .. ", please rename one of them.")
end
-- below description of full entry record, nils included for reference
local entry = {
key = k,
expanded_key = ek,
desc = desc,
default = default,
label = label,
flag = flag,
has_no_flag = has_no_flag,
value = default,
callback = callback,
}
table.insert(self.optional, entry)
if #label > self.maxlabel then self.maxlabel = #label end
end
--- Defines an option.
--- Optional arguments can use 3 different notations, and can accept a value.
--- *Aliases: `add_option`*
---
--- ### Parameters
--- 1. **key**: the argument identifier, can be either `-key`, or `-key, --expanded-key`:
--- if the first notation is used then a value can be defined after a space (`'-key VALUE'`),
--- if the 2nd notation is used then a value can be defined after an `=` (`'-key, --expanded-key=VALUE'`).
--- As a final option it is possible to only use the expanded key (eg. `'--expanded-key'`) both with and
--- without a value specified.
--- 2. **desc**: a description for the argument to be shown in --help
--- 3. **default**: *optional*; specify a default value (the default is nil)
--- 4. **callback**: *optional*; specify a function to call when this option is parsed (the default is nil)
---
--- ### Usage example
--- The following option will be stored in `args["i"]` and `args["input"]` with a default value of `my_file.txt`:
--- `cli:add_option("-i, --input=FILE", "path to the input file", "my_file.txt")`
function cli:add_opt(key, desc, default, callback)
-- parameterize the key if needed, possible variations:
-- 1. -key
-- 2. -key VALUE
-- 3. -key, --expanded
-- 4. -key, --expanded=VALUE
-- 5. -key --expanded
-- 6. -key --expanded=VALUE
-- 7. --expanded
-- 8. --expanded=VALUE
assert(type(key) == "string" and type(desc) == "string", "Key and description are mandatory arguments (Strings)")
assert(callable(callback) or callback == nil, "Callback argument: expected a function or nil")
assert(
(
type(default) == "string"
or default == nil
or type(default) == "boolean"
or (type(default) == "table" and next(default) == nil)
),
"Default argument: expected a string, nil, or {}"
)
local k, ek, v = disect(key)
-- set defaults
if v == nil and type(default) ~= "boolean" then default = nil end
self:__add_opt(k, ek, v, key, desc, default, callback)
end
--- Define a flag argument (on/off). This is a convenience helper for cli.add_opt().
--- See cli.add_opt() for more information.
---
--- ### Parameters
-- 1. **key**: the argument's key
-- 2. **desc**: a description of the argument to be displayed in the help listing
-- 3. **default**: *optional*; specify a default value (the default is nil)
-- 4. **callback**: *optional*; specify a function to call when this flag is parsed (the default is nil)
function cli:add_flag(key, desc, default, callback)
if type(default) == "function" then
callback = default
default = nil
end
assert(default == nil or type(default) == "boolean", "Default argument: expected a boolean, nil")
local k, ek, v = disect(key)
if v ~= nil then
error("A flag type option cannot have a value set: " .. key)
end
self:__add_opt(k, ek, nil, key, desc, default, callback)
end
--- Parses the arguments found in #arg and returns a table with the populated values.
--- (NOTE: after succesful parsing, the module will delete itself to free resources)
--- *Aliases: `parse_args`*
---
--- ### Parameters
--- 1. **arguments**: set this to arg
--- 2. **noprint**: set this flag to prevent any information (error or help info) from being printed
--- 3. **dump**: set this flag to dump the parsed variables for debugging purposes, alternatively
--- set the first option to --__DUMP__ (option with 2 trailing and leading underscores) to dump at runtime.
---
--- ### Returns
--- 1. a table containing the keys specified when the arguments were defined along with the parsed values,
--- or nil + error message (--help option is considered an error and returns nil + help message)
function cli:parse(arguments, noprint, dump)
if type(arguments) ~= "table" then
-- optional 'arguments' was not provided, so shift remaining arguments
noprint, dump, arguments = arguments, noprint, nil
end
local arguments = arguments or arg or {}
local args = {}
for k,v in pairs(arguments) do args[k] = v end -- copy args local
-- starts with --help? display the help listing and abort!
if args[1] and (args[1] == "--help" or args[1] == "-h") then
return nil, self:print_help(noprint)
end
-- starts with --__DUMP__; set dump to true to dump the parsed arguments
if dump == nil then
if args[1] and args[1] == "--__DUMP__" then
dump = true
table.remove(args, 1) -- delete it to prevent further parsing
end
end
while args[1] do
local entry = nil
local opt = args[1]
local _, optpref, optkey, optkey2, optval
_, _, optpref, optkey = opt:find("^(%-[%-]?)(.+)") -- split PREFIX & NAME+VALUE
if optkey then
_, _, optkey2, optval = optkey:find("(.-)[=](.+)") -- split value and key
if optval then
optkey = optkey2
end
end
if not optpref then
break -- no optional prefix, so options are done
end
if opt == "--" then
table.remove(args, 1)
break -- end of options
end
if optkey:sub(-1,-1) == "=" then -- check on a blank value eg. --insert=
optval = ""
optkey = optkey:sub(1,-2)
end
if optkey then
entry =
self:__lookup(optpref == '-' and optkey or nil,
optpref == '--' and optkey or nil)
end
if not optkey or not entry then
local option_type = optval and "option" or "flag"
return cli_error("unknown/bad " .. option_type .. ": " .. opt, noprint)
end
table.remove(args,1)
if optpref == "-" then
if optval then
return cli_error("short option does not allow value through '=': "..opt, noprint)
end
if entry.flag then
optval = true
else
-- not a flag, value is in the next argument
optval = args[1]
table.remove(args, 1)
end
elseif optpref == "--" then
-- using the expanded-key notation
entry = self:__lookup(nil, optkey)
if entry then
if entry.flag then
if optval then
return cli_error("flag --" .. optkey .. " does not take a value", noprint)
else
optval = not entry.has_no_flag or (optkey:sub(1,3) ~= "no-")
end
else
if not optval then
-- value is in the next argument
optval = args[1]
table.remove(args, 1)
end
end
else
return cli_error("unknown/bad flag: " .. opt, noprint)
end
end
if type(entry.value) == 'table' then
table.insert(entry.value, optval)
else
entry.value = optval
end
-- invoke the option's parse-callback, if any
if entry.callback then
local altkey = entry.key
if optkey == entry.key then
altkey = entry.expanded_key
else
optkey = entry.expanded_key
end
local status, err = entry.callback(optkey, optval, altkey, opt)
if status == nil and err then
return cli_error(err, noprint)
end
end
end
-- missing any required arguments, or too many?
if #args < #self.required or #args > #self.required + self.optargument.maxcount then
if self.optargument.maxcount > 0 then
return cli_error("bad number of arguments: " .. #self.required .."-" .. #self.required + self.optargument.maxcount .. " argument(s) must be specified, not " .. #args, noprint)
else
return cli_error("bad number of arguments: " .. #self.required .. " argument(s) must be specified, not " .. #args, noprint)
end
end
-- deal with required args here
for i, entry in ipairs(self.required) do
entry.value = args[1]
if entry.callback then
local status, err = entry.callback(entry.key, entry.value)
if status == nil and err then
return cli_error(err, noprint)
end
end
table.remove(args, 1)
end
-- deal with the last optional argument
while args[1] do
if self.optargument.maxcount > 1 then
self.optargument.value = self.optargument.value or {}
table.insert(self.optargument.value, args[1])
else
self.optargument.value = args[1]
end
if self.optargument.callback then
local status, err = self.optargument.callback(self.optargument.key, args[1])
if status == nil and err then
return cli_error(err, noprint)
end
end
table.remove(args,1)
end
-- if necessary set the defaults for the last optional argument here
if self.optargument.maxcount > 0 and not self.optargument.value then
if self.optargument.maxcount == 1 then
self.optargument.value = self.optargument.default
else
self.optargument.value = { self.optargument.default }
end
end
-- populate the results table
local results = {}
if self.optargument.maxcount > 0 then
results[self.optargument.key] = self.optargument.value
end
for _, entry in pairs(self.required) do
results[entry.key] = entry.value
end
for _, entry in pairs(self.optional) do
if entry.key then results[entry.key] = entry.value end
if entry.expanded_key then results[entry.expanded_key] = entry.value end
end
if dump then
print("\n======= Provided command line =============")
print("\nNumber of arguments: ", #arg)
for i,v in ipairs(arg) do -- use gloabl 'arg' not the modified local 'args'
print(string.format("%3i = '%s'", i, v))
end
print("\n======= Parsed command line ===============")
if #self.required > 0 then print("\nArguments:") end
for i,v in ipairs(self.required) do
print(" " .. v.key .. string.rep(" ", self.maxlabel + 2 - #v.key) .. " => '" .. v.value .. "'")
end
if self.optargument.maxcount > 0 then
print("\nOptional arguments:")
print(" " .. self.optargument.key .. "; allowed are " .. tostring(self.optargument.maxcount) .. " arguments")
if self.optargument.maxcount == 1 then
print(" " .. self.optargument.key .. string.rep(" ", self.maxlabel + 2 - #self.optargument.key) .. " => '" .. self.optargument.key .. "'")
else
for i = 1, self.optargument.maxcount do
if self.optargument.value[i] then
print(" " .. tostring(i) .. string.rep(" ", self.maxlabel + 2 - #tostring(i)) .. " => '" .. tostring(self.optargument.value[i]) .. "'")
end
end
end
end
if #self.optional > 0 then print("\nOptional parameters:") end
local doubles = {}
for _, v in pairs(self.optional) do
if not doubles[v] then
local m = v.value
if type(m) == "string" then
m = "'"..m.."'"
else
m = tostring(m) .." (" .. type(m) .. ")"
end
print(" " .. v.label .. string.rep(" ", self.maxlabel + 2 - #v.label) .. " => " .. m)
doubles[v] = v
end
end
print("\n===========================================\n\n")
return cli_error("commandline dump created as requested per '--__DUMP__' option", noprint)
end
if not _TEST then
-- cleanup entire module, as it's single use
-- remove from package.loaded table to enable the module to
-- garbage collected.
for k, v in pairs(package.loaded) do
if v == cli then
package.loaded[k] = nil
break
end
end
-- clear table in case user holds on to module table
for k, _ in pairs(cli) do
cli[k] = nil
end
end
return results
end
--- Prints the USAGE heading.
---
--- ### Parameters
---1. **noprint**: set this flag to prevent the line from being printed
---
--- ### Returns
--- 1. a string with the USAGE message.
function cli:print_usage(noprint)
-- print the USAGE heading
local msg = "Usage: " .. tostring(self.name)
if #self.optional > 0 then
msg = msg .. " [OPTIONS]"
end
if #self.required > 0 or self.optargument.maxcount > 0 then
msg = msg .. " [--]"
end
if #self.required > 0 then
for _,entry in ipairs(self.required) do
msg = msg .. " " .. entry.key
end
end
if self.optargument.maxcount == 1 then
msg = msg .. " [" .. self.optargument.key .. "]"
elseif self.optargument.maxcount == 2 then
msg = msg .. " [" .. self.optargument.key .. "-1 [" .. self.optargument.key .. "-2]]"
elseif self.optargument.maxcount > 2 then
msg = msg .. " [" .. self.optargument.key .. "-1 [" .. self.optargument.key .. "-2 [...]]]"
end
if not noprint then print(msg) end
return msg
end
--- Prints the HELP information.
---
--- ### Parameters
--- 1. **noprint**: set this flag to prevent the information from being printed
---
--- ### Returns
--- 1. a string with the HELP message.
function cli:print_help(noprint)
local msg = self:print_usage(true) .. "\n"
local col1 = self.colsz[1]
local col2 = self.colsz[2]
if col1 == 0 then col1 = cli.maxlabel end
col1 = col1 + 3 --add margins
if col2 == 0 then col2 = 72 - col1 end
if col2 <10 then col2 = 10 end
local append = function(label, desc)
label = " " .. label .. string.rep(" ", col1 - (#label + 2))
desc = wordwrap(desc, col2) -- word-wrap
desc = desc:gsub("\n", "\n" .. string.rep(" ", col1)) -- add padding
msg = msg .. label .. desc .. "\n"
end
if self.required[1] then
msg = msg .. "\nARGUMENTS: \n"
for _,entry in ipairs(self.required) do
append(entry.key, entry.desc .. " (required)")
end
end
if self.optargument.maxcount > 0 then
append(self.optargument.key, self.optargument.desc .. " (optional, default: " .. self.optargument.default .. ")")
end
if #self.optional > 0 then
msg = msg .. "\nOPTIONS: \n"
for _,entry in ipairs(self.optional) do
local desc = entry.desc
if not entry.flag and entry.default and #tostring(entry.default) > 0 then
local readable_default = type(entry.default) == "table" and "[]" or tostring(entry.default)
desc = desc .. " (default: " .. readable_default .. ")"
elseif entry.flag and entry.has_no_flag then
local readable_default = entry.default and 'on' or 'off'
desc = desc .. " (default: " .. readable_default .. ")"
end
append(entry.label, desc)
end
end
if not noprint then print(msg) end
return msg
end
--- Sets the amount of space allocated to the argument keys and descriptions in the help listing.
--- The sizes are used for wrapping long argument keys and descriptions.
--- ### Parameters
--- 1. **key_cols**: the number of columns assigned to the argument keys, set to 0 to auto detect (default: 0)
--- 1. **desc_cols**: the number of columns assigned to the argument descriptions, set to 0 to auto set the total width to 72 (default: 0)
function cli:set_colsz(key_cols, desc_cols)
self.colsz = { key_cols or self.colsz[1], desc_cols or self.colsz[2] }
end
-- finalize setup
cli._COPYRIGHT = "Copyright (C) 2011-2014 Ahmad Amireh"
cli._LICENSE = "The code is released under the MIT terms. Feel free to use it in both open and closed software as you please."
cli._DESCRIPTION = "Commandline argument parser for Lua"
cli._VERSION = "cliargs 2.5-4"
-- aliases
cli.add_argument = cli.add_arg
cli.add_option = cli.add_opt
cli.parse_args = cli.parse -- backward compatibility
-- test aliases for local functions
if _TEST then
cli.split = split
cli.wordwrap = wordwrap
end
return cli
|