module Getopt:Modulesig
..end
Getopt
: parsing of command line arguments.
Copyright (C) 2000-2004 Alain Frisch. Distributed under the terms of the MIT license.
email: Alain Frisch@ens.fr
web: http://www.eleves.ens.fr/home/frisch
This module provides a general mechanism for extracting options and
arguments from the command line to the program. It is an alternative
to the module Arg
from the standard OCaml distribution.
The syntax is close to GNU getopt and getop_long (man 3 getopt
).
Options may have an argument attached. For the long form, the syntax is "--option=argument". For the short form, there are two possible syntaxes: "-o argument" (argument doesn't start with a dash) and "-oargument"
Short options that refuse arguments may be concatenated, as in "-opq".
The special argument -- interrupts the parsing of options: all the remaining arguments are arguments even they start with a dash.
Command line specification
A specification lists the possible options and describe what to do
when they are found; it also gives the action for anonymous arguments
and for the special option - (a single dash alone).
typeopt =
char * string * (unit -> unit) option * (string -> unit) option
(short_form, long_form, action, handler)
where:short_form
is the character for the short form of the option
without the leading -
(or noshort='\000'
if the option does not have a short form)long_form
is the string for the long form of the option
without the leading --
(or nolong=""
if no long form)(action : (unit -> unit) option)
gives the action to be executed
when the option is found without an argument(handler : (string -> unit) option)
specifies how to handle the
argument when the option is found with the argument(action, handler)
, the corresponding option
may, must or mustn't have an argument :
(Some _, Some _)
: the option may have an argument; the short form can't be
concatenated with other options (even if the user does not want to provide
an argument). The behaviour (handler/action) is determined by the
presence of the argument.(Some _, None)
: the option must not have an argument; the short form, if
it exists, may be concatenated(None, Some _)
: the option must have an argument; the short form can't
be concatenated(None, None)
: not allowedval noshort : char
noshort='\000'
can be used when an option has no short formval nolong : string
nolong=""
can be used when an option has no long formexception Error of string
val parse : opt list -> (string -> unit) -> string array -> int -> int -> unit
parse opts others args first last
parse the arguments
args.(first)
, arg.(first+1)
... args.(last)
.
others
is called on anonymous arguments (and the special - argument);
opts
is a list of option specifications (there must be no ambiguities).Error
: Unknown options, unexpected argument, ...val parse_cmdline : opt list -> (string -> unit) -> unit
Sys.argv
using parse
.val set : 'a Pervasives.ref -> 'a -> (unit -> unit) option
val incr : int Pervasives.ref -> (unit -> unit) option
int
referenceval append : string list Pervasives.ref -> (string -> unit) option
string list
referenceval atmost_once : string Pervasives.ref -> exn -> (string -> unit) option
string
reference if
it is empty, raises an exception otherwise