1995-08-09 08:06:35 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Objective Caml *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
2000-01-07 08:47:25 -08:00
|
|
|
(* Damien Doligez, projet Para, INRIA Rocquencourt *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
|
|
|
(* under the terms of the GNU Library General Public License. *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
2001-10-26 15:37:14 -07:00
|
|
|
(** Parsing of command line arguments.
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2001-10-26 15:37:14 -07:00
|
|
|
This module provides a general mechanism for extracting options and
|
1996-10-24 07:17:48 -07:00
|
|
|
arguments from the command line to the program.
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2001-10-26 15:37:14 -07:00
|
|
|
Syntax of command lines:
|
1995-05-04 03:15:53 -07:00
|
|
|
A keyword is a character string starting with a [-].
|
|
|
|
An option is a keyword alone or followed by an argument.
|
1998-04-06 09:33:34 -07:00
|
|
|
The types of keywords are: [Unit], [Set], [Clear], [String],
|
|
|
|
[Int], [Float], and [Rest]. [Unit], [Set] and [Clear] keywords take
|
|
|
|
no argument. [String], [Int], and [Float] keywords take the following
|
|
|
|
word on the command line as an argument. A [Rest] keyword takes the
|
|
|
|
remaining of the command line as (string) arguments.
|
1996-10-24 07:17:48 -07:00
|
|
|
Arguments not preceded by a keyword are called anonymous arguments.
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2001-10-26 15:37:14 -07:00
|
|
|
Examples ([cmd] is assumed to be the command name):
|
1995-05-04 03:15:53 -07:00
|
|
|
- [cmd -flag ](a unit option)
|
|
|
|
- [cmd -int 1 ](an int option with argument [1])
|
|
|
|
- [cmd -string foobar ](a string option with argument ["foobar"])
|
|
|
|
- [cmd -float 12.34 ](a float option with argument [12.34])
|
1996-10-24 07:17:48 -07:00
|
|
|
- [cmd a b c ](three anonymous arguments: ["a"], ["b"], and ["c"])
|
1998-04-06 09:33:34 -07:00
|
|
|
- [cmd a b -- c d ](two anonymous arguments and a rest option with
|
2001-10-26 15:37:14 -07:00
|
|
|
two arguments)
|
1995-05-04 03:15:53 -07:00
|
|
|
*)
|
|
|
|
|
|
|
|
type spec =
|
2001-12-03 14:01:28 -08:00
|
|
|
Unit of (unit -> unit) (** Call the function with unit argument *)
|
2001-10-26 15:37:14 -07:00
|
|
|
| Set of bool ref (** Set the reference to true *)
|
|
|
|
| Clear of bool ref (** Set the reference to false *)
|
|
|
|
| String of (string -> unit) (** Call the function with a string argument *)
|
|
|
|
| Int of (int -> unit) (** Call the function with an int argument *)
|
|
|
|
| Float of (float -> unit) (** Call the function with a float argument *)
|
2001-12-03 14:01:28 -08:00
|
|
|
| Rest of (string -> unit) (** Stop interpreting keywords and call the
|
|
|
|
function with each remaining argument *)
|
|
|
|
|
|
|
|
(** The concrete type describing the behavior associated
|
|
|
|
with a keyword. *)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2001-12-03 14:01:28 -08:00
|
|
|
val parse :
|
|
|
|
(string * spec * string) list -> (string -> unit) -> string -> unit
|
2001-10-26 15:37:14 -07:00
|
|
|
(** [Arg.parse speclist anonfun usage_msg] parses the command line.
|
1996-10-24 07:17:48 -07:00
|
|
|
[speclist] is a list of triples [(key, spec, doc)].
|
1997-07-03 07:15:35 -07:00
|
|
|
[key] is the option keyword, it must start with a ['-'] character.
|
1996-10-24 07:17:48 -07:00
|
|
|
[spec] gives the option type and the function to call when this option
|
|
|
|
is found on the command line.
|
|
|
|
[doc] is a one-line description of this option.
|
|
|
|
[anonfun] is called on anonymous arguments.
|
|
|
|
The functions in [spec] and [anonfun] are called in the same order
|
|
|
|
as their arguments appear on the command line.
|
|
|
|
|
1998-04-27 02:55:50 -07:00
|
|
|
If an error occurs, [Arg.parse] exits the program, after printing
|
|
|
|
an error message as follows:
|
1996-10-24 07:17:48 -07:00
|
|
|
- The reason for the error: unknown option, invalid or missing argument, etc.
|
1997-05-21 08:28:30 -07:00
|
|
|
- [usage_msg]
|
1996-10-24 07:17:48 -07:00
|
|
|
- The list of options, each followed by the corresponding [doc] string.
|
|
|
|
|
|
|
|
For the user to be able to specify anonymous arguments starting with a
|
1998-04-06 09:33:34 -07:00
|
|
|
[-], include for example [("-", String anonfun, doc)] in [speclist].
|
1996-10-24 07:17:48 -07:00
|
|
|
|
2001-08-21 08:10:51 -07:00
|
|
|
By default, [parse] recognizes two unit options, [-help] and [--help],
|
|
|
|
which will display [usage_msg] and the list of options, and exit
|
|
|
|
the program. You can override this behaviour by specifying your
|
|
|
|
own [-help] and [--help] options in [speclist].
|
1996-10-24 07:17:48 -07:00
|
|
|
*)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2001-12-03 14:01:28 -08:00
|
|
|
exception Bad of string
|
2001-10-26 15:37:14 -07:00
|
|
|
(** Functions in [spec] or [anonfun] can raise [Arg.Bad] with an error
|
|
|
|
message to reject invalid arguments. *)
|
1996-10-24 08:19:22 -07:00
|
|
|
|
2001-12-03 14:01:28 -08:00
|
|
|
val usage : (string * spec * string) list -> string -> unit
|
2001-10-26 15:37:14 -07:00
|
|
|
(** [Arg.usage speclist usage_msg] prints an error message including
|
1998-04-27 02:55:50 -07:00
|
|
|
the list of valid options. This is the same message that
|
2001-10-26 15:37:14 -07:00
|
|
|
{!Arg.parse} prints in case of error.
|
|
|
|
[speclist] and [usage_msg] are the same as for [Arg.parse]. *)
|
1997-07-03 07:15:35 -07:00
|
|
|
|
2001-12-03 14:01:28 -08:00
|
|
|
val current : int ref
|
2001-10-26 15:37:14 -07:00
|
|
|
(** Position (in {!Sys.argv}) of the argument being processed. You can
|
|
|
|
change this value, e.g. to force {!Arg.parse} to skip some arguments.*)
|