File: getopt.mli

package info (click to toggle)
ocaml-getopt 0.0.20040811-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 68 kB
  • ctags: 36
  • sloc: ml: 116; makefile: 68
file content (120 lines) | stat: -rw-r--r-- 4,304 bytes parent folder | download | duplicates (6)
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
(** Module [Getopt]: parsing of command line arguments.

Copyright (C) 2000-2004 Alain Frisch. Distributed under the terms of the MIT
license.

email: {{:Alain.Frisch\@ens.fr}Alain Frisch\@ens.fr}

web:   {{:http://www.eleves.ens.fr/home/frisch}http://www.eleves.ens.fr/home/frisch}

   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 [Arg] from the standard OCaml distribution.

   The syntax is close to GNU getopt and getop_long ([man 3 getopt]).
*)

(**
{1 Layout of the command line}
   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 (--).

   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"

   Short options that refuse arguments may be concatenated, as in
   "-opq".

   The special argument -- interrupts the parsing of options: all the
   remaining arguments are arguments even they start with a dash.

{1 Command line specification}
   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).
*)
   
type opt = char * string * ((unit -> unit) option) * ((string -> unit) option)

(**
   The specification for a single option is a tuple
   [(short_form, long_form, action, handler)]
   where:
   - [short_form] is the character for the short form of the option
     without the leading -
     (or [noshort='\000'] if the option does not have a short form)

   - [long_form] is the string for the long form of the option
     without the leading --
     (or [nolong=""] if no long form)

   - [(action : (unit -> unit) option)] gives the action to be executed
     when the option is found without an argument

   - [(handler : (string -> unit) option)] specifies how to handle the
     argument when the option is found with the argument


   According to the pair [(action, handler)], the corresponding option
   may, must or mustn't have an argument :

   - [(Some _, Some _)] : 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.

   - [(Some _, None)] : the option must not have an argument; the short form, if
     it exists, may be concatenated

   - [(None, Some _)] : the option must have an argument; the short form can't
     be concatenated

   - [(None, None)] : not allowed

*)

(** [noshort='\000'] can be used when an option has no short form *)
val noshort : char

(** [nolong=""] can be used when an option has no long form *)
val nolong  : string

(** Signals error on the command line *)
exception Error of string

(** {1 Parsing the command line} *)

val parse : opt list -> (string -> unit) -> string array -> int -> int -> unit
(**
    [parse opts others args first last] parse the arguments
    [args.(first)], [arg.(first+1)] ... [args.(last)].
    [others] is called on anonymous arguments (and the special - argument);
    [opts] is a list of option specifications (there must be no ambiguities).

  @raise Error : Unknown options, unexpected argument, ...
*)

val parse_cmdline : opt list -> (string -> unit) -> unit
(**
    Parse the command line in [Sys.argv] using [parse].
*)


(** {1 Useful actions and handlers} *)

val set : 'a ref -> 'a -> ((unit -> unit) option)
(** @return an action that gives a reference a given value *)

val incr : int ref -> ((unit -> unit) option)
(** @return an action that increments an [int] reference *)

val append : string list ref -> ((string -> unit) option)
(** @return an handler that appends the argument to the end of a [string list]
   reference *)

val atmost_once : string ref -> exn -> ((string -> unit) option)
(** @return an handler that stores the argument in a [string] reference if
    it is empty, raises an exception otherwise *)