open Printf
open Parseopt
type option myOptions = {
alpha : int;
bravo : string;
charlie : float;
delta : bool;
echo : unit;
foxtrot : [ `Ecks | `Why | `ReallyLongConstructorName "zee" ];
golf = 3 : action = store 6; int;
india = true : action = clear; bool;
illinois : key = ["-j"]; string;
kilo : str = (Printf.sprintf "%.5f"); float;
mutable lima = false: action = set; bool;
mike = (0,0) : str = (fun (i0,i1) -> Printf.sprintf "%d,%d" i0 i1);
conversion =
(fun _ s -> Scanf.sscanf s "%d,%d" (fun a b ->a,b));
(int * int);
november = `Ecks : [`Ecks | `You | `Vee];
oscar = `Off : action = store `On; [ `On | `Off];
verbosity = 0 : action = (fun o -> succ o.options.verbosity);
help = "increase verbosity level"; int;
print = false : action = set; help = "display received opts, args";bool;
_ : handler =
(fun _ -> print_string "$Revision: 1.10 $\n"; exit 0);
key = ["-V"]; help = "print version and exit"; unit;
_ : handler = (fun _ -> print_string
"Available commands:\ntag\nlog\n"; exit 0);
key = ["-L"]; help = "display available commands"; unit;
_ : handler = (fun ap ->
printf "Default values:\n%s\n"
(ap.option_parameters.to_string
ap.option_parameters.default); exit 0);
key = ["-D"]; help = "display default values"; unit;
tango : handler = (fun ap ->
let anonargs = ref [] in
let alpha = ref "default-string" in
let beta = ref 0xBADBEEF in
Arg.parse_argv (Array.of_list ("progname"::ap.args))
["-a",Arg.Set_string alpha,"set alpha";"-beta",Arg.Set_int beta,"set beta"]
(fun anon -> anonargs:= anon::!anonargs) "example -A [OPTS] args";
{ap.options with tango=Some (!alpha,!beta)},(List.rev !anonargs));
help = "an Arg parser"; str = (fun (s,i) -> sprintf "%s/%08X" s i);
(string * int);
}
let myOptions = {myOptions with
usage_suffix = "command [sub-options]" ;
parsetype = Gnu ;
keyspecs = [Short;Long2];
keepgoing = false
}
type option logOptions = {
date : string;
log = false: action = set; bool;
}
let logOptions = {logOptions with usage_prefix = "log "}
type option tagOptions = {
all_users = false: action = set; bool;
tag : string;
}
let tagOptions = {tagOptions with usage_prefix = "tag "}
let () =
let options, args = parse_argv myOptions in
options.lima <- false;
if options.verbosity > 1 then Printf.printf "really verbose!\n";
if options.print then begin
printf "Received options:\n%s\n" (myOptions.to_string options);
printf "Received anonymous arguments: %s\n" (String.concat " " (args));
end;
match args with
| "tag" :: args ->
let o,args = parse tagOptions args in
Printf.printf "Received suboptions:\n%s\n" (tagOptions.to_string o);
Printf.printf "Received subarguments %s\n" (String.concat " " args);
| "log" :: args ->
let o,args = parse logOptions args in
Printf.printf "Received suboptions:\n%s\n" (logOptions.to_string o);
Printf.printf "Received subarguments %s\n" (String.concat " " args);
| _ ->
Printf.printf "no command specified\n";
Printf.printf "Use -h option for detailed help\n";
usage myOptions