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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
<html>
<head>
<link rel="stylesheet" href="style.css" type="text/css">
<link rel="Start" href="index.html">
<link rel="next" href="Arith_status.html">
<link rel="Up" href="index.html">
<link title="Index of types" rel=Appendix href="index_types.html">
<link title="Index of exceptions" rel=Appendix href="index_exceptions.html">
<link title="Index of values" rel=Appendix href="index_values.html">
<link title="Index of modules" rel=Appendix href="index_modules.html">
<link title="Index of module types" rel=Appendix href="index_module_types.html">
<link title="Arg" rel="Chapter" href="Arg.html">
<link title="Arith_status" rel="Chapter" href="Arith_status.html">
<link title="Array" rel="Chapter" href="Array.html">
<link title="ArrayLabels" rel="Chapter" href="ArrayLabels.html">
<link title="Big_int" rel="Chapter" href="Big_int.html">
<link title="Bigarray" rel="Chapter" href="Bigarray.html">
<link title="Buffer" rel="Chapter" href="Buffer.html">
<link title="Callback" rel="Chapter" href="Callback.html">
<link title="CamlinternalMod" rel="Chapter" href="CamlinternalMod.html">
<link title="CamlinternalOO" rel="Chapter" href="CamlinternalOO.html">
<link title="Char" rel="Chapter" href="Char.html">
<link title="Complex" rel="Chapter" href="Complex.html">
<link title="Condition" rel="Chapter" href="Condition.html">
<link title="Dbm" rel="Chapter" href="Dbm.html">
<link title="Digest" rel="Chapter" href="Digest.html">
<link title="Dynlink" rel="Chapter" href="Dynlink.html">
<link title="Event" rel="Chapter" href="Event.html">
<link title="Filename" rel="Chapter" href="Filename.html">
<link title="Format" rel="Chapter" href="Format.html">
<link title="Gc" rel="Chapter" href="Gc.html">
<link title="Genlex" rel="Chapter" href="Genlex.html">
<link title="Graphics" rel="Chapter" href="Graphics.html">
<link title="GraphicsX11" rel="Chapter" href="GraphicsX11.html">
<link title="Hashtbl" rel="Chapter" href="Hashtbl.html">
<link title="Int32" rel="Chapter" href="Int32.html">
<link title="Int64" rel="Chapter" href="Int64.html">
<link title="Lazy" rel="Chapter" href="Lazy.html">
<link title="Lexing" rel="Chapter" href="Lexing.html">
<link title="List" rel="Chapter" href="List.html">
<link title="ListLabels" rel="Chapter" href="ListLabels.html">
<link title="Map" rel="Chapter" href="Map.html">
<link title="Marshal" rel="Chapter" href="Marshal.html">
<link title="MoreLabels" rel="Chapter" href="MoreLabels.html">
<link title="Mutex" rel="Chapter" href="Mutex.html">
<link title="Nativeint" rel="Chapter" href="Nativeint.html">
<link title="Num" rel="Chapter" href="Num.html">
<link title="Obj" rel="Chapter" href="Obj.html">
<link title="Oo" rel="Chapter" href="Oo.html">
<link title="Parsing" rel="Chapter" href="Parsing.html">
<link title="Pervasives" rel="Chapter" href="Pervasives.html">
<link title="Printexc" rel="Chapter" href="Printexc.html">
<link title="Printf" rel="Chapter" href="Printf.html">
<link title="Queue" rel="Chapter" href="Queue.html">
<link title="Random" rel="Chapter" href="Random.html">
<link title="Scanf" rel="Chapter" href="Scanf.html">
<link title="Set" rel="Chapter" href="Set.html">
<link title="Sort" rel="Chapter" href="Sort.html">
<link title="Stack" rel="Chapter" href="Stack.html">
<link title="StdLabels" rel="Chapter" href="StdLabels.html">
<link title="Str" rel="Chapter" href="Str.html">
<link title="Stream" rel="Chapter" href="Stream.html">
<link title="String" rel="Chapter" href="String.html">
<link title="StringLabels" rel="Chapter" href="StringLabels.html">
<link title="Sys" rel="Chapter" href="Sys.html">
<link title="Thread" rel="Chapter" href="Thread.html">
<link title="ThreadUnix" rel="Chapter" href="ThreadUnix.html">
<link title="Unix" rel="Chapter" href="Unix.html">
<link title="UnixLabels" rel="Chapter" href="UnixLabels.html">
<link title="Weak" rel="Chapter" href="Weak.html"><title>Arg</title>
</head>
<body>
<div class="navbar"> <a href="index.html">Up</a>
<a href="Arith_status.html">Next</a>
</div>
<center><h1>Module <a href="type_Arg.html">Arg</a></h1></center>
<br>
<pre><span class="keyword">module</span> Arg: <code class="code"><span class="keyword">sig</span></code> <a href="Arg.html">..</a> <code class="code"><span class="keyword">end</span></code></pre>Parsing of command line arguments.
<p>
This module provides a general mechanism for extracting options and
arguments from the command line to the program.
<p>
Syntax of command lines:
A keyword is a character string starting with a <code class="code">-</code>.
An option is a keyword alone or followed by an argument.
The types of keywords are: <code class="code"><span class="constructor">Unit</span></code>, <code class="code"><span class="constructor">Bool</span></code>, <code class="code"><span class="constructor">Set</span></code>, <code class="code"><span class="constructor">Clear</span></code>,
<code class="code"><span class="constructor">String</span></code>, <code class="code"><span class="constructor">Set_string</span></code>, <code class="code"><span class="constructor">Int</span></code>, <code class="code"><span class="constructor">Set_int</span></code>, <code class="code"><span class="constructor">Float</span></code>, <code class="code"><span class="constructor">Set_float</span></code>,
<code class="code"><span class="constructor">Tuple</span></code>, <code class="code"><span class="constructor">Symbol</span></code>, and <code class="code"><span class="constructor">Rest</span></code>.
<code class="code"><span class="constructor">Unit</span></code>, <code class="code"><span class="constructor">Set</span></code> and <code class="code"><span class="constructor">Clear</span></code> keywords take no argument. A <code class="code"><span class="constructor">Rest</span></code>
keyword takes the remaining of the command line as arguments.
Every other keyword takes the following word on the command line
as argument.
Arguments not preceded by a keyword are called anonymous arguments.
<p>
Examples (<code class="code">cmd</code> is assumed to be the command name):<ul>
<li> <code class="code">cmd -flag </code>(a unit option)</li>
<li> <code class="code">cmd -int 1 </code>(an int option with argument <code class="code">1</code>)</li>
<li> <code class="code">cmd -string foobar </code>(a string option with argument <code class="code"><span class="string">"foobar"</span></code>)</li>
<li> <code class="code">cmd -float 12.34 </code>(a float option with argument <code class="code">12.34</code>)</li>
<li> <code class="code">cmd a b c </code>(three anonymous arguments: <code class="code"><span class="string">"a"</span></code>, <code class="code"><span class="string">"b"</span></code>, and <code class="code"><span class="string">"c"</span></code>)</li>
<li> <code class="code">cmd a b -- c d </code>(two anonymous arguments and a rest option with
two arguments)</li>
</ul>
<br>
<hr width="100%">
<br><code><span class="keyword">type</span> <a name="TYPEspec"></a><code class="type"></code>spec = </code><table class="typetable">
<tr>
<td align="left" valign="top" >
<code><span class="keyword">|</span></code></td>
<td align="left" valign="top" >
<code><span class="constructor">Unit</span> <span class="keyword">of</span> <code class="type">(unit -> unit)</code></code></td>
<td class="typefieldcomment" align="left" valign="top" ><code>(*</code></td><td class="typefieldcomment" align="left" valign="top" >Call the function with unit argument</td><td class="typefieldcomment" align="left" valign="bottom" ><code>*)</code></td>
</tr>
<tr>
<td align="left" valign="top" >
<code><span class="keyword">|</span></code></td>
<td align="left" valign="top" >
<code><span class="constructor">Bool</span> <span class="keyword">of</span> <code class="type">(bool -> unit)</code></code></td>
<td class="typefieldcomment" align="left" valign="top" ><code>(*</code></td><td class="typefieldcomment" align="left" valign="top" >Call the function with a bool argument</td><td class="typefieldcomment" align="left" valign="bottom" ><code>*)</code></td>
</tr>
<tr>
<td align="left" valign="top" >
<code><span class="keyword">|</span></code></td>
<td align="left" valign="top" >
<code><span class="constructor">Set</span> <span class="keyword">of</span> <code class="type">bool <a href="Pervasives.html#TYPEref">ref</a></code></code></td>
<td class="typefieldcomment" align="left" valign="top" ><code>(*</code></td><td class="typefieldcomment" align="left" valign="top" >Set the reference to true</td><td class="typefieldcomment" align="left" valign="bottom" ><code>*)</code></td>
</tr>
<tr>
<td align="left" valign="top" >
<code><span class="keyword">|</span></code></td>
<td align="left" valign="top" >
<code><span class="constructor">Clear</span> <span class="keyword">of</span> <code class="type">bool <a href="Pervasives.html#TYPEref">ref</a></code></code></td>
<td class="typefieldcomment" align="left" valign="top" ><code>(*</code></td><td class="typefieldcomment" align="left" valign="top" >Set the reference to false</td><td class="typefieldcomment" align="left" valign="bottom" ><code>*)</code></td>
</tr>
<tr>
<td align="left" valign="top" >
<code><span class="keyword">|</span></code></td>
<td align="left" valign="top" >
<code><span class="constructor">String</span> <span class="keyword">of</span> <code class="type">(string -> unit)</code></code></td>
<td class="typefieldcomment" align="left" valign="top" ><code>(*</code></td><td class="typefieldcomment" align="left" valign="top" >Call the function with a string argument</td><td class="typefieldcomment" align="left" valign="bottom" ><code>*)</code></td>
</tr>
<tr>
<td align="left" valign="top" >
<code><span class="keyword">|</span></code></td>
<td align="left" valign="top" >
<code><span class="constructor">Set_string</span> <span class="keyword">of</span> <code class="type">string <a href="Pervasives.html#TYPEref">ref</a></code></code></td>
<td class="typefieldcomment" align="left" valign="top" ><code>(*</code></td><td class="typefieldcomment" align="left" valign="top" >Set the reference to the string argument</td><td class="typefieldcomment" align="left" valign="bottom" ><code>*)</code></td>
</tr>
<tr>
<td align="left" valign="top" >
<code><span class="keyword">|</span></code></td>
<td align="left" valign="top" >
<code><span class="constructor">Int</span> <span class="keyword">of</span> <code class="type">(int -> unit)</code></code></td>
<td class="typefieldcomment" align="left" valign="top" ><code>(*</code></td><td class="typefieldcomment" align="left" valign="top" >Call the function with an int argument</td><td class="typefieldcomment" align="left" valign="bottom" ><code>*)</code></td>
</tr>
<tr>
<td align="left" valign="top" >
<code><span class="keyword">|</span></code></td>
<td align="left" valign="top" >
<code><span class="constructor">Set_int</span> <span class="keyword">of</span> <code class="type">int <a href="Pervasives.html#TYPEref">ref</a></code></code></td>
<td class="typefieldcomment" align="left" valign="top" ><code>(*</code></td><td class="typefieldcomment" align="left" valign="top" >Set the reference to the int argument</td><td class="typefieldcomment" align="left" valign="bottom" ><code>*)</code></td>
</tr>
<tr>
<td align="left" valign="top" >
<code><span class="keyword">|</span></code></td>
<td align="left" valign="top" >
<code><span class="constructor">Float</span> <span class="keyword">of</span> <code class="type">(float -> unit)</code></code></td>
<td class="typefieldcomment" align="left" valign="top" ><code>(*</code></td><td class="typefieldcomment" align="left" valign="top" >Call the function with a float argument</td><td class="typefieldcomment" align="left" valign="bottom" ><code>*)</code></td>
</tr>
<tr>
<td align="left" valign="top" >
<code><span class="keyword">|</span></code></td>
<td align="left" valign="top" >
<code><span class="constructor">Set_float</span> <span class="keyword">of</span> <code class="type">float <a href="Pervasives.html#TYPEref">ref</a></code></code></td>
<td class="typefieldcomment" align="left" valign="top" ><code>(*</code></td><td class="typefieldcomment" align="left" valign="top" >Set the reference to the float argument</td><td class="typefieldcomment" align="left" valign="bottom" ><code>*)</code></td>
</tr>
<tr>
<td align="left" valign="top" >
<code><span class="keyword">|</span></code></td>
<td align="left" valign="top" >
<code><span class="constructor">Tuple</span> <span class="keyword">of</span> <code class="type"><a href="Arg.html#TYPEspec">spec</a> list</code></code></td>
<td class="typefieldcomment" align="left" valign="top" ><code>(*</code></td><td class="typefieldcomment" align="left" valign="top" >Take several arguments according to the
spec list</td><td class="typefieldcomment" align="left" valign="bottom" ><code>*)</code></td>
</tr>
<tr>
<td align="left" valign="top" >
<code><span class="keyword">|</span></code></td>
<td align="left" valign="top" >
<code><span class="constructor">Symbol</span> <span class="keyword">of</span> <code class="type">string list * (string -> unit)</code></code></td>
<td class="typefieldcomment" align="left" valign="top" ><code>(*</code></td><td class="typefieldcomment" align="left" valign="top" >Take one of the symbols as argument and
call the function with the symbol</td><td class="typefieldcomment" align="left" valign="bottom" ><code>*)</code></td>
</tr>
<tr>
<td align="left" valign="top" >
<code><span class="keyword">|</span></code></td>
<td align="left" valign="top" >
<code><span class="constructor">Rest</span> <span class="keyword">of</span> <code class="type">(string -> unit)</code></code></td>
<td class="typefieldcomment" align="left" valign="top" ><code>(*</code></td><td class="typefieldcomment" align="left" valign="top" >Stop interpreting keywords and call the
function with each remaining argument</td><td class="typefieldcomment" align="left" valign="bottom" ><code>*)</code></td>
</tr></table>
<div class="info">
The concrete type describing the behavior associated
with a keyword.<br>
</div>
<pre><span class="keyword">type</span> <a name="TYPEkey"></a><code class="type"></code>key = <code class="type">string</code> </pre>
<pre><span class="keyword">type</span> <a name="TYPEdoc"></a><code class="type"></code>doc = <code class="type">string</code> </pre>
<pre><span class="keyword">type</span> <a name="TYPEusage_msg"></a><code class="type"></code>usage_msg = <code class="type">string</code> </pre>
<pre><span class="keyword">type</span> <a name="TYPEanon_fun"></a><code class="type"></code>anon_fun = <code class="type">string -> unit</code> </pre>
<pre><span class="keyword">val</span> <a name="VALparse"></a>parse : <code class="type">(<a href="Arg.html#TYPEkey">key</a> * <a href="Arg.html#TYPEspec">spec</a> * <a href="Arg.html#TYPEdoc">doc</a>) list -> <a href="Arg.html#TYPEanon_fun">anon_fun</a> -> <a href="Arg.html#TYPEusage_msg">usage_msg</a> -> unit</code></pre><div class="info">
<code class="code"><span class="constructor">Arg</span>.parse speclist anon_fun usage_msg</code> parses the command line.
<code class="code">speclist</code> is a list of triples <code class="code">(key, spec, doc)</code>.
<code class="code">key</code> is the option keyword, it must start with a <code class="code"><span class="string">'-'</span></code> character.
<code class="code">spec</code> gives the option type and the function to call when this option
is found on the command line.
<code class="code">doc</code> is a one-line description of this option.
<code class="code">anon_fun</code> is called on anonymous arguments.
The functions in <code class="code">spec</code> and <code class="code">anon_fun</code> are called in the same order
as their arguments appear on the command line.
<p>
If an error occurs, <code class="code"><span class="constructor">Arg</span>.parse</code> exits the program, after printing
an error message as follows:<ul>
<li> The reason for the error: unknown option, invalid or missing argument, etc.</li>
<li> <code class="code">usage_msg</code></li>
<li> The list of options, each followed by the corresponding <code class="code">doc</code> string.</li>
</ul>
For the user to be able to specify anonymous arguments starting with a
<code class="code">-</code>, include for example <code class="code">(<span class="string">"-"</span>, <span class="constructor">String</span> anon_fun, doc)</code> in <code class="code">speclist</code>.
<p>
By default, <code class="code">parse</code> recognizes two unit options, <code class="code">-help</code> and <code class="code">--help</code>,
which will display <code class="code">usage_msg</code> and the list of options, and exit
the program. You can override this behaviour by specifying your
own <code class="code">-help</code> and <code class="code">--help</code> options in <code class="code">speclist</code>.<br>
</div>
<pre><span class="keyword">val</span> <a name="VALparse_argv"></a>parse_argv : <code class="type">?current:int <a href="Pervasives.html#TYPEref">ref</a> -><br> string array -><br> (<a href="Arg.html#TYPEkey">key</a> * <a href="Arg.html#TYPEspec">spec</a> * <a href="Arg.html#TYPEdoc">doc</a>) list -> <a href="Arg.html#TYPEanon_fun">anon_fun</a> -> <a href="Arg.html#TYPEusage_msg">usage_msg</a> -> unit</code></pre><div class="info">
<code class="code"><span class="constructor">Arg</span>.parse_argv ~current args speclist anon_fun usage_msg</code> parses
the array <code class="code">args</code> as if it were the command line. It uses and updates
the value of <code class="code">~current</code> (if given), or <code class="code"><span class="constructor">Arg</span>.current</code>. You must set
it before calling <code class="code">parse_argv</code>. The initial value of <code class="code">current</code>
is the index of the program name (argument 0) in the array.
If an error occurs, <code class="code"><span class="constructor">Arg</span>.parse_argv</code> raises <code class="code"><span class="constructor">Arg</span>.<span class="constructor">Bad</span></code> with
the error message as argument. If option <code class="code">-help</code> or <code class="code">--help</code> is
given, <code class="code"><span class="constructor">Arg</span>.parse_argv</code> raises <code class="code"><span class="constructor">Arg</span>.<span class="constructor">Help</span></code> with the help message
as argument.<br>
</div>
<pre><span class="keyword">exception</span> <a name="EXCEPTIONHelp"></a>Help <span class="keyword">of</span> <code class="type">string</code></pre>
<div class="info">
Raised by <code class="code"><span class="constructor">Arg</span>.parse_argv</code> when the user asks for help.<br>
</div>
<pre><span class="keyword">exception</span> <a name="EXCEPTIONBad"></a>Bad <span class="keyword">of</span> <code class="type">string</code></pre>
<div class="info">
Functions in <code class="code">spec</code> or <code class="code">anon_fun</code> can raise <code class="code"><span class="constructor">Arg</span>.<span class="constructor">Bad</span></code> with an error
message to reject invalid arguments.
<code class="code"><span class="constructor">Arg</span>.<span class="constructor">Bad</span></code> is also raised by <code class="code"><span class="constructor">Arg</span>.parse_argv</code> in case of an error.<br>
</div>
<pre><span class="keyword">val</span> <a name="VALusage"></a>usage : <code class="type">(<a href="Arg.html#TYPEkey">key</a> * <a href="Arg.html#TYPEspec">spec</a> * <a href="Arg.html#TYPEdoc">doc</a>) list -> <a href="Arg.html#TYPEusage_msg">usage_msg</a> -> unit</code></pre><div class="info">
<code class="code"><span class="constructor">Arg</span>.usage speclist usage_msg</code> prints an error message including
the list of valid options. This is the same message that
<a href="Arg.html#VALparse"><code class="code"><span class="constructor">Arg</span>.parse</code></a> prints in case of error.
<code class="code">speclist</code> and <code class="code">usage_msg</code> are the same as for <code class="code"><span class="constructor">Arg</span>.parse</code>.<br>
</div>
<pre><span class="keyword">val</span> <a name="VALalign"></a>align : <code class="type">(<a href="Arg.html#TYPEkey">key</a> * <a href="Arg.html#TYPEspec">spec</a> * <a href="Arg.html#TYPEdoc">doc</a>) list -> (<a href="Arg.html#TYPEkey">key</a> * <a href="Arg.html#TYPEspec">spec</a> * <a href="Arg.html#TYPEdoc">doc</a>) list</code></pre><div class="info">
Align the documentation strings by inserting spaces at the first
space, according to the length of the keyword. Use a
space as the first character in a doc string if you want to
align the whole string. The doc strings corresponding to
<code class="code"><span class="constructor">Symbol</span></code> arguments are not aligned.<br>
</div>
<pre><span class="keyword">val</span> <a name="VALcurrent"></a>current : <code class="type">int <a href="Pervasives.html#TYPEref">ref</a></code></pre><div class="info">
Position (in <a href="Sys.html#VALargv"><code class="code"><span class="constructor">Sys</span>.argv</code></a>) of the argument being processed. You can
change this value, e.g. to force <a href="Arg.html#VALparse"><code class="code"><span class="constructor">Arg</span>.parse</code></a> to skip some arguments.
<a href="Arg.html#VALparse"><code class="code"><span class="constructor">Arg</span>.parse</code></a> uses the initial value of <a href="Arg.html#VALcurrent"><code class="code"><span class="constructor">Arg</span>.current</code></a> as the index of
argument 0 (the program name) and starts parsing arguments
at the next element.<br>
</div>
</body></html>
|