Module eco.cli

Command-line argument parsing utilities.

Functions

parse_args (spec[, argv]) Parse command-line options and positional arguments.


Functions

parse_args (spec[, argv])
Parse command-line options and positional arguments.

The parser follows POSIX/GNU getopt_long-style conventions: long options use --name or --name=value, short options use -x, and single-dash multi-character forms are parsed as short option bundles or short option values, not as long options. Use -- to stop option parsing.

Parsed option values are returned in a result table keyed by option name; positional arguments are returned in result.args. If argv is omitted, the global arg table is used and arg[0] is ignored.

Each option supports these fields:

  • name (required): result field name and default long option name
  • short: one-character short option
  • long: long option name; defaults to name
  • type: "boolean" (default), "string", "number", "integer", "count" or "array"
  • default: default value; array defaults must be arrays of strings
  • required: fail if the option is not present

Supported value forms include --name value, --name=value, -x value, and -xvalue. Boolean and count short options may be bundled, such as -vvq.

Parameters:

  • spec table Option specification array.
  • argv table Argument array. Defaults to global arg. (optional)

Returns:

    table Parsed options with positional arguments in args.

Or

  1. nil On parse failure.
  2. string Error message.

Usage:

    local cli = require 'eco.cli'
    local opts, err = cli.parse_args({
        { name = 'dev', short = 'i', type = 'string', required = true },
        { name = 'promisc', short = 'p' },
        { name = 'verbose', short = 'v', type = 'count', default = 0 },
        { name = 'include', short = 'I', type = 'array' },
    })
    if not opts then
        io.stderr:write(err, '\n')
        os.exit(1)
    end
generated by LDoc 1.5.0 Last updated 2026-08-06 11:36:12