package batteries

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type

This module contains the option parser itself.

It provides functions to create, populate and use option parsers to parse command line arguments.

Exceptions
exception Option_conflict of string

Option_conflict name is raised by OptParse.OptParser.add when two different options are added with identical names. Usually this doesn't need to be caught since this error is usually easily fixed permanently by removing/renaming the conflicting option names.

Types
type t

The type of an option parser.

type group

The type of an option group.

Option parser creation
val make : ?usage:string -> ?description:string -> ?version:string -> ?suppress_usage:bool -> ?suppress_help:bool -> ?only_leading_opts:bool -> ?prog:string -> ?formatter:Formatter.t -> unit -> t

Creates a new option parser with the given options.

  • parameter usage

    Usage message. The default is a reasonable usage message for most programs. Any occurrence of the substring "%prog" in usage is replaced with the name of the program (see prog).

  • parameter prog

    Program name. The default is the base name of the executable.

  • parameter suppress_usage

    Suppress the usage message if set.

  • parameter suppress_help

    Suppress the 'help' option which is otherwise added by default.

  • parameter only_leading_opts

    Only consider leading options (options appearing before the first non-option argument). All arguments from the first non-option argument on are returned as the arguments.

  • parameter version

    Version string. If set, a '--version' option is automatically added. When encountered on the command line it causes version to be printed to the standard output and the program to exit.

  • parameter description:

    description of the main purpose of the program.

  • returns

    the new option parser.

val add : t -> ?group:group -> ?help:string -> ?hide:bool -> ?short_name:char -> ?short_names:char list -> ?long_name:string -> ?long_names:string list -> 'a Opt.t -> unit

Add an option to the option parser.

  • raises Option_conflict

    if the short name(s) or long name(s) have alread been used for some other option.

  • parameter help

    Short help message describing the option (for the usage message).

  • parameter hide

    If true, hide the option from the usage message. This can be used to implement "secret" options which are not shown, but work just the same as regular options in all other respects.

  • parameter short_name

    is the name for the short form of the option (e.g. 'x' means that the option is invoked with -x on the command line).

  • parameter short_names

    is a list of names for the short form of the option (see short_name).

  • parameter long_name

    is the name for the long form of the option (e.g. "xyzzy" means that the option is invoked with --xyzzy on the command line).

  • parameter long_names

    is a list of names for the long form of the option (see long_name).

val add_group : t -> ?parent:group -> ?description:string -> string -> group

Add a group to the option parser.

  • parameter parent

    is the parent group (if any).

  • parameter description

    is a description of the group.

  • returns

    the new group.

Output and error handling
val error : t -> ?chn:out_channel -> ?status:int -> string -> unit

Display an error message and exit the program. The error message is printed to the channel chn (default is Pervasives.stderr) and the program exits with exit status status (default is 1).

val usage : t -> ?chn:out_channel -> unit -> unit

Display the usage message to the channel chn (default is Pervasives.stdout) and return.

Option parsing
val parse : t -> ?first:int -> ?last:int -> string array -> string list

Parse arguments as if the arguments args.(first), args.(first+1), ..., args.(last) had been given on the command line. By default first is 0 and last is the index of the last element of the array.

val parse_argv : t -> string list

Parse all the arguments in Sys.argv.

OCaml

Innovation. Community. Security.