1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
# argparse
[](https://travis-ci.org/luarocks/argparse)
[](https://codecov.io/gh/luarocks/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, and can generate shell completion scripts.
## Contents
* [Example](#example)
* [Installation](#installation)
* [Tutorial](#tutorial)
* [Testing](#testing)
* [License](#license)
## Example
Simple example:
```lua
-- 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:
```bash
$ lua script.lua foo
```
```lua
{
input = "foo",
output = "a.out",
include = {}
}
```
```bash
$ lua script.lua foo -I/usr/local/include -Isrc -o bar
```
```lua
{
input = "foo",
output = "bar",
include = {"/usr/local/include", "src"}
}
```
Error messages depending on command line arguments:
```bash
$ lua script.lua foo bar
```
```
Usage: script [-h] [-o <output>] [-I <include>] <input>
Error: too many arguments
```
```bash
$ lua script.lua --help
```
```
Usage: 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.
```
```bash
$ lua script.lua foo --outptu=bar
```
```
Usage: script [-h] [-o <output>] [-I <include>] <input>
Error: unknown option '--outptu'
Did you mean '--output'?
```
## Installation
### Using LuaRocks
Installing argparse using [LuaRocks](http://luarocks.org) is simple:
```bash
$ 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](http://argparse.readthedocs.org). If argparse has been installed using LuaRocks 2.1.2 or later, it can be viewed using `luarocks doc argparse` command.
Tutorial HTML files can be built using [Sphinx](http://sphinx-doc.org/): `sphinx-build docsrc doc`, the files will be found inside `doc/`.
## Testing
argparse comes with a testing suite located in `spec` directory. [busted](http://olivinelabs.com/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).
|