GNU compatible access to command-line options.

This document describes the GETOPTS unit for Free Pascal. It was written for linux by Michael Van Canneyt. It now also works for all supported platforms.

The getopts unit provides a mechanism to handle command-line options in a structured way, much like the GNU getopts mechanism. It allows you to define the valid options for you program, and the unit will then parse the command-line options for you, and inform you of any errors.

Character indicating an option on the command-line. Command-line ordering options. Don't touch the ordering of the command-line options Change command-line options. Return options in the correct order. Specifies that a long option does not take an argument. Specifies that a long option needs an argument. Specifies that a long option optionally takes an argument. Returned by , to indicate that there are no more options. Long option description record The TOption type is used to communicate the long options to . The Name field is the name of the option. Has_arg specifies if the option wants an argument, Flag is a pointer to a char, which is set to Value, if it is non-nil. Long option name Does the option have arguments (values) Flag Value to return when option is encountered Pointer to record. Set to the argument of an option, if the option needs one. Index of the current paramstr(). when all options have been processed, optind is the index of the first non-option parameter. This is a read-only variable. Note that it can become equal to paramcount+1. Indicates whether getopt() prints error messages. In case of an error, contains the character causing the error. Return next long option.

Returns the next option found on the command-line, taking into account long options as well. If no more options are found, returns EndOfOptions. If the option requires an argument, it is returned in the OptArg variable.

ShortOptions is a string containing all possible one-letter options. (see for its description and use) LongOpts is a pointer to the first element of an array of Option records, the last of which needs a name of zero length.

The function tries to match the names even partially (i.e. --app will match e.g. the append option), but will report an error in case of ambiguity. If the option needs an argument, set Has_arg to Required_argument, if the option optionally has an argument, set Has_arg to Optional_argument. If the option needs no argument, set Has_arg to zero.

Required arguments can be specified in two ways :

  1. Pasted to the option : --option=value
  2. As a separate argument : --option value

Optional arguments can only be specified through the first method.

see .
Get next short option.

Returns the next option found on the command-line. If no more options are found, returns EndOfOptions. If the option requires an argument, it is returned in the OptArg variable.

ShortOptions is a string containing all possible one-letter options. If a letter is followed by a colon (:), then that option needs an argument. If a letter is followed by 2 colons, the option has an optional argument. If the first character of shortoptions is a '+' then options following a non-option are regarded as non-options (standard Unix behavior). If it is a '-', then all non-options are treated as arguments of a option with character #0. This is useful for applications that require their options in the exact order as they appear on the command-line. If the first character of shortoptions is none of the above, options and non-options are permuted, so all non-options are behind all options. This allows options and non-options to be in random order on the command line.

Errors are reported through giving back a '?' character. OptOpt then gives the character which caused the error. If OptErr is True then getopt prints an error-message to stdout.