[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: WANTED: POSIX compatible getopt
- From: Aleksey Cheusov <cheusov@...>
- Date: Sat, 10 Jan 2009 14:10:36 +0200
>> I'll write it in Lua
> I was hoping you'd say that... would you please also post your
> solution on the wiki page
> http://lua-users.org/wiki/AlternativeGetOpt
> as an alternative alternative? ;-)
I've done it.
Sources are there
http://mova.org/~cheusov/pub/lua_alt_getopt/
I'm new to Lua and this module is actually my first program written in Lua.
So, I need your feedback ;-)
How it works. Two method for obtaining options are provided.
1) opts+optarg return values indexed by numbers 1,2,...N
2) associative array optarg: key - option name, value - 1 or value
In both cases 'optind' return value is provided that points
to the first non-option argument.
Main features:
1) compatible to SUS "Utility Syntax Guidelines"
http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap12.html#tag_12_02
2) No extra dependencies
3) No hooks ( Yet? :-) ), no functional tricks
4) Support for long options
5) Support for -kVALUE, -k VALUE, --key=VALUE, --key VALUE,
-abcd is equivalent to -a -b -c -d if neither of them accept value.
6) Strict error checking (incorrect use of an option)
7) alt_getopt.lua is a module
Example is below
Tarball provides lots of examples which are a part unit tests.
Run 'make test' for tests.
BSD make is required, NetBSD make is preferred.
Under Linux ooooold version of NetBSD is widely available under name pmane.
FreeBSD make may be sufficient (not tested). OpenBSD make - don't know.
After some period of time (after I stabilize the code) I'll move it
sourceforge or luaforge and add final link and examples to lua wiki.
#############################################################################
0 lua_alt_getopt>cat alt_getopt
#!/usr/bin/env lua
require "alt_getopt"
local long_opts = {
verbose = "v",
help = "h",
fake = 0,
len = 1,
output = "o",
}
local ret
local optarg
local optind
opts,optind,optarg = alt_getopt.get_ordered_opts (arg, "hVvo:n:", long_opts)
for i,v in ipairs (opts) do
if optarg [i] then
io.write ("option `" .. v .. "': " .. optarg [i] .. "\n")
else
io.write ("option `" .. v .. "'\n")
end
end
optarg,optind = alt_getopt.get_opts (arg, "hVvo:n:", long_opts)
for k,v in pairs (optarg) do
io.write ("fin-option `" .. k .. "': " .. v .. "\n")
end
for i = optind,#arg do
io.write (string.format ("ARGV [%s] = %s\n", i, arg [i]))
end
0 lua_alt_getopt>./alt_getopt -h
option `h'
fin-option `h': 1
0 lua_alt_getopt>./alt_getopt -o
Missed value for option `-o'
1 lua_alt_getopt>./alt_getopt --output file1
option `o': file1
fin-option `o': file1
0 lua_alt_getopt>./alt_getopt --output file1 -o file2
option `o': file1
option `o': file2
fin-option `o': file2
0 lua_alt_getopt>./alt_getopt --output file1 -o file2 --output=file3
option `o': file1
option `o': file2
option `o': file3
fin-option `o': file3
0 lua_alt_getopt>./alt_getopt -hVv -n123 -n 234
option `h'
option `V'
option `v'
option `n': 123
option `n': 234
fin-option `n': 234
fin-option `h': 1
fin-option `v': 1
fin-option `V': 1
0 lua_alt_getopt>./alt_getopt -n1 -- --file1-- --file2--
option `n': 1
fin-option `n': 1
ARGV [3] = --file1--
ARGV [4] = --file2--
0 lua_alt_getopt>./alt_getopt -n1 - file2 file3
option `n': 1
fin-option `n': 1
ARGV [2] = -
ARGV [3] = file2
ARGV [4] = file3
0 lua_alt_getopt>./alt_getopt -ho
Bad usage of option `-o'
1 lua_alt_getopt>./alt_getopt --help=oops
Bad usage of option `--help=oops'
1 lua_alt_getopt>
--
Best regards, Aleksey Cheusov.
- References:
- WANTED: POSIX compatible getopt, Aleksey Cheusov
- Re: WANTED: POSIX compatible getopt, Javier Guerra
- Re: WANTED: POSIX compatible getopt, Aleksey Cheusov
- Re: WANTED: POSIX compatible getopt, Rob Kendrick
- Re: WANTED: POSIX compatible getopt, Aleksey Cheusov
- Re: WANTED: POSIX compatible getopt, Rob Kendrick
- Re: WANTED: POSIX compatible getopt, Aleksey Cheusov
- Re: WANTED: POSIX compatible getopt, Rob Kendrick
- Re: WANTED: POSIX compatible getopt, Aleksey Cheusov
- Re: WANTED: POSIX compatible getopt, Rob Kendrick
- Re: WANTED: POSIX compatible getopt, Aleksey Cheusov
- Re: WANTED: POSIX compatible getopt, Rob Kendrick
- Re: WANTED: POSIX compatible getopt, Aleksey Cheusov
- Re: WANTED: POSIX compatible getopt, Doug Currie