lua-users home
lua-l archive

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


On Thu, Dec 23, 2010 at 14:38, Cheusov Aleksey <cheusov@tut.by> wrote:
>> So what are you all using for Getopt or preferably Getopt_long in Lua?
>
> http://luaforge.net/projects/alt-getopt/
>
> It support both POSIX short options and long options.
>
>

I use a relatively simple process:
For each item in arg:
1) Remove all leading hyphens
2) Split at the first '=', creating two strings 'name' and 'value'. If
there is no '=', name = the entire string, value = nil.
3) Look up 'name' in a table of functions; call the function with
'value' as parameter. If no function by this name is found, display
brief help and exit.

Short names can be done by having aliases in the table (argfunc.l =
argfunc.list). So "./foo.lua --something -a=b more options here=1" is
valid. This doesn't however handle multiple single-character options
("./foo.lua -abcd" as short for "./foo.lua -a -b -c -d"); you might
try that in step 3 before exiting.

-- 
Sent from my toaster.