1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset= ISO-8859-1">
<TITLE>
Module Arg: parsing of command line arguments
</TITLE>
</HEAD>
<BODY >
<A HREF="manual032.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<A HREF="manual030.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
<HR>
<H2>17.1 Module <TT>Arg</TT>: parsing of command line arguments</H2><A NAME="s:Arg"></A>
<A NAME="@manual155"></A><BLOCKQUOTE>
This module provides a general mechanism for extracting options and
arguments from the command line to the program.</BLOCKQUOTE>
<BLOCKQUOTE>
Syntax of command lines:
A keyword is a character string starting with a <CODE>-</CODE>.
An option is a keyword alone or followed by an argument.
The types of keywords are: <CODE>Unit</CODE>, <CODE>Set</CODE>, <CODE>Clear</CODE>, <CODE>String</CODE>,
<CODE>Int</CODE>, <CODE>Float</CODE>, and <CODE>Rest</CODE>. <CODE>Unit</CODE>, <CODE>Set</CODE> and <CODE>Clear</CODE> keywords take
no argument. <CODE>String</CODE>, <CODE>Int</CODE>, and <CODE>Float</CODE> keywords take the following
word on the command line as an argument. A <CODE>Rest</CODE> keyword takes the
remaining of the command line as (string) arguments.
Arguments not preceded by a keyword are called anonymous arguments.</BLOCKQUOTE>
<BLOCKQUOTE>
Examples (<CODE>cmd</CODE> is assumed to be the command name):<BR><CODE>cmd -flag </CODE>(a unit option)<BR><CODE>cmd -int 1 </CODE>(an int option with argument <CODE>1</CODE>)<BR><CODE>cmd -string foobar </CODE>(a string option with argument <CODE>"foobar"</CODE>)<BR><CODE>cmd -float 12.34 </CODE>(a float option with argument <CODE>12.34</CODE>)<BR><CODE>cmd a b c </CODE>(three anonymous arguments: <CODE>"a"</CODE>, <CODE>"b"</CODE>, and <CODE>"c"</CODE>)<BR><CODE>cmd a b -- c d </CODE>(two anonymous arguments and a rest option with<BR><CODE> </CODE> two arguments)</BLOCKQUOTE>
<PRE>
type spec =
| Unit of (unit -> unit) (* Call the function with unit argument *)
| 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 *)
| Rest of (string -> unit) (* Stop interpreting keywords and call the
function with each remaining argument *)
</PRE>
<BLOCKQUOTE>
The concrete type describing the behavior associated
with a keyword.
</BLOCKQUOTE>
<PRE>
val parse : (string * spec * string) list -> (string -> unit) -> string -> unit
</PRE>
<A NAME="@manual156"></A><BLOCKQUOTE><CODE>Arg.parse speclist anonfun usage_msg</CODE> parses the command line.
<CODE>speclist</CODE> is a list of triples <CODE>(key, spec, doc)</CODE>.
<CODE>key</CODE> is the option keyword, it must start with a <CODE>'-'</CODE> character.
<CODE>spec</CODE> gives the option type and the function to call when this option
is found on the command line.
<CODE>doc</CODE> is a one-line description of this option.
<CODE>anonfun</CODE> is called on anonymous arguments.
The functions in <CODE>spec</CODE> and <CODE>anonfun</CODE> are called in the same order
as their arguments appear on the command line.<BR>
<BR>
If an error occurs, <CODE>Arg.parse</CODE> exits the program, after printing
an error message as follows:<BR>The reason for the error: unknown option, invalid or missing argument, etc.<BR><CODE>usage_msg</CODE><BR>The list of options, each followed by the corresponding <CODE>doc</CODE> string.<BR>
<BR>
For the user to be able to specify anonymous arguments starting with a
<CODE>-</CODE>, include for example <CODE>("-", String anonfun, doc)</CODE> in <CODE>speclist</CODE>.<BR>
<BR>
By default, <CODE>parse</CODE> recognizes a unit option <CODE>-help</CODE>, which will
display <CODE>usage_msg</CODE> and the list of options, and exit the program.
You can override this behaviour by specifying your own <CODE>-help</CODE>
option in <CODE>speclist</CODE>.</BLOCKQUOTE>
<PRE>
exception Bad of string
</PRE>
<A NAME="@manual157"></A><BLOCKQUOTE>Functions in <CODE>spec</CODE> or <CODE>anonfun</CODE> can raise <CODE>Arg.Bad</CODE> with an error
message to reject invalid arguments.</BLOCKQUOTE>
<PRE>
val usage: (string * spec * string) list -> string -> unit
</PRE>
<A NAME="@manual158"></A><BLOCKQUOTE><CODE>Arg.usage speclist usage_msg</CODE> prints an error message including
the list of valid options. This is the same message that
<CODE>Arg.parse</CODE> prints in case of error.
<CODE>speclist</CODE> and <CODE>usage_msg</CODE> are the same as for <CODE>Arg.parse</CODE>.</BLOCKQUOTE>
<PRE>
val current: int ref;;
</PRE>
<A NAME="@manual159"></A><BLOCKQUOTE>Position (in <CODE>Sys.argv</CODE>) of the argument being processed. You can
change this value, e.g. to force <CODE>Arg.parse</CODE> to skip some arguments.</BLOCKQUOTE>
<HR>
<A HREF="manual032.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<A HREF="manual030.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
</BODY>
</HTML>
|