Module getopt

Argument parsing.

Provides an argument parser loosely inspired by GNU getopt; its interface is tailored to Lua rather than C, and it is thereby somewhat less obtuse. The code is taken from ULOS 1 and has been modified very slightly to work with ULOS 2.

Functions

getopt (_opts, _args) Process program arguments according to the given options.
build (supported) Build a set of options and usage information.
process (parameters) Process options more easily.

Tables

options Options given to getopt
Supported Supported options given to build.


Functions

getopt (_opts, _args)
Process program arguments according to the given options. Takes the given table of options, and processes the given arguments accordingly. The returned table of options will vary slightly depending whether the can_repeat_opts option is set. If it is set, all values returned in the opts table will be tables containing an array of all the values given to that option over one or more occurrences of that option (possibly just one item). Otherwise, the most recent occurrence of an option takes precedence and all values in opts are strings. Keys in the returned opts table are always strings corresponding to the name of the option. Option names should never begin with a - or --.

Parameters:

  • _opts table The options to use
  • _args table The arguments to process

Returns:

  1. table The arguments left over (args)
  2. table The provided options (opts)
build (supported)
Build a set of options and usage information. For some example usage, see getopt.build.lua.

Parameters:

Returns:

  1. table Options to pass to the options field of getopt
  2. string Usage information, indented by two spaces
  3. function A wrapper over getopt that condenses all options to their short forms
process (parameters)
Process options more easily. Effectively build and getopt combined. For example usage, see getopt.process.lua.

Parameters:

  • parameters table An amalgamation of the arguments given to build and getopt, with the addition of an args field to hold the raw arguments.

Returns:

  1. table The arguments left over
  2. table The options left over
  3. string Usage information for the given options.

Tables

options
Options given to getopt

Fields:

  • options table Key-value pairs of all possible options, where the key is the option name and the value is a boolean indicating whether that option takes an argument. This is the only mandatory field.
  • help_message string A help message that is written to the standard error stream when an error condition is reached.
  • exit_on_bad_opt boolean Whether it is an error to find an invalid option (one not specified in the options table).
  • finish_after_arg boolean Whether to stop processing options after any non-option argument is reached.
  • can_repeat_opts boolean Whether options may be repeated.
  • exclude_numbers boolean Whether to exclude numbers from short options, e.g. -700
Supported
Supported options given to build.

Fields:

  • 1 string A short description of the option
  • 2 string or boolean The name of the argument the option takes, or false if it does not take one
  • 3 string The short name of the option
  • 4 string The long name of the option (optional)
generated by LDoc 1.4.6 Last updated 2023-03-04 11:42:14