2
-- http://lua-users.org/wiki/AlternativeGetOpt
4
-- getopt, POSIX style command line argument parser
5
-- param arg contains the command line arguments in a standard table.
6
-- param options is a string with the letters that expect string values.
7
-- returns a table where associated keys are true, nil, or a string value.
8
-- The following example styles are supported
9
-- -a one ==> opts["a"]=="one"
10
-- -bone ==> opts["b"]=="one"
11
-- -c ==> opts["c"]==true
12
-- --c=one ==> opts["c"]=="one"
13
-- -cdaone ==> opts["c"]==true opts["d"]==true opts["a"]=="one"
14
-- note POSIX demands the parser ends at the first non option
15
-- this behavior isn't implemented.
17
function getopt( arg, options )
19
for k, v in ipairs(arg) do
20
if string.sub( v, 1, 2) == "--" then
21
local x = string.find( v, "=", 1, true )
22
if x then tab[ string.sub( v, 3, x-1 ) ] = string.sub( v, x+1 )
23
else tab[ string.sub( v, 3 ) ] = true
25
elseif string.sub( v, 1, 1 ) == "-" then
27
local l = string.len(v)
30
jopt = string.sub( v, y, y )
31
if string.find( options, jopt, 1, true ) then
33
tab[ jopt ] = string.sub( v, y+1 )
36
tab[ jopt ] = arg[ k + 1 ]
b'\\ No newline at end of file'