module Dispatch_arg: Dispatch_arg
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 ; |
}
The documentation for an embedded command. Used the generate help
messages.
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
Manual declaration; should be kept only for advanced uses
val run : ?prog_name:string ->
?args:string list ->
?global:Core.Std.Arg.t list -> string t list -> 'a
Run a command
If args is not specified then defaults to the command line arguments
global
: is a list of flags that are run whatever is the command you use.
val run_gen : ?prog_name:string ->
?args:string list ->
?global:Core.Std.Arg.t list -> 'a t list -> 'a
Shells
The following functions are used to define interactive shells.
type
shell = {
|
prompt : unit -> string ; |
|
quit : 'a. unit -> 'a ; |
|
err : exn -> unit ; |
}
The type used to represent shell configurations
val default_shell : shell
val shell : shell ->
?global:Core.Std.Arg.t list -> string t list -> 'a
Dispatch a new shell. This function never returns.