lua-users home
lua-l archive

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


Hello, 

I wrote a library for parsing command-line arguments called argparse [1]. 

The library is a loose port of the Python library of the same name [2]. Argparse allows to create a parser object and fill it with positional arguments, options and flags. These elements and the parser itself can be configured in several ways. 

Here is a very basic example: 

    local argparse = require "argparse"
    local parser = argparse()
    parser:argument "input" -- a regular positional argument
    parser:option "-o" "--output" -- an option...
       :default "a.out" -- ... with a default value
    parser:option "-I" "--include" -- an option...
       :count "*" -- ... which can be used several times
    local args = parser:parse()

(A boring, less declarative interface can be used, too)

And it can be used like this: 

    $ lua script.lua foo -I/usr/local/include -I/src --output bar

Of course, pretty error, usage and help messages are generated automatically and displayed when necessary. 

A not very practical but 'clever' feature is automatic typo detection: 

    $ lua script.lua foo --outptu=bar
    Usage: script.lua [-o <output>] [-I <include>] [-h] <input>
    
    Error: unknown option '--outptu'
    Did you mean '--output'?

There some other higher-level features like automatic conversions and validation, callbacks and commands(subparsers triggered by a special positional argument, e.g. luarocks `install`). All the features are showcased in the tutorial [3], available via 'luarocks doc argparse'. 

I've posted the rockspec on the luarocks mailing list, in the meantime the module can be installed from moonrocks: 

    luarocks install --server=http://rocks.moonscript.org/manifests/mpeterv argparse

Cheers, 

Peter


[1] https://github.com/mpeterv/argparse
[2] http://docs.python.org/3.4/library/argparse.html
[3] http://mpeterv.github.io/argparse/