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]) resultresults andconv.
type 'a printer= Stdlib.Format.formatter -> 'a -> unitThe type for converted argument printers.
type 'a conv= 'a parser * 'a printerThe type for argument converters.
WARNING. This type will become abstract in the next major version of cmdliner, use
convorpconvto construct values of this type.
val conv : ?docv:string -> ((string -> ('a, [ `Msg of string ]) Stdlib.result) * 'a printer) -> 'a convconverter ~docv (parse, print)is an argument converter parsing values withparseand printing them withprint.docvis 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 convpconvis likeconverter, but uses a deprecatedparsersignature.
val conv_parser : 'a conv -> string -> ('a, [ `Msg of string ]) Stdlib.resultconv_parser c'sc's parser.
val conv_docv : 'a conv -> stringconv_docv cisc's documentation meta-variable.WARNING. Currently always returns
"VALUE"in the future will return the value given toconvorpconv.
val parser_of_kind_of_string : kind:string -> (string -> 'a option) -> string -> ('a, [ `Msg of string ]) Stdlib.resultparser_of_kind_of_string ~kind kind_of_stringis an argument parser using thekind_of_stringfunction for parsing andkindto report errors (e.g. could be"an integer"for anintparser.).
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_infoThe type for environment variables and their documentation.
val env_var : ?docs:string -> ?doc:string -> string -> envenv_var docs doc varis an environment variablesvar.docis the man page information of the environment variable, the documentation markup language with the variables mentioned ininfobe used; it defaults to"See option $(opt).".docsis 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 -> infoinfo docs docv doc env namesdefines information for an argument.namesdefines 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").namesmust be empty for positional arguments.envdefines 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.docis 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.
docvis for positional and non-flag optional arguments. It is a variable name used in the man page to stand for their value.docsis 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 adocanddocvspecified.
Optional arguments
The information of an optional argument must have at least one name or Invalid_argument is raised.
val flag : info -> bool tflag iis aboolargument defined by an optional flag that may appear at most once on the command line under one of the names specified byi. The argument holdstrueif the flag is present on the command line andfalseotherwise.
val flag_all : info -> bool list tflag_allis likeflagexcept the flag may appear more than once. The argument holds a list that contains onetruevalue 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 tvflag v [v0,i0;...]is an'aargument defined by an optional flag that may appear at most once on the command line under one of the names specified in theik values. The argument holdsvif the flag is absent from the command line and the valuevk if the name under which it appears is inik.Note. Environment variable lookup is unsupported for for these arguments.
val vflag_all : 'a list -> ('a * info) list -> 'a list tvflag_all v lis likevflagexcept the flag may appear more than once. The argument holds the listvif 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 topt vopt c v iis an'aargument 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 holdsvif the option is absent from the command line. Otherwise it has the value of the option as converted byc.If
voptis provided the value of the optional argument is itself optional, taking the valuevoptif unspecified on the command line.
val opt_all : ?vopt:'a -> 'a conv -> 'a list -> info -> 'a list topt_all vopt c v iis likeoptexcept 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 listvif 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 tpos rev n c v iis an'aargument defined by thenth positional argument of the command line as converted byc. If the positional argument is absent from the command line the argument isv.If
revistrue(defaults tofalse), the computed position ismax-nwheremaxis the position of the last positional argument present on the command line.
val pos_all : 'a conv -> 'a list -> info -> 'a list tpos_all c v iis an'a listargument that holds all the positional arguments of the command line as converted bycorvif there are none.
val pos_left : ?rev:bool -> int -> 'a conv -> 'a list -> info -> 'a list tpos_left rev n c v iis an'a listargument that holds all the positional arguments as converted bycfound on the left of thenth positional argument orvif there are none.If
revistrue(defaults tofalse), the computed position ismax-nwheremaxis the position of the last positional argument present on the command line.
Arguments as terms
val required : 'a option t -> 'a Term.trequired ais a term that fails ifa's value isNoneand evaluates to the value ofSomeotherwise. 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.tman_formatis a term that defines a--man-formatoption and evaluates to a value that can be used withManpage.print.
Predefined converters
val bool : bool convboolconverts values withbool_of_string.
val char : char convcharconverts values by ensuring the argument has a single char.
val int : int convintconverts values withint_of_string.
val nativeint : nativeint convnativeintconverts values withNativeint.of_string.
val int32 : int32 convint32converts values withInt32.of_string.
val int64 : int64 convint64converts values withInt64.of_string.
val float : float convfloatconverts values withfloat_of_string.
val string : string convstringconverts values with the identity function.
val enum : (string * 'a) list -> 'a convenum l pconverts values such that unambiguous prefixes of string names inlmap to the corresponding value of type'a.Warning. The type
'amust be comparable withPervasives.compare.- raises Invalid_argument
if
lis empty.
val file : string convfileconverts a value with the identity function and checks withSys.file_exists that a file with that name exists.
val dir : string convdirconverts 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 convnon_dir_fileconverts 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 convlist sep csplits the argument at eachsep(defaults to',') character and converts each substrings withc.
val array : ?sep:char -> 'a conv -> 'a array convarray sep csplits the argument at eachsep(defaults to',') character and converts each substring withc.
val pair : ?sep:char -> 'a conv -> 'b conv -> ('a * 'b) convpair sep c0 c1splits the argument at the firstsepcharacter (defaults to',') and respectively converts the substrings withc0andc1.
Documentation formatting helpers
val doc_alts : ?quoted:bool -> string list -> stringdoc_alts altsdocuments the alternative tokensaltsaccording the number of alternatives. Ifquotedistrue(default) the tokens are quoted. The resulting string can be used in sentences of the form"$(docv) must be %s".- raises Invalid_argument
if
altsis the empty string.