@eryx/argparse Module

JSON

Imperative command-line argument parser.

Supports positional arguments, flags, options with values, subcommands, and merged short flags. A built-in --help/-h flag is registered by default and can be customised or disabled.

local argparse = require("@eryx/argparse")

local parser = argparse.Parser.new("mytool", "A handy CLI")
parser:addPositional("file", "string", nil, true, "Input file")
parser:addFlag("verbose", "v", "Enable verbose output")
parser:addOption("port", "p", "number", 8080, nil, "Port to listen on")

local run = parser:addSubCommand("run", "Run a script")
run:addPositional("script", "string", nil, true, "Script to run")

local config = parser:parse()

Summary

Classes

name: string
description: string?
epilog: string?
positionals: { PositionalSpec }
flags: { TFlag }
options: { OptionSpec }
subCommands: { [string]: Parser }
mutexGroups: { MutuallyExclusiveGroup }
_helpEnabled: boolean
Parser.new(name: string, description: string?, epilog: string?)Parser
Parser:addSubCommand(name: string, description: string?, epilog: string?)Parser
Parser:setHelpHandler(handler: ((self: Parser) → ()))()
Parser:addPositional(name: string, type: "string" | "number", default: any?, required: boolean?, description: string?, variadic: boolean?)()
Parser:addFlag(longName: string, shortName: string?, description: string?)()
Parser:addOption(longName: string, shortName: string?, type: "string" | "number", default: any?, required: boolean?, description: string?, settings: { multiple: boolean?, arity: number? }?)()
Parser:addMutuallyExclusive(names: { string }, required: boolean?)()
Parser:getCompletionCandidates(arguments: { string }?){ string }
Parser:generateCompletion(shell: "bash" | "zsh" | "fish" | "powershell", programName: string?)string
Parser:generateHelp()string
Parser:parse(arguments: { string }?){ [string]: any }

API Reference

Classes

Parser

A command-line parser that can be configured with positional arguments, boolean flags, key-value options, and subcommands.

Every new parser comes with a built-in --help/-h flag. Use Parser.disableHelp to remove it or Parser.setHelpHandler to replace its behaviour.

Properties

name: string

The program or subcommand name shown in help text.

description: string?

One-line summary printed below the usage line.

epilog: string?

Text printed at the very end of help output.

positionals: { PositionalSpec }

Registered positional arguments, in order.

flags: { TFlag }

Registered boolean flags.

options: { OptionSpec }

Registered key-value options.

subCommands: { [string]: Parser }

Registered subcommands, keyed by name.

mutexGroups: { MutuallyExclusiveGroup }

Mutually exclusive groups of flag/option names.

_helpEnabled: boolean

Whether the built-in --help / -h behavior is active.

Parser:_helpHandler

The function invoked when --help is parsed.

Parser:_helpHandler()()

Parser.new

Creates a new Parser.

The parser starts with a built-in --help/-h flag that prints generated help text and exits.

Parser.new(name: string, description: string?, epilog: string?)Parser

Parameters

name: string

The program or subcommand name shown in usage text.

description: string?

One-line summary printed below the usage line.

epilog: string?

Text printed at the very end of help output.

Returns

A new parser ready to be configured.

Parser:addSubCommand

Registers a subcommand under this parser.

When the subcommand name is encountered during parsing, all remaining arguments are delegated to the subcommand's parser. The subcommand's result table is stored under its name in the parent result.

Parser:addSubCommand(name: string, description: string?, epilog: string?)Parser

Parameters

name: string

The subcommand name (must not collide with an existing subcommand).

description: string?

One-line summary shown in help text.

epilog: string?

Text printed at the end of the subcommand's help.

Returns

The new subcommand parser, ready to be configured.

Parser:disableHelp

Removes the built-in --help/-h flag and sets the help handler to a no-op. After calling this, --help and -h are free to be re-registered as custom flags or options.

Parser:disableHelp()()

Parser:setHelpHandler

Replaces the built-in help behaviour with a custom handler.

The handler receives the parser so it can call Parser.generateHelp or inspect registered arguments. The built-in --help/-h flag remains registered; only the action changes.

parser:setHelpHandler(function(p)
	io.write(p:generateHelp())
	os.exit(2)
end)
Parser:setHelpHandler(handler: ((self: Parser) → ()))()

