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, and can generate shell completion scripts.
Simple example:
-- script.lua
local argparse = require "argparse"
local parser = argparse("script", "An example.")
parser:argument("input", "Input file.")
parser:option("-o --output", "Output file.", "a.out")
parser:option("-I --include", "Include locations."):count("*")
local args = parser:parse()args contents depending on command line arguments:
$ lua script.lua foo{
input = "foo",
output = "a.out",
include = {}
}$ lua script.lua foo -I/usr/local/include -Isrc -o bar{
input = "foo",
output = "bar",
include = {"/usr/local/include", "src"}
}Error messages depending on command line arguments:
$ lua script.lua foo barUsage: script [-h] [-o <output>] [-I <include>] <input>
Error: too many arguments
$ lua script.lua --helpUsage: script [-h] [-o <output>] [-I <include>] <input>
An example.
Arguments:
input Input file.
Options:
-h, --help Show this help message and exit.
-o <output>, --output <output>
Output file. (default: a.out)
-I <include>, --include <include>
Include locations.
$ lua script.lua foo --outptu=barUsage: script [-h] [-o <output>] [-I <include>] <input>
Error: unknown option '--outptu'
Did you mean '--output'?
argparse with CMake Support relies on an installation of Lua with CMake Support. The same toolchain which has been used with Lua with CMake Support is required. argparse gets all necessary path information from CMake liblua package data and will be installed automatically into the right directory locations - no hassle with package.path settings anymore.
Note: As argparse does not comprise any files to be compiled and linked (just files to be installed properly) the mention of architecture or configuration below is somewhat arbitrary.
Open Developer Command Prompt for VS 2022 and change drive and directory. Download and unpack sources or simply clone this repository:
c:
cd c:\Temp
git clone git@github.com:KritzelKratzel/argparse.git
cd argparseCMake strongly encourages out-of-source builds.
mkdir build
cd build
cmake .. -G "Visual Studio 17 2022" -A <arch>
cmake --build . --config Release
cmake --install . --config ReleaseReplace <arch> with your desired architecture. Available architectures with selected Visual Studio 17 2022 generator are Win32, x64, ARM and ARM64. argparse documentation is available in <lua_install_dir>/share/doc/argparse after install. In addition, a tutorial is available online.
Open Git Bash and execute ./SyncFork.sh.
John Doe@DESKTOP-1HK25HF MINGW64 /c/misc/argparse (master)
$ ./SyncFork.sh
Original remote repo found.
Already on 'master'
Your branch is up to date with 'origin/master'.
From github.com:KritzelKratzel/argparse
* branch master -> FETCH_HEAD
Already up to date.
Already up to date.
Everything up-to-date
John Doe@DESKTOP-1HK25HF MINGW64 /c/misc/argparse (master)
argparse comes with a testing suite located in `spec` directory. [busted](https://github.com/lunarmodules/busted) is required for testing, it can be installed using LuaRocks. Run the tests using `busted` command from the argparse folder.
## License
argparse is licensed under the same terms as Lua itself (MIT license).