lua-users home
lua-l archive

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

> On 11 Apr 2016, at 21:50, Petite Abeille <> wrote:
>> On Apr 11, 2016, at 10:38 PM, Coda Highland <> wrote:
>> So the question really was, what style of command line parsing is
>> actually desired?
> Ultimately, a combination of short options, long options, and sub commands ala svn.
> So, I guess, something along the lines of docopt in term of expressiveness:

Here’s mine, optparse:

The idea is that you write the help text output by hand, and feed that to
optparse, which returns an option parser.  When you run that option parser
against the command line arguments it raises errors for unrecognised or
otherwise malformed options, and returns an opts table set according to
what arguments it processed.

It’s reasonably configurable too if you have any unusual requirements. But
often enough, you only need the help text string.


The optparse package returns a parser factory when loaded:

    local OptionParser = require "optparse"

    local help = [[
    parseme (optparse spec) 0α1

    Copyright © 2016 Gary V. Vaughan
    This test program comes with ABSOLUTELY NO WARRANTY.

    Usage: parseme [<options>] <file>...

    Banner text.

    Long description.


      -h, --help               display this help, then exit
          --version            display version information, then exit
      -b                       a short option with no long option
          --long               a long option with no short option
          --another-long       a long option with internal hypen
          --true               a Lua keyword as an option name
      -v, --verbose            a combined short and long option
      -n, --dryrun, --dry-run  several spellings of the same option
      -u, --name=USER          require an argument
      -o, --output=[FILE]      accept an optional argument
      --                       end of options

    Footer text.

    Please report bugs at <>.

   local parser = OptionParser (help)
   local arg, opts = parser:parse (_G.arg)