fork download
  1. kakadu:/tmp$ ocamlc a.ml && ./a.out -Dpublic-protected 1 2 3 -Dprivate=protected
  2. Defines: -Dprivate=protected,-Dpublic-protected
  3. Files: 1,2,3
  4. kakadu:/tmp$ cat a.ml
  5. open Printf
  6.  
  7. (* Filled with any cpp-like defines seen on argv.
  8.  
  9. FIXME: Add something like `-D__ACTION_SCRIPT_2__' to it so we can
  10. differentiate when we're compiling action script, and when c *)
  11. let defines = ref ([] : string list)
  12.  
  13. (* Filled with the rest of command-line arguments *)
  14. let args = ref ([] : string list)
  15.  
  16. (* List of command-options we'd like to process *)
  17. let options = ref ([] : (string * Arg.spec * string) list)
  18.  
  19. (* One option just for the test *)
  20. let verbose = ref false
  21.  
  22. let begins_with_D str =
  23. String.length str >= 2 && str.[0] = '-' && str.[1] = 'D';;
  24.  
  25. let split_argv arr =
  26. let usage = "usage string\n Options :" in
  27.  
  28. let defines = ref [] in
  29. let arr = Array.map (fun s ->
  30. if (String.length s>=2) && (s.[0]='-' && s.[1]='D')
  31. then (defines := s :: !defines; "-dummy")
  32. else s
  33. ) arr
  34. in
  35. let args_spec = [
  36. ("-v", Arg.Unit (fun () -> verbose := true), "turn on verbose mode");
  37. ("-dummy", Arg.Unit (fun () -> ()), "dummy value")
  38. ] @ !options in
  39.  
  40. let anon : string list ref = ref [] in
  41. Arg.parse_argv arr args_spec (fun x -> anon := x :: !anon) usage;
  42. anon:= List.rev !anon;
  43.  
  44. printf "Defines: %s\n" (String.concat "," !defines);
  45. printf "Files: %s\n" (String.concat "," !anon)
  46. ;;
  47.  
  48. let () = split_argv Sys.argv;;
  49.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty