The primary export you receive when you require the library. For example:
local cli = require 'cliargs'
Sets the amount of space allocated to the argument keys and descriptions in the help listing.
The sizes are used for wrapping long argument keys and descriptions.
number
The number of columns assigned to the argument keys, set to 0 to auto detect.
number
The number of columns assigned to the argument descriptions, set to 0 to auto set the total width to 72.
Load default values from a table.
table
Your new set of defaults. The keys could either point to the short or expanded option keys, and their values are the new defaults.
boolean
Turn this on to return nil and an error message if a key in the config table could not be mapped to any CLI option.
true
When the new defaults were loaded successfully, or strict was not set.
union.<nil, string>
When strict was set and there was an error.
Read config values from a configuration file.
string
Absolute file path.
string
The config file format, which has to be one of: "lua", "json", "ini", or "yaml". When this is left blank, we try to auto-detect the format from the file extension.
boolean
Forwarded to #load_defaults. See that method for the parameter description.
true|union.<nil, string>
Returns true on successful load. Otherwise, nil and an error message are returned instead.
Define a required argument.
Required arguments do not take a symbol like -
or --
, may not have a
default value, and are parsed in the order they are defined.
For example:
cli:argument('INPUT', 'path to the input file')
cli:argument('OUTPUT', 'path to the output file')
At run-time, the arguments have to be specified using the following notation:
$ ./script.lua ./main.c ./a.out
If the user does not pass a value to every argument, the parser will raise an error.
string
The argument identifier that will be displayed to the user and be used to reference the run-time value.
string
A description for this argument to display in usage help.
function
Callback to invoke when this argument is parsed.
Defines a "splat" (or catch-all) argument.
This is a special kind of argument that may be specified 0 or more times, the values being appended to a list.
For example, let's assume our program takes a single output file and works on multiple source files:
cli:argument('OUTPUT', 'path to the output file')
cli:splat('INPUTS', 'the sources to compile', nil, 10) -- up to 10 source files
At run-time, it could be invoked as such:
$ ./script.lua ./a.out file1.c file2.c main.c
If you want to make the output optional, you could do something like this:
cli:option('-o, --output=FILE', 'path to the output file', './a.out')
cli:splat('INPUTS', 'the sources to compile', nil, 10)
And now we may omit the output file path:
$ ./script.lua file1.c file2.c main.c
string
The argument's "name" that will be displayed to the user.
string
A description of the argument.
A default value.
number
The maximum number of occurences allowed.
function
A function to call everytime a value for this argument is parsed.
Defines an optional argument.
Optional arguments can use 3 different notations, and can accept a value.
string
The argument identifier. This can either be -key
, or
-key, --expanded-key
.
Values can be specified either by appending a space after the
identifier (e.g. -key value
or --expanded-key value
) or by
separating them with a =
(e.g. -key=value
or
--expanded-key=value
).
string
A description for the argument to be shown in --help.
bool
A default value to use in case the option was not specified at run-time (the default value is nil if you leave this blank.)
function
A callback to invoke when this option is parsed.
The following option will be stored in args["i"]
and args["input"]
with a default value of file.txt
:
cli:option("-i, --input=FILE", "path to the input file", "file.txt")
Define an optional "flag" argument.
Flags are a special subset of options that can either be true
or false
.
For example:
cli:flag('-q, --quiet', 'Suppress output.', true)
At run-time:
$ ./script.lua --quiet
$ ./script.lua -q
Passing a value to a flag raises an error:
$ ./script.lua --quiet=foo
$ echo $? # => 1
Flags may be negatable by prepending [no-]
to their key:
cli:flag('-c, --[no-]compress', 'whether to compress or not', true)
Now the user gets to pass --no-compress
if they want to skip
compression, or either specify --compress
explicitly or leave it
unspecified to use compression.
string
string
function
Define a command argument.
string
The name of the command and the argument that the user has to supply to invoke it.
string
An optional string to show in the help listing which should describe what the command does. It will be displayed if --help was run on the main program.
cmd
Another instance of the CLI library which is scoped to that command.
Parse the process arguments table.
table.<string>
The list of arguments to parse. Defaults to the global arg
table
which contains the arguments the process was started with.
table
A table containing all the arguments, options, flags, and splat arguments that were specified or had a default (where applicable).
array.<nil, string>
If a parsing error has occured, note that the --help option is also considered an error.
This is a special instance of the cli module that you receive when you define a new command using cli#command.