Module pl.lapp
Simple command-line parsing using human-readable specification.
Supports GNU-style parameters.
lapp = require 'pl.lapp'
local args = lapp [[
Does some calculations
-o,--offset (default 0.0) Offset to add to scaled number
-s,--scale (number) Scaling factor
<number> (number ) Number to be scaled
]]
print(args.offset + args.scale * args.number)
Lines begining with ‘–’ are flags; there may be a short and a long name; lines begining wih ‘’ are arguments. Anything in parens after the flag/argument is either a default, a type name or a range constraint.
>See the Guide
Dependencies: pl.sip
Functions
quit (msg, no_usage) | quit this script immediately. |
error (msg, no_usage) | print an error to stderr and quit. |
open (file, opt) | open a file. |
assert (condn, msg) | quit if the condition is false. |
add_type (name, converter, constraint) | add a new type to Lapp. |
process_options_string (str, args) | process a Lapp options string. |
Fields
show_usage_error | controls whether to dump usage on error. |
Functions
- quit (msg, no_usage)
-
quit this script immediately.
Parameters:
msg
: optional messageno_usage
: suppress ‘usage’ display
- error (msg, no_usage)
-
print an error to stderr and quit.
Parameters:
msg
: a messageno_usage
: suppress ‘usage’ display
- open (file, opt)
-
open a file.
This will quit on error, and keep a list of file objects for later cleanup.
Parameters:
file
: filenameopt
: same as second parameter ofio.open
- assert (condn, msg)
-
quit if the condition is false.
Parameters:
condn
: a conditionmsg
: an optional message
- add_type (name, converter, constraint)
-
add a new type to Lapp. These appear in parens after the value like
a range constraint, e.g. ‘
(integer) Process PID’ Parameters:
name
: name of typeconverter
: either a function to convert values, or a Lua type name.constraint
: optional function to verify values, should use lapp.error if failed.
- process_options_string (str, args)
-
process a Lapp options string.
Usually called as lapp().
Parameters:
str
: the options textargs
: a table of arguments (default is_G.arg
)
Returns:
-
a table with parameter-value pairs