Module GetArg


module GetArg: sig .. end
Command line parser inspired by GNU getopt.

This module offers an alternative to the standard Ocaml Arg module with a support for short and long options (cf. man 3 getopt for an example of C API).

It is loosely inspired by the Getopt module written by Alain Frish, but offers several improvements (hopefully):




Options specification


The options are specified by a list of ((short,long,description),withoutarg,witharg): Whether an option can or must take an argument is specified by (withoutarg,witharg):

type opt_spec =
| NoArg (*the option can't take an argument*)
| String of (string -> unit) (*function called on the argument*)
| Int of (int -> unit) (*function called on the converted argument*)
| Float of (float -> unit) (*function called on the converted argument*)
Callback functions binding an option to its argument.
exception Missing_argument of string
exception Unknown_option of string
exception Needless_argument of string
exception Incorrect_argument of string * string

Options specification

val print_help : unit -> unit
Display the usage message passed to GetArg.parse, then the list of options.

For each option, the available forms are displayed, then an argument if available, then a question mark if the argument is optional. Last, the description (except for all that comes before the first space) is displayed. As a consequence, if an option takes no argument and the description doesn't start with a space in the specification, its first word won't be displayed.

val parse : ?argc:int ->
?argv:string array ->
?auto_help:bool ->
((char * string * string) * (unit -> unit) option * opt_spec) list ->
(string -> unit) -> string -> unit
parse longopts process usage_msg builds a help message to be displayed with GetArg.print_help, then processes the command-line options.

The callback functions provided in longopts and by process are called in the order of the options/arguments on the command line. This is in contrast with the default behaviour of GNU getopt, where all the plain arguments are first shifted to the end, and then the process calls are made. This is easy to emulate by using process to just fill a list that will be processed after; the current behaviour is that of GNU getopt when a '-' is appended to optstring.

argc : defaults to Array.length Sys.argv
argv : defaults to Sys.argc
auto_help : whether a -h/--help option must be generated if missing