1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
let switch t = if t then "ON" else "OFF"

let cmdopt arr = 
  let o = ref false 
  and q = ref false
  and d = ref "-"
  and args = Queue.create () in

  Arg.parse_argv ~current:(ref 0) arr
    [ "-o", Arg.Set o, "";
      "-q", Arg.Set q, "";
      "-d", Arg.Symbol (["0";"1";"2";], fun s -> d:=s),"";] 
    (fun anon -> Queue.push anon args) "";

  if not !o then raise Exit ;

  let len = Queue.length args in

  Printf.printf "\
    [オプション情報]\n\
    o(output): %s\n\
    q(quote): %s\n\
    d(debug): %s\n\
    \n[パラメータ情報]\n指定数:%d \n"
    (switch !o) (switch !q) !d len;

  for i = 1 to len do 
    Printf.printf "%2d : %s \n" i (Queue.pop args);
  done;;

(* cmdopt [| "prog_name"; "-q"; "-o"; "aa"; "bb"|];; *)