Stdlib.Arg(3) OCaml library Stdlib.Arg(3)

# [NAME](#NAME)

Stdlib.Arg - no description

# [Module](#Module)

Module Stdlib.Arg

# [Documentation](#Documentation)

Module **Arg**  
: **(module Stdlib\_\_Arg)**

*type spec* =  
| Unit **of** **(unit -&gt; unit)**  
(* Call the function with unit argument  
\*)  
| Bool **of** **(bool -&gt; unit)**  
(* Call the function with a bool argument  
\*)  
| Set **of** **bool ref**  
(* Set the reference to true  
\*)  
| Clear **of** **bool ref**  
(* Set the reference to false  
\*)  
| String **of** **(string -&gt; unit)**  
(* Call the function with a string argument  
\*)  
| Set\_string **of** **string ref**  
(* Set the reference to the string argument  
\*)  
| Int **of** **(int -&gt; unit)**  
(* Call the function with an int argument  
\*)  
| Set\_int **of** **int ref**  
(* Set the reference to the int argument  
\*)  
| Float **of** **(float -&gt; unit)**  
(* Call the function with a float argument  
\*)  
| Set\_float **of** **float ref**  
(* Set the reference to the float argument  
\*)  
| Tuple **of** **spec list**  
(* Take several arguments according to the spec list  
\*)  
| Symbol **of** **string list * (string -&gt; unit)**  
(* Take one of the symbols as argument and call the function with the symbol  
\*)  
| Rest **of** **(string -&gt; unit)**  
(* Stop interpreting keywords and call the function with each remaining argument  
\*)  
| Rest\_all **of** **(string list -&gt; unit)**  
(* Stop interpreting keywords and call the function with all remaining arguments  
\*)  
| Expand **of** **(string -&gt; string array)**  
(* If the remaining arguments to process are of the form **\["-foo"; "arg"] @ rest** where "foo" is registered as **Expand f** , then the arguments **f "arg" @ rest** are processed. Only allowed in **parse\_and\_expand\_argv\_dynamic** .  
\*)

The concrete type describing the behavior associated with a keyword.

*type key* = **string**

*type doc* = **string**

*type usage\_msg* = **string**

*type anon\_fun* = **string -&gt; unit**

*val parse* : **(key * spec * doc) list -&gt; anon\_fun -&gt; usage\_msg -&gt; unit**

**Arg.parse speclist anon\_fun usage\_msg** parses the command line. **speclist** is a list of triples **(key, spec, doc)** . **key** is the option keyword, it must start with a **'-'** character. **spec** gives the option type and the function to call when this option is found on the command line. **doc** is a one-line description of this option. **anon\_fun** is called on anonymous arguments. The functions in **spec** and **anon\_fun** are called in the same order as their arguments appear on the command line.

If an error occurs, **Arg.parse** exits the program, after printing to standard error an error message as follows:

\- The reason for the error: unknown option, invalid or missing argument, etc.

\- **usage\_msg**

\- The list of options, each followed by the corresponding **doc** string. Beware: options that have an empty **doc** string will not be included in the list.

For the user to be able to specify anonymous arguments starting with a **-** , include for example **("-", String anon\_fun, doc)** in **speclist** .

By default, **parse** recognizes two unit options, **-help** and **--help** , which will print to standard output **usage\_msg** and the list of options, and exit the program. You can override this behaviour by specifying your own **-help** and **--help** options in **speclist** .

*val parse\_dynamic* : **(key * spec * doc) list ref -&gt;** **anon\_fun -&gt; usage\_msg -&gt; unit**

Same as **Arg.parse** , except that the **speclist** argument is a reference and may be updated during the parsing. A typical use for this feature is to parse command lines of the form:

\- command subcommand **options** where the list of options depends on the value of the subcommand argument.

**Since** 4.01

*val parse\_argv* : **?current:int ref -&gt;** **string array -&gt;** **(key * spec * doc) list -&gt; anon\_fun -&gt; usage\_msg -&gt; unit**

