lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi all,

This little utilty was inspired by the discussions around standard
parsing of command-line args. Well, there does seem to be standards
for such args (e.g. GNU style) but that's no reason to use standard
thinking to parse them.

Here is a little example, typical of a small command-line script:

require 'lapp'
local args = lapp [[
Trims output lines to a maximum length
    -n,--trim_col (default 72)        Maximum line length
    <input file> (default stdin)      File to be read
    <output file> (default stdout) Trimmed file to be written
]]
local n = args.trim_col
local outf = args.output_file
for line in args.input_file:lines() do
    outf:write(line:sub(1,n)..'\n')
end

Another example:

-- scale.lua
require 'lapp'
local args = lapp [[
Does some calculations
    -o,--offset (default 0.0)  Offset to add to scaled number
    -s,--scale  (number)  Scaling factor
    <number> (number )  Number to be scaled
]]

print(args.offset + args.scale * args.number)
----
The central idea of lapp is that since we have to write help for our
scripts, it may as well be semantically meaningful help.  Parameters
are either flags or arguments, and they can be either required (in
which case their type is specified in parens) or not (in which case
their default value is in parens, and the type deduced from this)  The
tedious stuff like opening files and issuing error messages is handled
for you.

-h or --help is trivially implemented by dumping out this structured
usage string.

This is the 'proof-of-concept' release; lapp.lua is only 211 lines
long, so it has room to grow. An idea I'm exploring is the option to
generate a default GUI form from this information, since we all have
command-line-challenged users.

http://mysite.mweb.co.za/residents/sdonovan/lapp.zip

steve d.