Parameters

handler: ((self: Parser) → ())

A function called when --help or -h is parsed.

Parser:addPositional

Registers a positional argument.

Positional arguments are consumed left-to-right in the order they are added. Required positionals must come before any optional ones.

If a default is provided the positional is automatically optional. To make a positional optional with no default, pass nil for default and false for required.

Set variadic to true to let the positional consume all remaining arguments. Variadic positionals must be the final positional.

Parser:addPositional(name: string, type: "string" | "number", default: any?, required: boolean?, description: string?, variadic: boolean?)()

Parameters

name: string

Key used in the result table.

type: "string" | "number"

"string" or "number" - numeric values are coerced automatically.

default: any?

Default value when omitted. Implies optional when non-nil.

required: boolean?

Whether the argument must be provided. Defaults to true unless a default is given.

description: string?

Human-readable text shown in help output.

variadic: boolean?

Whether this positional should consume all remaining values.

Parser:addFlag

Registers a boolean flag (true when present, false when absent).

Flags can be combined in short form: -vh sets both -v and -h.

Parser:addFlag(longName: string, shortName: string?, description: string?)()

Parameters

longName: string

The --long name (without dashes).

shortName: string?

Optional single-character -s alias.

description: string?

Human-readable text shown in help output.

Parser:addOption

Registers a key-value option.

Options consume the next argument (or the =-separated value) as their value. In merged short form the last character is treated as the option and everything after it (or the next argument) is the value: -vp 8080 sets -v (flag) and -p 8080 (option).

If a default is provided the option is automatically optional. To make an option optional with no default, pass nil for default and false for required.

settings.multiple = true allows an option to be repeated and collects its values into an array. settings.arity controls how many values each occurrence consumes.

Parser:addOption(longName: string, shortName: string?, type: "string" | "number", default: any?, required: boolean?, description: string?, settings: { multiple: boolean?, arity: number? }?)()

Parameters

longName: string

The --long name (without dashes).

shortName: string?

Optional single-character -s alias.

type: "string" | "number"

"string" or "number" - numeric values are coerced automatically.

default: any?

Default value when omitted. Implies optional when non-nil.

required: boolean?

Whether the option must be provided. Defaults to true unless a default is given.

description: string?

Human-readable text shown in help output.

settings: { multiple: boolean?, arity: number? }?

Optional parser settings such as multiple and arity.

Parser:addMutuallyExclusive

Registers a mutually exclusive group of flags/options by long name.

At most one name in the group may be present during parsing. If required is true, exactly one member must be present.

Parser:addMutuallyExclusive(names: { string }, required: boolean?)()

Parser:getCompletionCandidates

Returns shell completion candidates for the parser after consuming the provided fully-typed arguments.

This is intentionally conservative: if the parser is currently waiting for an option value it returns no candidates so the shell can fall back to filename or custom value completion.

Parser:getCompletionCandidates(arguments: { string }?){ string }

Parser:generateCompletion

Generates a shell completion script for this parser.

Supports "bash", "zsh", "fish", and "powershell", and generates subcommand-aware completions for commands and flags/options.

Parser:generateCompletion(shell: "bash" | "zsh" | "fish" | "powershell", programName: string?)string

Parser:generateHelp

Builds a human-readable help string from the parser's current configuration, including usage line, description, arguments, subcommands, flags, options, and epilog.

Parser:generateHelp()string

Returns

string

The formatted help text.

Parser:parse

Parses command-line arguments against this parser's configuration.

Parsing supports:

  • Positional arguments - consumed left-to-right.
  • Long flags / options - --verbose, --port 8080, --port=8080.
  • Short flags / options - -v, -p 8080.
  • Merged short flags - -vh expands to -v -h. The last character may be an option: -vp 8080 -> -v flag + -p 8080 option.
  • Subcommands - remaining arguments are delegated to the subcommand's parser and its result is stored under the subcommand name.

If the built-in --help flag is set, the help handler is invoked before required-argument validation, so mytool --help always works.

Parser:parse(arguments: { string }?){ [string]: any }

Parameters

arguments: { string }?

Argument list to parse. Defaults to os.cliargs().

Returns

{ [string]: any }

A table mapping argument names to their parsed values.