module GetArg:Command line parser inspired by GNU getopt.sig
..end
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):
--foo bar
is
an alternative to --foo=bar
if --foo
must take an argument)-y 42
is equivalent to --y-option 42
, ie. an argument-less
option and a plain argument, instead of being equivalent to -y42
and thus to --y-option=42
, ie. an argument supplied to an option)-z
takes
no argument, -zx 42
is legal even if -x
may or must take an
argument, and is equivalent to -z -x 42
((short,long,description),withoutarg,witharg)
:short
is the short form of the option ('\\000'
for no short form)long
is the long form of the option (""
for no long form)description
is the one-line description of the option.
Like in Arg
, the first space splits the description into the displayed
form of the argument (if needed) and the actual description
("<n> This option does…" becomes "--option <n> This option does…").
Unlike in Arg
, the descriptions are wrapped, and aligned by defaultwithoutarg
is an optional unit -> unit
called for options without
argumentwitharg
is an optional function called for options with an argument(withoutarg,witharg)
:None,NoArg
: silent optionNone,_
: option with a mandatory argument_,NoArg
: option with no argument_,_
: option with an optional argument; the second function is called
whenever an argument is found in collapsed form (-xy42
or
--y-option=42
for -y
/--y-option
),
otherwise the first is be usedtype
opt_spec =
| |
NoArg |
(* | the option can't take an argument | *) |
| |
String of |
(* | function called on the argument | *) |
| |
Int of |
(* | function called on the converted argument | *) |
| |
Float of |
(* | function called on the converted argument | *) |
exception Missing_argument of string
exception Unknown_option of string
exception Needless_argument of string
exception Incorrect_argument of string * string
val print_help : unit -> unit
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