module Dispatch_arg: sig
.. end
Command line argument handling by projecting ocaml functions.
Command line argument handling by projecting ocaml functions.
Introduction
A trivial sample program could look like:
open Core_extended.Std
open Dispatch_arg.Spec
let e = Dispatch_arg.embed
let () =
Dispatch_arg.run
[
e (fun x y -> x + y)
(int "x" ++ int "y" --> Result.int)
~cmd:"add"
~descr:"add two integers";
e (fun x y -> x - y)
(int "x" ++ int "y" --> Result.int)
~cmd:"sub"
~descr:"substract two integers"
]
This embeds the (-)
and the (+)
for the command line. The embedding is
done based on the functions signatures.
Commands are usually embedded using an OCaml representation of their type.
Base types
type
descr = {
|
name : string option ; |
|
descr : string ; |
|
arg_descr : string ; |
}
type 'a
t = {
|
run : string list -> 'a option ; |
|
doc : descr option ; |
}
This is the low level representation of a command.
module Spec: sig
.. end
This module defines functional unparsing style combinators used to embed our
callback functions.
val embed : ?cmd:string ->
descr:string -> 'a -> ('b, 'a) Spec.t -> 'b t
val declare : ?doc:descr -> (string list -> 'a option) -> 'a t
val run : ?prog_name:string ->
?args:string list ->
?global:Core.Std.Arg.t list -> string t list -> 'a
val run_gen : ?prog_name:string ->
?args:string list ->
?global:Core.Std.Arg.t list -> 'a t list -> 'a
type
shell = {
|
prompt : unit -> string ; |
|
quit : 'a. unit -> 'a ; |
|
err : exn -> unit ; |
}
val default_shell : shell
val shell : shell ->
?global:Core.Std.Arg.t list -> string t list -> 'a