lua-users home
lua-l archive

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


Steve, Nice little module, some behaviors a little unexpected? If no
parameters entered at all I'd expect help to be displayed. Also expect
numbers to parse with or without space. Great idea, good fit with Lua.

C:\tmp>scale.lua
scale.lua:missing required parameter: scale        -- Expect help to
be shown here.

C:\tmp>scale.lua -s 2
scale.lua:missing required parameter: number     -- Expect help again.

C:\tmp>scale.lua -s 2 1
2

C:\tmp>scale.lua -s2 1 -- I'd expect 2 here and not interpret number as -2.
-2

Cheers
Andrew



On Mon, Mar 9, 2009 at 8:21 AM, steve donovan <steve.j.donovan@gmail.com> wrote:
> 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.
>