Module getopt

Simplified getopt, based on Svenne Panne's Haskell GetOpt.
Usage:

  • options = Options {Option {...} ...}
    getopt.processArgs ()
  • Assumes prog = {name[, banner] [, purpose] [, notes] [, usage]}
  • Options take a single dash, but may have a double dash.
  • Arguments may be given as -opt=arg or -opt arg.
  • If an option taking an argument is given multiple times, only the last value is returned; missing arguments are returned as 1.
getOpt, usageInfo and usage can be called directly (see below, and the example at the end). Set _DEBUG.std to a non-nil value to run the example.
  • TODO: Sort out the packaging. getopt.Option is tedious to type, but surely Option shouldn't be in the root namespace?
  • TODO: Wrap all messages; do all wrapping in processArgs, not usageInfo; use sdoc-like library (see string.format todos).
  • TODO: Don't require name to be repeated in banner.
  • TODO: Store version separately (construct banner?).

Functions

_G.Options (t) Options table constructor: adds lookup tables for the option names
getOpt (argIn, options) Perform argument processing
processArgs () Simple getOpt wrapper.
usage () Emit a usage message.
usageInfo (header, optDesc, pageWidth) Produce usage info for the given options

Tables

_G.Option Options table type.


Functions

_G.Options (t)
Options table constructor: adds lookup tables for the option names

Parameters

  • t:
getOpt (argIn, options)
Perform argument processing

Parameters

  • argIn: list of command-line args
  • options: options table

Return values:

  1. table of remaining non-options
  2. table of option key-value list pairs
  3. table of error messages
processArgs ()
Simple getOpt wrapper. Adds -version/-v and -help/-h/-? automatically; stops program if there was an error, or if -help or -version was used.
usage ()
Emit a usage message.
usageInfo (header, optDesc, pageWidth)
Produce usage info for the given options

Parameters

  • header: header string
  • optDesc: option descriptors
  • pageWidth: width to format to [78]

Return value:

formatted string

Tables

_G.Option
Options table type.

Fields

  • name: list of names
  • desc: description of this option
  • type: type of argument (if any): Req(uired), Opt(ional)
  • var: descriptive name for the argument
  • func: optional function (newarg, oldarg) to convert argument into actual argument, (if omitted, argument is left as it is)

Valid XHTML 1.0!