lua-users home
lua-l archive

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


On 7/15/2016 1:57 PM, Dirk Laurie wrote:
It took some time to read them all.
....
I could find argparse, optparse and pl.lapp.....

lapp is a table with several fields, one of which looks promising.
....
help(lapp)
Contents: add_type assert callback error open process_options_string
    quit show_usage_error
help(lapp.process_options_string)
--- process a Lapp options string.
-- Usually called as `lapp()`.
-- @string str the options text
-- @tparam {string} args a table of arguments (default is `_G.arg`)
-- @return a table with parameter-value pairs

OK, this one is on the shortlist. I may need to read its docs after
all, in which case the others come back into contention.

Even better, it has metatable that provides __call(). You can use the additional methods it supplies if you need to extend it or fine tune its behavior.

Most of my casual utility scripts quickly get something like this at the top:

local lapp = require 'pl.lapp'
local args = lapp[[
Process something and fill a folder full of output files.
  --boxes                   Show layout boxes layer
  --dpi (default 300)       DPI for scaling
  -o,--outputfolder (default output)
                            Output folder
  --png                     Output PNG not PDF

Some additional options you shouldn't need:
  -v                        Be more verbose
  --debug                   Dump internal data structures
]]

The __call() parses the single string argument along with the global variable _G.arg to learn what options are allowed and which are present. The table it returns has fields named after the long name of every option. It supports easy things like boolean flags and default values with rather natural syntax in the Usage text. It can do more fancy things with some effort.

--
Ross Berteig                               Ross@CheshireEng.com
Cheshire Engineering Corp.           http://www.CheshireEng.com/
+1 626 303 1602