**Arg.parse\_argv ~current args speclist anon\_fun usage\_msg** parses the array **args** as if it were the command line. It uses and updates the value of **~current** (if given), or **Arg.current** . You must set it before calling **parse\_argv** . The initial value of **current** is the index of the program name (argument 0) in the array. If an error occurs, **Arg.parse\_argv** raises **Arg.Bad** with the error message as argument. If option **-help** or **--help** is given, **Arg.parse\_argv** raises **Arg.Help** with the help message as argument.

*val parse\_argv\_dynamic* : **?current:int ref -&gt;** **string array -&gt;** **(key * spec * doc) list ref -&gt;** **anon\_fun -&gt; string -&gt; unit**

Same as **Arg.parse\_argv** , except that the **speclist** argument is a reference and may be updated during the parsing. See **Arg.parse\_dynamic** .

**Since** 4.01

*val parse\_and\_expand\_argv\_dynamic* : **int ref -&gt;** **string array ref -&gt;** **(key * spec * doc) list ref -&gt;** **anon\_fun -&gt; string -&gt; unit**

Same as **Arg.parse\_argv\_dynamic** , except that the **argv** argument is a reference and may be updated during the parsing of **Expand** arguments. See **Arg.parse\_argv\_dynamic** .

**Since** 4.05

*val parse\_expand* : **(key * spec * doc) list -&gt; anon\_fun -&gt; usage\_msg -&gt; unit**

Same as **Arg.parse** , except that the **Expand** arguments are allowed and the **Arg.current** reference is not updated.

**Since** 4.05

*exception Help* **of** **string**

Raised by **Arg.parse\_argv** when the user asks for help.

*exception Bad* **of** **string**

Functions in **spec** or **anon\_fun** can raise **Arg.Bad** with an error message to reject invalid arguments. **Arg.Bad** is also raised by **Arg.parse\_argv** in case of an error.

*val usage* : **(key * spec * doc) list -&gt; usage\_msg -&gt; unit**

**Arg.usage speclist usage\_msg** prints to standard error an error message that includes the list of valid options. This is the same message that **Arg.parse** prints in case of error. **speclist** and **usage\_msg** are the same as for **Arg.parse** .

*val usage\_string* : **(key * spec * doc) list -&gt; usage\_msg -&gt; string**

Returns the message that would have been printed by **Arg.usage** , if provided with the same parameters.

*val align* : **?limit:int -&gt;** **(key * spec * doc) list -&gt; (key * spec * doc) list**

Align the documentation strings by inserting spaces at the first alignment separator (tab or, if tab is not found, space), according to the length of the keyword. Use a alignment separator as the first character in a doc string if you want to align the whole string. The doc strings corresponding to **Symbol** arguments are aligned on the next line.

*val current* : **int ref**

Position (in **Sys.argv** ) of the argument being processed. You can change this value, e.g. to force **Arg.parse** to skip some arguments. **Arg.parse** uses the initial value of **Arg.current** as the index of argument 0 (the program name) and starts parsing arguments at the next element.

*val read\_arg* : **string -&gt; string array**

**Arg.read\_arg file** reads newline-terminated command line arguments from file **file** .

**Since** 4.05

*val read\_arg0* : **string -&gt; string array**

Identical to **Arg.read\_arg** but assumes null character terminated command line arguments.

**Since** 4.05

*val write\_arg* : **string -&gt; string array -&gt; unit**

**Arg.write\_arg file args** writes the arguments **args** newline-terminated into the file **file** . If any of the arguments in **args** contains a newline, use **Arg.write\_arg0** instead.

**Since** 4.05

*val write\_arg0* : **string -&gt; string array -&gt; unit**

Identical to **Arg.write\_arg** but uses the null character for terminator instead of newline.

**Since** 4.05

2026-10-23 OCamldoc

---

Document: Stdlib.Arg(3o) from ocaml 5.5.2-r0 (Chainguard OS)
Source: https://github.com/ocaml/ocaml
