open Printf (** a simple symbol type *) type option symbol_one = Ecks | Why | Zee "zed" (** an example option type: options can be specified either short (-a, -b, -c, ...) or long with two dashes (--alpha, --bravo, --charlie, ...) *) (*type option opts = { *) type option [short, long2] opts = { (** these fields just use the default behavior: the types become option types, and provided arguments are assigned to the fields, which default to None if no argument is given. *) alpha: int; bravo: string; charlie: float; delta: bool; (* three possible values: Some true | Some false | None *) echo: unit; (* note that unit option is effectively boolean *) foxtrot: symbol_one; (* provided value must be ecks | why | zed *) (** these fields modify the default behavior in some simple ways *) golf: default = 5; help = "this is alpha"; int; hotel: key = "-x"; (* set key to -x (otherwise would be -h) *) string; india: spec = "Clear"; (* -i sets india to false; otherwise it's true *) default = true; bool; juliet: spec = "Set"; (* -j sets juliet to Some true; otherwise None *) bool; kilo: str = (Printf.sprintf "%.5f"); (* change print behavior *) float; (* some more complex behavior *) (* a silly example of how a more complex type can be used. Note spec defaults to Arg.String since type isn't int, float, bool, unit, or symbol*) lima: default = (0,0); str = (fun (i0,i1) -> Printf.sprintf "%d,%d" i0 i1); arg = (fun s -> lima := Scanf.sscanf s "%d,%d" (fun a b ->a,b)); (int * int); (** every appearance of -v increases the verbosity level by one *) verbosity: spec = "Unit"; arg = (fun () -> incr verbosity); default = 0; help = "increase verbosity level"; int; (** this option has no corresponding field *) _: arg = (fun () -> print_string "$Revision: 1.9 $\n"; exit 0); key = "-V"; help = "print version and exit"; unit; } let _ = let opts,args = parse_opts " [OPTS] arg0 arg1" in match args with | [arg0; arg1] -> print_opts stdout opts; printf "args: [%s %s]\n" arg0 arg1 | _ -> usage_opts ": Wrong number of arguments\nUsage: example [OPTS] arg0 arg1"