Disable undocumented ability to specify aliases as arguments
for constructors, e.g. parser:option("-f", "--foo"), and instead
order properties and pass constructor arguments to them.
E.g. parser:argument("foo", "A foo that bars") sets argument
name to foo and description to "A foo that bars".
TODO: remove "aliases" property, instead allow setting several
names in one string by separating them using space.
TODO: reorder properties so that most useful ones could be used
as constructor arguments.
argparse
argparse is a feature-rich command line parser for Lua inspired by argparse for Python.
argparse supports positional arguments, options, flags, optional arguments, subcommands and more. argparse automatically generates usage, help and error messages.
Quick glance:
-- script.lua
local argparse = require "argparse"
local parser = argparse()
:description "An example."
parser:argument "input"
:description "Input file."
parser:option "-o" "--output"
:default "a.out"
:description "Output file."
parser:option "-I" "--include"
:count "*"
:description "Include locations."
local args = parser:parse()
prettyprint(args)
$ lua script.lua foo
input: foo
output: a.out
include: {}
$ lua script.lua foo -I/usr/local/include -I/src -o bar
input: foo
output: bar
include: {/usr/local/include, /src}
$ lua script.lua foo bar
Usage: script.lua [-o <output>] [-I <include>] [-h] <input>
Error: too many arguments
$ lua script.lua --help
Usage: script.lua [-o <output>] [-I <include>] [-h] <input>
An example.
Arguments:
input Input file.
Options:
-o <output>, --output <output>
Output file. (default: a.out)
-I <include>, --include <include>
Include locations.
-h, --help Show this help message and exit.
$ lua script.lua foo --outptu=bar
Usage: script.lua [-o <output>] [-I <include>] [-h] <input>
Error: unknown option '--outptu'
Did you mean '--output'?
Contents
Installation
Using luarocks
Installing argparse using luarocks is simple.
$ luarocks install argparse
Without luarocks
Download src/argparse.lua file and put it into the directory for Lua libraries or your working directory.
Tutorial
The tutorial is available online and in the doc directory. If argparse was installed using luarocks 2.1.2 or later, it can be viewed using luarocks doc argparse command.
Testing
argparse comes with a testing suite located in spec directory. busted is required for testing, it can be installed using luarocks. Run the tests using busted spec command from the argparse folder.
License
argparse is licensed under the same terms as Lua itself(MIT license).
