Module Posix_getopt

POSIX command-line option parsing bindings.

This module provides OCaml bindings to the POSIX getopt functions defined in unistd.h.

It supports short options (single character), long options (GNU extension), and various argument handling modes.

Option Types

type short = char

Short option: a single character (e.g., 'v' for -v).

type long = string * char

Long option: a pair of (long name, short equivalent). For example, ("verbose", 'v') matches both --verbose and -v.

type arg = [
  1. | `None of unit -> unit
  2. | `Optional of string option -> unit
  3. | `Required of string -> unit
]

Argument specification for an option.

  • `None f - Option takes no argument. f () is called when matched.
  • `Optional f - Option has an optional argument. f (Some arg) or f None.
  • `Required f - Option requires an argument. f arg is called with the value.
type 'a opt = {
  1. name : 'a;
  2. arg : arg;
}

Option specification combining name and argument handler.

Exceptions

exception Unknown_option of string

Raised when an unknown option is encountered.

exception Missing_argument of char

Raised when a required argument is missing for the given option.

Feature Detection

val has_getopt_long : bool

true if the system supports GNU-style long options via getopt_long.

val has_getopt_long_only : bool

true if the system supports getopt_long_only (long options with single dash).

Configuration

val print_error : bool -> unit

Enable or disable error messages printed to stderr by getopt. Default is true (errors are printed).

val reset : unit -> unit

Reset the getopt state for parsing a new set of arguments. Call this before parsing a new argv if you've already parsed arguments.

Parsing Functions

val getopt : string array -> short opt list -> string array

Parse command-line arguments using short options only.

See getopt(3).

  • parameter argv

    The argument array to parse (typically Sys.argv).

  • parameter opts

    List of short option specifications.

  • returns

    Array of non-option arguments (positional arguments).

Example:

  let verbose = ref false in
  let output = ref "out.txt" in
  let opts = [
    { name = 'v'; arg = `None (fun () -> verbose := true) };
    { name = 'o'; arg = `Required (fun s -> output := s) };
  ] in
  let args = getopt Sys.argv opts in
  (* args contains non-option arguments *)
val getopt_long : string array -> long opt list -> string array

Parse command-line arguments using long options (GNU extension).

See getopt_long(3).

Long options are specified as --name or --name=value. Each long option also has a short equivalent.

  • parameter argv

    The argument array to parse.

  • parameter opts

    List of long option specifications.

  • returns

    Array of non-option arguments.

val getopt_long_only : string array -> long opt list -> string array

Like getopt_long but also accepts long options with a single dash.

For example, -verbose is treated the same as --verbose.

  • parameter argv

    The argument array to parse.

  • parameter opts

    List of long option specifications.

  • returns

    Array of non-option arguments.