options_parser

Options Parser is used to parse options provided to command-line programs.

e.g. to parse a version flag and an output argument:

  # set up an options parser
  options := options_parser_new()
  options.title(options, "testproc: a fancy CLI program")
  options_add(options, ["-v", "--version"], "", "show version")
  options_add(options, ["-o", "--output"] , "filename", "set output filename")
  # parse some input, e.g. arglist could be arguments passed to main
  options_parse(options, arglist)
  if options_has(options, "-v") then write_version()
  if options_has(options, "-h") then write_help(options)
  if options_has(options, "-o") then output_file := options_value(options, "-o")

Note that `options_has` and `options_value` always use the first of the two options as their key, irrespective of the option used by the caller.

Procedures

options_add (object, forms, value, help_string)

Adds a new option definition to current options object.

Parameters:

object
the options object to add the definition to.
forms
the short and optional long form of the option, presented as either a single string, like "-v", or a pair ["-v", "--version"].
value
the optional value for the option: use "" for no value.
help_string
the string to use for this option when writing the help message.

options_arguments (object)

Returns the remaining elements of the parsed string, after any options.

Parameters:

object
the options object containing the definitions to use.

options_has (object, form)

Succeeds if form was used in parsed input.

Parameters:

object
the options object containing the definitions to use.
form
the argument short form, as a string.

options_parse (object, arglist)

Parses the given list of input arguments.

Parameters:

object
the options object containing the definitions to use.
arglist
list of string arguments to process.

options_parser_new ()

Returns a new record ready to store option definitions.


options_title (object, title)

Adds a title string to the current options object.

Parameters:

object
the options object to add the title to.
title
the string to write first when writing the help message.

options_value (object, form)

Returns value of form from parsed input, or fails if form not found.

Parameters:

object
the options object containing the definitions to use.
form
the argument short form, as a string.

options_write_help (object)

Writes the help message.

Parameters:

object
the options object containing the definitions to use.

Home