Copyright (C) 2014-2018, 2021-2022, 2026 Gary V. Vaughan
In the common case, you can write the long-form help output typical of a modern command line program, and let this module generate a custom parser that collects and diagnoses the options it describes.
The parser is actually an object instance which can then be tweaked for the uncommon case, by hand, or by using the "on" method to tie your custom handlers to options that are not handled quite the way you'd like.
This is a pure Lua library compatible with LuaJIT, Lua 5.1, 5.2, 5.3, 5.4 and 5.5.
The simplest and best way to install optparse is with LuaRocks. To install the latest release (recommended):
luarocks install optparseTo install current git master (for testing, before submitting a bug report for example):
luarocks install https://raw.githubusercontent.com/gvvaughan/optparse/master/optparse-dev-1.rockspecThe best way to install without LuaRocks is to copy init.lua and
_strict.lua from lib/optparse into an optparse directory in your
package search path.
optparse includes comprehensive documentation, which is written in doc/optparse.md.
Please make bug reports and suggestions as GitHub Issues. Pull requests are especially appreciated.
But first, please check that your issue has not already been reported by someone else, and that it is not already fixed by master in preparation for the next release (see Installation section above for how to temporarily install master with LuaRocks).
There is no strict coding style, but please bear in mind the following points when proposing changes:
-
Follow existing code. There are a lot of useful patterns and avoided traps there.
-
3-character indentation using SPACES in Lua sources: It makes rogue TABs easier to see, and lines up nicely with 'if' and 'end' keywords.
-
Simple strings are easiest to type using single-quote delimiters, saving double-quotes for where a string contains apostrophes.
-
Save horizontal space by only using SPACEs where the parser requires them.
-
Use vertical space to separate out compound statements to help the coverage reports discover untested lines.
-
Prefer explicit string function calls over object methods, to mitigate issues with monkey-patching in caller environment.