Module Cmdliner.Arg
Terms for command line arguments.
This module provides functions to define terms that evaluate to the arguments provided on the command line.
Basic constraints, like the argument type or repeatability, are specified by defining a value of type t
. Further constraints can be specified during the conversion to a term.
Argument converters
An argument converter transforms a string argument of the command line to an OCaml value. Predefined converters are provided for many types of the standard library.
type 'a parser
= string -> [ `Ok of 'a | `Error of string ]
The type for argument parsers.
- deprecated
Use a parser with
('a, [ `Msg of string]) result
results andconv
.
type 'a printer
= Stdlib.Format.formatter -> 'a -> unit
The type for converted argument printers.
type 'a conv
= 'a parser * 'a printer
The type for argument converters.
WARNING. This type will become abstract in the next major version of cmdliner, use
conv
orpconv
to construct values of this type.
val conv : ?docv:string -> ((string -> ('a, [ `Msg of string ]) Stdlib.result) * 'a printer) -> 'a conv
converter ~docv (parse, print)
is an argument converter parsing values withparse
and printing them withprint
.docv
is a documentation meta-variable used in the documentation to stand for the argument value, defaults to"VALUE"
.
val pconv : ?docv:string -> ('a parser * 'a printer) -> 'a conv
pconv
is likeconverter
, but uses a deprecatedparser
signature.
val conv_parser : 'a conv -> string -> ('a, [ `Msg of string ]) Stdlib.result
conv_parser c
'sc
's parser.
val conv_docv : 'a conv -> string
conv_docv c
isc
's documentation meta-variable.WARNING. Currently always returns
"VALUE"
in the future will return the value given toconv
orpconv
.
val parser_of_kind_of_string : kind:string -> (string -> 'a option) -> string -> ('a, [ `Msg of string ]) Stdlib.result
parser_of_kind_of_string ~kind kind_of_string
is an argument parser using thekind_of_string
function for parsing andkind
to report errors (e.g. could be"an integer"
for anint
parser.).
Arguments and their information
Argument information defines the man page information of an argument and, for optional arguments, its names. An environment variable can also be specified to read the argument value from if the argument is absent from the command line and the variable is defined.
type env
= Term.env_info
The type for environment variables and their documentation.
val env_var : ?docs:string -> ?doc:string -> string -> env
env_var docs doc var
is an environment variablesvar
.doc
is the man page information of the environment variable, the documentation markup language with the variables mentioned ininfo
be used; it defaults to"See option $(opt)."
.docs
is the title of the man page section in which the environment variable will be listed, it defaults toManpage.s_environment
.
val info : ?docs:string -> ?docv:string -> ?doc:string -> ?env:env -> string list -> info
info docs docv doc env names
defines information for an argument.names
defines the names under which an optional argument can be referred to. Strings of length1
("c"
) define short option names ("-c"
), longer strings ("count"
) define long option names ("--count"
).names
must be empty for positional arguments.env
defines the name of an environment variable which is looked up for defining the argument if it is absent from the command line. See environment variables for details.doc
is the man page information of the argument. The documentation language can be used and the following variables are recognized:"$(docv)"
the value ofdocv
(see below)."$(opt)"
, one of the options ofnames
, preference is given to a long one."$(env)"
, the environment var specified byenv
(if any).
These functions can help with formatting argument values.
docv
is for positional and non-flag optional arguments. It is a variable name used in the man page to stand for their value.docs
is the title of the man page section in which the argument will be listed. For optional arguments this defaults toManpage.s_options
. For positional arguments this defaults toManpage.s_arguments
. However a positional argument is only listed if it has both adoc
anddocv
specified.
Optional arguments
The information of an optional argument must have at least one name or Invalid_argument
is raised.
val flag : info -> bool t
flag i
is abool
argument defined by an optional flag that may appear at most once on the command line under one of the names specified byi
. The argument holdstrue
if the flag is present on the command line andfalse
otherwise.
val flag_all : info -> bool list t
flag_all
is likeflag
except the flag may appear more than once. The argument holds a list that contains onetrue
value per occurrence of the flag. It holds the empty list if the flag is absent from the command line.
val vflag : 'a -> ('a * info) list -> 'a t
vflag v [v
0,i
0;...]
is an'a
argument defined by an optional flag that may appear at most once on the command line under one of the names specified in thei
k values. The argument holdsv
if the flag is absent from the command line and the valuev
k if the name under which it appears is ini
k.Note. Environment variable lookup is unsupported for for these arguments.
val vflag_all : 'a list -> ('a * info) list -> 'a list t
vflag_all v l
is likevflag
except the flag may appear more than once. The argument holds the listv
if the flag is absent from the command line. Otherwise it holds a list that contains one corresponding value per occurrence of the flag, in the order found on the command line.Note. Environment variable lookup is unsupported for for these arguments.
val opt : ?vopt:'a -> 'a conv -> 'a -> info -> 'a t
opt vopt c v i
is an'a
argument defined by the value of an optional argument that may appear at most once on the command line under one of the names specified byi
. The argument holdsv
if the option is absent from the command line. Otherwise it has the value of the option as converted byc
.If
vopt
is provided the value of the optional argument is itself optional, taking the valuevopt
if unspecified on the command line.
val opt_all : ?vopt:'a -> 'a conv -> 'a list -> info -> 'a list t
opt_all vopt c v i
is likeopt
except the optional argument may appear more than once. The argument holds a list that contains one value per occurrence of the flag in the order found on the command line. It holds the listv
if the flag is absent from the command line.
Positional arguments
The information of a positional argument must have no name or Invalid_argument
is raised. Positional arguments indexing is zero-based.
Warning. The following combinators allow to specify and extract a given positional argument with more than one term. This should not be done as it will likely confuse end users and documentation generation. These over-specifications may be prevented by raising Invalid_argument
in the future. But for now it is the client's duty to make sure this doesn't happen.
val pos : ?rev:bool -> int -> 'a conv -> 'a -> info -> 'a t
pos rev n c v i
is an'a
argument defined by then
th positional argument of the command line as converted byc
. If the positional argument is absent from the command line the argument isv
.If
rev
istrue
(defaults tofalse
), the computed position ismax-n
wheremax
is the position of the last positional argument present on the command line.
val pos_all : 'a conv -> 'a list -> info -> 'a list t
pos_all c v i
is an'a list
argument that holds all the positional arguments of the command line as converted byc
orv
if there are none.
val pos_left : ?rev:bool -> int -> 'a conv -> 'a list -> info -> 'a list t
pos_left rev n c v i
is an'a list
argument that holds all the positional arguments as converted byc
found on the left of then
th positional argument orv
if there are none.If
rev
istrue
(defaults tofalse
), the computed position ismax-n
wheremax
is the position of the last positional argument present on the command line.
Arguments as terms
val required : 'a option t -> 'a Term.t
required a
is a term that fails ifa
's value isNone
and evaluates to the value ofSome
otherwise. Use this for required positional arguments (it can also be used for defining required optional arguments, but from a user interface perspective this shouldn't be done, it is a contradiction in terms).
Predefined arguments
val man_format : Manpage.format Term.t
man_format
is a term that defines a--man-format
option and evaluates to a value that can be used withManpage.print
.
Predefined converters
val bool : bool conv
bool
converts values withbool_of_string
.
val char : char conv
char
converts values by ensuring the argument has a single char.
val int : int conv
int
converts values withint_of_string
.
val nativeint : nativeint conv
nativeint
converts values withNativeint
.of_string.
val int32 : int32 conv
int32
converts values withInt32
.of_string.
val int64 : int64 conv
int64
converts values withInt64
.of_string.
val float : float conv
float
converts values withfloat_of_string
.
val string : string conv
string
converts values with the identity function.
val enum : (string * 'a) list -> 'a conv
enum l p
converts values such that unambiguous prefixes of string names inl
map to the corresponding value of type'a
.Warning. The type
'a
must be comparable withPervasives
.compare.- raises Invalid_argument
if
l
is empty.
val file : string conv
file
converts a value with the identity function and checks withSys
.file_exists that a file with that name exists.
val dir : string conv
dir
converts a value with the identity function and checks withSys
.file_exists andSys
.is_directory that a directory with that name exists.
val non_dir_file : string conv
non_dir_file
converts a value with the identity function and checks withSys
.file_exists andSys
.is_directory that a non directory file with that name exists.
val list : ?sep:char -> 'a conv -> 'a list conv
list sep c
splits the argument at eachsep
(defaults to','
) character and converts each substrings withc
.
val array : ?sep:char -> 'a conv -> 'a array conv
array sep c
splits the argument at eachsep
(defaults to','
) character and converts each substring withc
.
val pair : ?sep:char -> 'a conv -> 'b conv -> ('a * 'b) conv
pair sep c0 c1
splits the argument at the firstsep
character (defaults to','
) and respectively converts the substrings withc0
andc1
.
Documentation formatting helpers
val doc_alts : ?quoted:bool -> string list -> string
doc_alts alts
documents the alternative tokensalts
according the number of alternatives. Ifquoted
istrue
(default) the tokens are quoted. The resulting string can be used in sentences of the form"$(docv) must be %s"
.- raises Invalid_argument
if
alts
is the empty string.