Simple, conventional argument parsing with a friendly license. For Lua 5.1 and later. See the source file header for full documentation.
getopt = require('getopt')
local append = false
local binary = false
local color = 'white'
local nonoptions = {}
local infile = io.input()
for opt, arg in getopt(arg, 'abc:h', nonoptions) do
if opt == 'a' then
append = true
elseif opt == 'b' then
binary = true
elseif opt == 'c' then
color = arg
elseif opt == 'h' then
usage()
os.exit(0)
elseif opt == '?' then
print('error: unknown option: ' .. arg)
os.exit(1)
elseif opt == ':' then
print('error: missing argument: ' .. arg)
os.exit(1)
end
end
if #nonoptions == 1 then
infile = io.open(nonoptions[1], 'r')
elseif #nonoptions > 1 then
print('error: wrong number of arguments: ' .. #nonoptions)
os.exit(1)
end
-- ...
$ lua tests.lua