lua-users home
lua-l archive

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


2016-04-11 20:42 GMT+01:00 Petite Abeille <petite.abeille@gmail.com>:
> Would anyone recommend a Lua command line parsing module?
>
> Aside from these:
>
> http://lua-users.org/wiki/CommandLineParsing
>
> Preferably something in Lua, small, and more or less POSIX compliant.
>
> Suggestions?

I'd suggest my config module which is all that but not POSIX
compliant: https://bitbucket.org/doub/config/src/tip/config.lua

In code it looks like this:

local config = require 'config'

local options = {
    name = nil,
    value = nil,
    use_x = false,
    use_y = false,
    use_z = true,
}
config.load(options, "mytool.conf")
config.args(options, ...)

if #options >= 1 then options.name = options[1] end
if #options >= 2 then options.value = options[2] end

config.check("mytool", {
    {options.name, "no name provided"},
    {type(options.name)=="string", "invalid name"},
    {options.value~=nil, "no value provided"},
}, [[
usage: mytool [options] [name [value]]

options:
    -name <name>
    -value <value>
    --use-x
    --use-y
    --use-z
]])

assert(options.name=="foo")
assert(options.value==42)
assert(options.use_x==true)
assert(options.use_y==true)
assert(options.use_z==false)

And to pass the asserts then you can run it like that: lua mytool
-use-x true --use-y --no-use-z -value 42 foo

It has a few more advanced features that you'll have to find out from
the code unless you give me a decent reason to write the doc :D

Doub.