Module Cmdliner.Term
Terms.
A term is evaluated by a program to produce a result, which can be turned into an exit status. A term made of terms referring to command line arguments implicitly defines a command line syntax.
Terms
val const : 'a -> 'a tconst vis a term that evaluates tov.
Interacting with Cmdliner's evaluation
type 'a ret=[|`Help of Manpage.format * string option|`Error of bool * string|`Ok of 'a]The type for command return values. See
ret.
val ret : 'a ret t -> 'a tret vis a term whose evaluation depends on the case to whichvevaluates. With :`Ok v, it evaluates tov.`Error (usage, e), the evaluation fails andCmdlinerprints the erroreand the term's usage ifusageistrue.`Help (format, name), the evaluation fails andCmdlinerprints the term's man page in the givenformat(or the man page for a specificnameterm in case of multiple term evaluation).
val term_result : ?usage:bool -> ('a, [ `Msg of string ]) Stdlib.result t -> 'a tterm_result ~usage tevaluates to`Ok viftevaluates toOk v`Error `Termwith the error messageeand usage shown according tousage(defaults tofalse), iftevaluates toError (`Msg e).
val cli_parse_result : ('a, [ `Msg of string ]) Stdlib.result t -> 'a tcli_parse_result tis a term that evaluates to:`Ok viftevaluates toOk v.`Error `Parsewith the error messageeiftevaluates toError (`Msg e).
val main_name : string tmain_nameis a term that evaluates to the "main" term's name.
val choice_names : string list tchoice_namesis a term that evaluates to the names of the terms to choose from.
Term information
Term information defines the name and man page of a term. For simple evaluation this is the name of the program and its man page. For multiple term evaluation, this is the name of a command and its man page.
val exit_info : ?docs:string -> ?doc:string -> ?max:int -> int -> exit_infoexit_info ~docs ~doc min ~maxdescribe the range of exit statuses frommintomax(defaults tomin).docis the man page information for the statuses, defaults to"undocumented".docsis the title of the man page section in which the statuses will be listed, it defaults toManpage.s_exit_status.In
docthe documentation markup language can be used with following variables:$(status), the value ofmin.$(status_max), the value ofmax.- The variables mentioned in
info
val default_exits : exit_info listdefault_exitsis information for exit statusexit_status_successadded todefault_error_exits.
val default_error_exits : exit_info listdefault_error_exitsis information for exit statusesexit_status_cli_errorandexit_status_internal_error.
val env_info : ?docs:string -> ?doc:string -> string -> env_infoenv_info ~docs ~doc vardescribes an environment variablevar.docis the man page information of the environment variable, defaults to"undocumented".docsis the title of the man page section in which the environment variable will be listed, it defaults toManpage.s_environment.In
docthe documentation markup language can be used with following variables:$(env), the value ofvar.- The variables mentioned in
info
val info : ?man_xrefs:Manpage.xref list -> ?man:Manpage.block list -> ?envs:env_info list -> ?exits:exit_info list -> ?sdocs:string -> ?docs:string -> ?doc:string -> ?version:string -> string -> infoinfo sdocs man docs doc version nameis a term information such that:nameis the name of the program or the command.versionis the version string of the program, ignored for commands.docis a one line description of the program or command used for theNAMEsection of the term's man page. For commands this description is also used in the list of commands of the main term's man page.docs, only for commands, the title of the section of the main term's man page where it should be listed (defaults toManpage.s_commands).sdocsdefines the title of the section in which the standard--helpand--versionarguments are listed (defaults toManpage.s_options).exitsis a list of exit statuses that the term evaluation may produce.envsis a list of environment variables that influence the term's evaluation.manis the text of the man page for the term.man_xrefsare cross-references to other manual pages. These are used to generate aManpage.s_see_alsosection.
doc,man,envssupport the documentation markup language in which the following variables are recognized:$(tname)the term's name.$(mname)the main term's name.
val name : info -> stringname tiis the name of the term information.
Evaluation
type 'a result=[|`Ok of 'a|`Error of [ `Parse | `Term | `Exn ]|`Version|`Help]The type for evaluation results.
`Ok v, the term evaluated successfully andvis the result.`Version, the version string of the main term was printed on the help formatter.`Help, man page about the term was printed on the help formatter.`Error `Parse, a command line parse error occurred and was reported on the error formatter.`Error `Term, a term evaluation error occurred and was reported on the error formatter (seeTerm.ret).`Error `Exn, an exceptionewas caught and reported on the error formatter (see the~catchparameter ofeval).
val eval : ?help:Stdlib.Format.formatter -> ?err:Stdlib.Format.formatter -> ?catch:bool -> ?env:(string -> string option) -> ?argv:string array -> ('a t * info) -> 'a resulteval help err catch argv (t,i)is the evaluation result oftwith command line argumentsargv(defaults toSys.argv).If
catchistrue(default) uncaught exceptions are intercepted and their stack trace is written to theerrformatter.helpis the formatter used to print help or version messages (defaults toFormat.std_formatter).erris the formatter used to print error messages (defaults toFormat.err_formatter).envis used for environment variable lookup, the default usesSys.getenv.
val eval_choice : ?help:Stdlib.Format.formatter -> ?err:Stdlib.Format.formatter -> ?catch:bool -> ?env:(string -> string option) -> ?argv:string array -> ('a t * info) -> ('a t * info) list -> 'a resulteval_choice help err catch argv (t,i) choicesis likeevalexcept that if the first argument on the command line is not an option name it will look inchoicesfor a term whose information has this name and evaluate it.If the command name is unknown an error is reported. If the name is unspecified the "main" term
tis evaluated.idefines the name and man page of the program.
val eval_peek_opts : ?version_opt:bool -> ?env:(string -> string option) -> ?argv:string array -> 'a t -> 'a option * 'a resulteval_peek_opts version_opt argv tevaluatest, a term made of optional arguments only, with the command lineargv(defaults toSys.argv). In this evaluation, unknown optional arguments and positional arguments are ignored.The evaluation returns a pair. The first component is the result of parsing the command line
argvstripped from any help and version option ifversion_optistrue(defaults tofalse). It results in:Some _if the command line would be parsed correctly given the partial knowledge int.Noneif a parse error would occur on the options oft
The second component is the result of parsing the command line
argvwithout stripping the help and version options. It indicates what the evaluation would result in onargvgiven the partial knowledge int(for example it would return`Helpif there's a help option inargv). However in contrasts toevalandeval_choiceno side effects like error reporting or help output occurs.Note. Positional arguments can't be peeked without the full specification of the command line: we can't tell apart a positional argument from the value of an unknown optional argument.
Turning evaluation results into exit codes
Note. If you are using the following functions to handle the evaluation result of a term you should add default_exits to the term's information ~exits argument.
WARNING. You should avoid status codes strictly greater than 125 as those may be used by some shells.
val exit_status_cli_error : intexit_status_cli_erroris 124, an exit status for command line parsing errors.
val exit_status_internal_error : intexit_status_internal_erroris 125, an exit status for unexpected internal errors.
val exit_status_of_result : ?term_err:int -> 'a result -> intexit_status_of_result ~term_err ris anexit(3)status code determined fromras follows:exit_status_successifris one of`Ok _,`Version,`Helpterm_errifris`Error `Term,term_errdefaults to1.exit_status_cli_errorifris`Error `Parseexit_status_internal_errorifris`Error `Exn
val exit_status_of_status_result : ?term_err:int -> int result -> intexit_status_of_status_resultis likeexit_status_of_resultexcept for`Ok nwherenis used as the status exit code.
val exit : ?term_err:int -> 'a result -> unitexit ~term_err risPervasives.exit @@ exit_status_of_result ~term_err r
val exit_status : ?term_err:int -> int result -> unitexit_status ~term_err risPervasives.exit @@ exit_status_of_status_result ~term_err r