kakadu:/tmp$ ocamlc a.ml && ./a.out -Dpublic-protected 1 2 3 -Dprivate=protected
Defines: -Dprivate=protected,-Dpublic-protected
Files: 1,2,3
kakadu:/tmp$ cat a.ml
open Printf
(* Filled with any cpp-like defines seen on argv.
FIXME: Add something like `-D__ACTION_SCRIPT_2__' to it so we can
differentiate when we're compiling action script, and when c *)
let defines = ref ([] : string list)
(* Filled with the rest of command-line arguments *)
let args = ref ([] : string list)
(* List of command-options we'd like to process *)
let options = ref ([] : (string * Arg.spec * string) list)
(* One option just for the test *)
let verbose = ref false
let begins_with_D str =
String.length str >= 2 && str.[0] = '-' && str.[1] = 'D';;
let split_argv arr =
let usage = "usage string\n Options :" in
let defines = ref [] in
let arr = Array.map (fun s ->
if (String.length s>=2) && (s.[0]='-' && s.[1]='D')
then (defines := s :: !defines; "-dummy")
else s
) arr
in
let args_spec = [
("-v", Arg.Unit (fun () -> verbose := true), "turn on verbose mode");
("-dummy", Arg.Unit (fun () -> ()), "dummy value")
] @ !options in
let anon : string list ref = ref [] in
Arg.parse_argv arr args_spec (fun x -> anon := x :: !anon) usage;
anon:= List.rev !anon;
printf "Defines: %s\n" (String.concat "," !defines);
printf "Files: %s\n" (String.concat "," !anon)
;;
let () = split_argv Sys.argv;;
a2FrYWR1Oi90bXAkIG9jYW1sYyAgYS5tbCAmJiAuL2Eub3V0IC1EcHVibGljLXByb3RlY3RlZCAxIDIgMyAtRHByaXZhdGU9cHJvdGVjdGVkCkRlZmluZXM6IC1EcHJpdmF0ZT1wcm90ZWN0ZWQsLURwdWJsaWMtcHJvdGVjdGVkCkZpbGVzOiAxLDIsMwprYWthZHU6L3RtcCQgY2F0IGEubWwKb3BlbiBQcmludGYKCigqIEZpbGxlZCB3aXRoIGFueSBjcHAtbGlrZSBkZWZpbmVzIHNlZW4gb24gYXJndi4gICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICA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