package ppx_deriving_cmdliner
Install
Dune Dependency
Authors
Maintainers
Sources
sha256=9d8c7e9955dafacac1eadc4059ff8e299ea780de4e3e55a2d7951fe1d0d35ad6
md5=5ff93ea7d67b016192cbe7a5f41853f8
Description
ppx_deriving_cmdliner is a ppx_deriving plugin that generates a Cmdliner Term.t for a record type.
README
[@@deriving cmdliner]
deriving Cmdliner is the easiest way to get a command line interface.
It is also a ppx_deriving plugin that generates a Cmdliner Term
for a given type.
Example
type params = {
username: string;
(** Your Github username *)
api_key: string;
(** Your Github API key *)
command: string; [@pos 0] [@docv "CMD"]
(** The Github API command to run *)
dry_run: bool;
(** Don't really run this command *)
time_to_wait: float; [@default 0.]
(** Just an example of another type *)
} [@@deriving cmdliner,show]
let _ =
let term = Cmdliner.Term.(const show_params $ params_cmdliner_term ()) in
let info = Cmdliner.Term.info Sys.argv.(0) in
Cmdliner.Term.eval (term, info)
Which gives you a CLI like the following:
NAME
awesome-cli
SYNOPSIS
awesome-cli [OPTION]... CMD
ARGUMENTS
CMD (required)
The Github API command to run
OPTIONS
--api-key=STRING (required)
Your Github API key
--dry-run
Don't really run this command
--help[=FMT] (default=auto)
Show this help in format FMT. The value FMT must be one of `auto',
`pager', `groff' or `plain'. With `auto', the format is `pager` or
`plain' whenever the TERM env var is `dumb' or undefined.
--time-to-wait=FLOAT (absent=0.)
Just an example of another type
--username=STRING (required)
Your Github username
Features
Custom type support
Ppx_deriving_cmdliner supports arbitrary types via a cmdliner_converter
interface. For example, the below two methods work for supporting M.t
(from test/tests.ml
)
module M = struct
type t = int * int
let fst (f,_) = f
let snd (_,s) = s
let of_string s =
try
let sepi = String.index s '|' in
let fst = String.sub s 0 sepi in
let snd = String.sub s (sepi+1) ((String.length s)-sepi-1) in
Result.Ok (int_of_string fst, int_of_string snd)
with _ -> Result.Error (`Msg (Printf.sprintf "Couldn't parse `%s`" s))
let to_string t =
Printf.sprintf "%d|%d" (fst t) (snd t)
let cmdliner_converter =
of_string,
(fun fmt t -> Format.fprintf fmt "%s" (to_string t))
end
type custom_types = {
foo: M.t; [@conv M.cmdliner_converter]
bar: M.t;
} [@@deriving cmdliner]
In short, a value of type string -> ('a, [ `Msg of string ]) Result.result) * 'a printer
must be provided (or will be looked for under the name cmdliner_converter
if the type is t
, else type_name_cmdliner_converter
) for the given type.
Attributes supported
Docs:
[@doc "Overwrites the docstring"]
,[@docs "SECTION TWO"]
,[@docv "VAL"]
Environment variables:
[@env "ENVNAME"]
,[@env.doc "Docs for the variable"]
,[@env.docs "SECTION ENVS"]
Other:
[@list_sep '@']
,[@default 123]
,[@enum [("a", Foo); ("b", Bar)]]
,[@aka ["b";"another-flag-name"]]
,[@conv cmdliner_converter]
(cf. required argument toconv
in Cmdliner),[@opt_all]
only ona' list
fields,[@term cmdliner_term]
for assiging an arbitraryCmdliner.Term.t
to a field.
Dependencies (8)
Dev Dependencies (2)
-
ppx_import
with-test & >= "1.1" & < "2.0"
-
alcotest
with-test
Used by (1)
Conflicts
None