<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <link rel="stylesheet" href="style.css" type="text/css"> <meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type"> <link rel="Start" href="index.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="Getopt" rel="Chapter" href="Getopt.html"><link title="Layout of the command line" rel="Section" href="#1_Layoutofthecommandline"> <link title="Command line specification" rel="Section" href="#1_Commandlinespecification"> <link title="Parsing the command line" rel="Section" href="#1_Parsingthecommandline"> <link title="Useful actions and handlers" rel="Section" href="#1_Usefulactionsandhandlers"> <title>Getopt</title> </head> <body> <div class="navbar"> <a href="index.html">Up</a> </div> <center><h1>Module <a href="type_Getopt.html">Getopt</a></h1></center> <br> <pre><span class="keyword">module</span> Getopt: <code class="code">sig</code> <a href="Getopt.html">..</a> <code class="code">end</code></pre>Module <code class="code">Getopt</code>: parsing of command line arguments. <p> Copyright (C) 2000-2004 Alain Frisch. Distributed under the terms of the MIT license. <p> email: <a href="Alain.Frisch@ens.fr">Alain Frisch@ens.fr</a> <p> web: <a href="http://www.eleves.ens.fr/home/frisch">http://www.eleves.ens.fr/home/frisch</a> <p> This module provides a general mechanism for extracting options and arguments from the command line to the program. It is an alternative to the module <code class="code">Arg</code> from the standard OCaml distribution. <p> The syntax is close to GNU getopt and getop_long (<code class="code">man 3 getopt</code>).<br> <hr width="100%"> <br> <a name="1_Layoutofthecommandline"></a> <h1>Layout of the command line</h1> There are two types of argument on the command line: options and anonymous arguments. Options may have two forms: a short one introduced by a single dash character (-) and a long one introduced by a double dash (--). <p> Options may have an argument attached. For the long form, the syntax is "--option=argument". For the short form, there are two possible syntaxes: "-o argument" (argument doesn't start with a dash) and "-oargument" <p> Short options that refuse arguments may be concatenated, as in "-opq". <p> The special argument -- interrupts the parsing of options: all the remaining arguments are arguments even they start with a dash. <p> <a name="1_Commandlinespecification"></a> <h1>Command line specification</h1> A specification lists the possible options and describe what to do when they are found; it also gives the action for anonymous arguments and for the special option - (a single dash alone).<br> <pre><span class="keyword">type</span> <a name="TYPEopt"></a><code class="type"></code>opt = <code class="type">char * string * (unit -> unit) option * (string -> unit) option</code> </pre> <br> The specification for a single option is a tuple <code class="code">(short_form, long_form, action, handler)</code> where:<ul> <li><code class="code">short_form</code> is the character for the short form of the option without the leading - (or <code class="code">noshort='\000'</code> if the option does not have a short form)</li> </ul> <ul> <li><code class="code">long_form</code> is the string for the long form of the option without the leading -- (or <code class="code">nolong=""</code> if no long form)</li> </ul> <ul> <li><code class="code">(action : (unit -> unit) option)</code> gives the action to be executed when the option is found without an argument</li> </ul> <ul> <li><code class="code">(handler : (string -> unit) option)</code> specifies how to handle the argument when the option is found with the argument</li> </ul> According to the pair <code class="code">(action, handler)</code>, the corresponding option may, must or mustn't have an argument : <p> <ul> <li><code class="code">(Some _, Some _)</code> : the option may have an argument; the short form can't be concatenated with other options (even if the user does not want to provide an argument). The behaviour (handler/action) is determined by the presence of the argument.</li> </ul> <ul> <li><code class="code">(Some _, None)</code> : the option must not have an argument; the short form, if it exists, may be concatenated</li> </ul> <ul> <li><code class="code">(None, Some _)</code> : the option must have an argument; the short form can't be concatenated</li> </ul> <ul> <li><code class="code">(None, None)</code> : not allowed</li> </ul> <br> <pre><span class="keyword">val</span> <a name="VALnoshort"></a>noshort : <code class="type">char</code></pre><div class="info"> <code class="code">noshort='\000'</code> can be used when an option has no short form<br> </div> <pre><span class="keyword">val</span> <a name="VALnolong"></a>nolong : <code class="type">string</code></pre><div class="info"> <code class="code">nolong=""</code> can be used when an option has no long form<br> </div> <pre><span class="keyword">exception</span> <a name="EXCEPTIONError"></a>Error <span class="keyword">of</span> <code class="type">string</code></pre> <div class="info"> Signals error on the command line<br> </div> <br> <a name="1_Parsingthecommandline"></a> <h1>Parsing the command line</h1><br> <pre><span class="keyword">val</span> <a name="VALparse"></a>parse : <code class="type"><a href="Getopt.html#TYPEopt">opt</a> list -> (string -> unit) -> string array -> int -> int -> unit</code></pre><div class="info"> <code class="code">parse opts others args first last</code> parse the arguments <code class="code">args.(first)</code>, <code class="code">arg.(first+1)</code> ... <code class="code">args.(last)</code>. <code class="code">others</code> is called on anonymous arguments (and the special - argument); <code class="code">opts</code> is a list of option specifications (there must be no ambiguities).<br> <b>Raises</b> <code>Error</code> : Unknown options, unexpected argument, ...<br> </div> <pre><span class="keyword">val</span> <a name="VALparse_cmdline"></a>parse_cmdline : <code class="type"><a href="Getopt.html#TYPEopt">opt</a> list -> (string -> unit) -> unit</code></pre><div class="info"> Parse the command line in <code class="code">Sys.argv</code> using <code class="code">parse</code>.<br> </div> <br> <a name="1_Usefulactionsandhandlers"></a> <h1>Useful actions and handlers</h1><br> <pre><span class="keyword">val</span> <a name="VALset"></a>set : <code class="type">'a Pervasives.ref -> 'a -> (unit -> unit) option</code></pre><div class="info"> <b>Returns</b> an action that gives a reference a given value<br> </div> <pre><span class="keyword">val</span> <a name="VALincr"></a>incr : <code class="type">int Pervasives.ref -> (unit -> unit) option</code></pre><div class="info"> <b>Returns</b> an action that increments an <code class="code">int</code> reference<br> </div> <pre><span class="keyword">val</span> <a name="VALappend"></a>append : <code class="type">string list Pervasives.ref -> (string -> unit) option</code></pre><div class="info"> <b>Returns</b> an handler that appends the argument to the end of a <code class="code">string list</code> reference<br> </div> <pre><span class="keyword">val</span> <a name="VALatmost_once"></a>atmost_once : <code class="type">string Pervasives.ref -> exn -> (string -> unit) option</code></pre><div class="info"> <b>Returns</b> an handler that stores the argument in a <code class="code">string</code> reference if it is empty, raises an exception otherwise<br> </div> </body></html>