1Arg(3)                           OCaml library                          Arg(3)
2
3
4

NAME

6       Arg - Parsing of command line arguments.
7

Module

9       Module   Arg
10

Documentation

12       Module Arg
13        : sig end
14
15
16       Parsing of command line arguments.
17
18       This  module  provides  a  general mechanism for extracting options and
19       arguments from the command line to the program.
20
21       Syntax of command lines: A keyword is a character string starting  with
22       a  -  .   An option is a keyword alone or followed by an argument.  The
23       types of keywords are: Unit , Bool , Set , Clear , String ,  Set_string
24       , Int , Set_int , Float , Set_float , Tuple , Symbol , and Rest .  Unit
25       , Set and Clear keywords take no argument. A  Rest  keyword  takes  the
26       remaining  of the command line as arguments.  Every other keyword takes
27       the following word on the command line as argument.  Arguments not pre‐
28       ceded by a keyword are called anonymous arguments.
29
30       Examples ( cmd is assumed to be the command name):
31
32       - cmd -flag (a unit option)
33
34       - cmd -int 1 (an int option with argument 1 )
35
36       - cmd -string foobar (a string option with argument foobar )
37
38       - cmd -float 12.34 (a float option with argument 12.34 )
39
40       - cmd a b c (three anonymous arguments: a , b , and c )
41
42       -  cmd  a  b -- c d (two anonymous arguments and a rest option with two
43       arguments)
44
45
46
47
48
49
50
51       type spec =
52        | Unit of (unit -> unit)  (* Call the function with unit argument *)
53        | Bool of (bool -> unit)  (* Call the function with a bool argument *)
54        | Set of bool Pervasives.ref  (* Set the reference to true *)
55        | Clear of bool Pervasives.ref  (* Set the reference to false *)
56        | String of (string -> unit)  (* Call the function with a string argu‐
57       ment *)
58        |  Set_string  of  string  Pervasives.ref  (* Set the reference to the
59       string argument *)
60        | Int of (int -> unit)  (* Call the function with an int argument *)
61        | Set_int of int Pervasives.ref  (* Set the reference to the int argu‐
62       ment *)
63        | Float of (float -> unit)  (* Call the function with a float argument
64       *)
65        | Set_float of float Pervasives.ref  (* Set the reference to the float
66       argument *)
67        |  Tuple of spec list  (* Take several arguments according to the spec
68       list *)
69        | Symbol of string list * (string -> unit)  (* Take one of the symbols
70       as argument and call the function with the symbol *)
71        | Rest of (string -> unit)  (* Stop interpreting keywords and call the
72       function with each remaining argument *)
73
74
75       The concrete type describing the behavior associated with a keyword.
76
77
78
79       type key = string
80
81
82
83
84       type doc = string
85
86
87
88
89       type usage_msg = string
90
91
92
93
94       type anon_fun = string -> unit
95
96
97
98
99
100       val parse : (key * spec * doc) list -> anon_fun -> usage_msg -> unit
101
102
103       Arg.parse  speclist  anon_fun  usage_msg  parses  the   command   line.
104       speclist  is  a  list  of triples (key, spec, doc) .  key is the option
105       keyword, it must start with a '-' character.   spec  gives  the  option
106       type  and the function to call when this option is found on the command
107       line.  doc is a one-line  description  of  this  option.   anon_fun  is
108       called  on anonymous arguments.  The functions in spec and anon_fun are
109       called in the same order as their arguments appear on the command line.
110
111       If an error occurs, Arg.parse exits  the  program,  after  printing  an
112       error message as follows:
113
114       -   The  reason for the error: unknown option, invalid or missing argu‐
115       ment, etc.
116
117       - usage_msg
118
119
120       -  The list of options, each followed by the corresponding doc string.
121
122       For the user to be able to specify anonymous arguments starting with  a
123       - , include for example ("-", String anon_fun, doc) in speclist .
124
125       By default, parse recognizes two unit options, -help and --help , which
126       will display usage_msg and the list of options, and exit  the  program.
127       You can override this behaviour by specifying your own -help and --help
128       options in speclist .
129
130
131
132
133       val parse_argv : ?current:int Pervasives.ref -> string array -> (key  *
134       spec * doc) list -> anon_fun -> usage_msg -> unit
135
136
137       Arg.parse_argv  ~current  args  speclist  anon_fun usage_msg parses the
138       array args as if it were the command line.  It  uses  and  updates  the
139       value  of ~current (if given), or Arg.current .  You must set it before
140       calling parse_argv .  The initial value of current is the index of  the
141       program   name  (argument  0)  in  the  array.   If  an  error  occurs,
142       Arg.parse_argv raises Arg.Bad with the error message as  argument.   If
143       option  -help  or  --help is given, Arg.parse_argv raises Arg.Help with
144       the help message as argument.
145
146
147
148
149       exception Help of string
150
151
152       Raised by Arg.parse_argv when the user asks for help.
153
154
155
156
157       exception Bad of string
158
159
160       Functions in spec or anon_fun can raise Arg.Bad with an  error  message
161       to  reject invalid arguments.  Arg.Bad is also raised by Arg.parse_argv
162       in case of an error.
163
164
165
166
167       val usage : (key * spec * doc) list -> usage_msg -> unit
168
169
170       Arg.usage speclist usage_msg prints an error message including the list
171       of  valid  options.   This is the same message that Arg.parse prints in
172       case of error.  speclist and usage_msg are the same as for Arg.parse .
173
174
175
176
177       val align : (key * spec * doc) list -> (key * spec * doc) list
178
179       Align the documentation strings by inserting spaces at the first space,
180       according to the length of the keyword.  Use a space as the first char‐
181       acter in a doc string if you want to align the whole string.   The  doc
182       strings corresponding to Symbol arguments are aligned on the next line.
183
184
185
186
187       val current : int Pervasives.ref
188
189       Position  (in  Sys.argv  )  of  the  argument being processed.  You can
190       change this value, e.g. to force  Arg.parse  to  skip  some  arguments.
191       Arg.parse  uses  the initial value of Arg.current as the index of argu‐
192       ment 0 (the program name) and starts parsing arguments at the next ele‐
193       ment.
194
195
196
197
198
199
200OCamldoc                          2017-03-22                            Arg(3)
Impressum