Program Options

This module allows the use of command-line options in HALMD scripts.


halmd liquid.lua --lennard-jones epsilon=2 sigma=2 --disable-gpu
class halmd.utility.program_options.argument_parser

Create new command-line parser.


local options = require("halmd.utility.program_options")

local parser = options.argument_parser()
add_argument(name, args)

Add argument to parser.

  • name (string) – long name, and (optionally) short name separated by comma
  • args (table) – keyword arguments
  • args.type (string) – value type of option
  • args.dtype (string) – element type of vector or matrix (optional)
  • (string) – description of option for –help (optional)
  • args.composing (boolean) – allow multiple occurences with single value (default: false)
  • args.multitoken (boolean) – allow multiple occurences with multiple values (default: false)
  • args.required (boolean) – require at minimum one occurence (default: false)
  • args.choices (table) – allowed values with descriptions (optional)
  • args.action (function) – argument handler function (optional)
  • args.default – default option value (optional)
  • args.implicit – implicit option value (optional)

The following value types are supported:

Type Description
boolean Boolean
string String
accumulate Increment integer
vector 1-dimensional array of type dtype
matrix 2-dimensional array of type dtype

These integral and floating-point value types are supported:

Type Description
number Double-precision floating-point
integer Signed 64-bit integer
int32 Signed 32-bit integer
int64 Signed 64-bit integer
uint32 Unsigned 32-bit integer
uint64 Unsigned 64-bit integer
float32 Single-precision floating-point
float64 Double-precision floating-point


parser:add_argument("disable-gpu", {type = "boolean", help = "disable GPU acceleration"})

An optional table choices may be used to constrain the value of an argument:

parser.add_argument("ensemble", {type = "string", choices = {
    nve = "Constant NVE",
    nvt = "Constant NVT",
    npt = "Constant NPT",
}, help = "statistical ensemble"})

Note that only arguments of type string are supported.

The optional action function receives the following arguments:

  • args (table) – parsed arguments
  • key (string) – args table key of this argument
  • value – parsed value of this argument

Note that if you specify action, the argument value will not be stored in the table returned by parse_args(), i.e. the argument handler function has to store a value in args[key] itself.


parser:add_argument("output", {type = "string", action = function(args, key, value)
   -- substitute current time
   args[key] =
end, default = "halmd_%Y%m%d_%H%M%S.h5", help = "H5MD output file"})
add_argument_group(name, args)

Add argument group.

  • name – name of argument group
  • args (table) – keyword arguments (optional)
  • (string) – description of argument group for –help (optional)

argument group


local group = parser:add_argument_group("lennard-jones")
group:add_argument("epsilon", {type = "number", help = "potential well depths"})
group:add_argument("sigma", {type = "number", help = "collision diameter"})

Set default option values.

Parameters:defaults (table) – argument names with default values


parser:set_defaults({particles = {9000, 1000}, number_density = 0.8})

Parse arguments.

Parameters:args (table) – sequence of arguments (optional)
Returns:parsed arguments

If args is not specified, the command-line arguments are parsed.


local args = parser:parse_args()