Source

SmallCC / src / option.ml

Full commit
exception OptionError of string * string

class option argv = 
object(this)

  val mutable files = []
  val mutable output = "a.out"

  method getFiles = files
  method getOutput = output

  method addFile (filepath:string) =
    files <- filepath::files

  method next (l:string list) (f:string -> unit) = match l with
      []   -> raise (OptionError("next", "Option missing"))
    | e::r -> f e; this#rec_parce r

  method rec_parce (l:string list) = match l with
        []                  ->
          ()
      | e::r when e = "-o" ->
          this#next r (fun e -> output <- e; ()); ()
      | e::r                ->
          this#addFile e; this#rec_parce r; ()
  
  method parse () = match (Array.to_list argv) with
      []      -> ()
    | _::args -> this#rec_parce args

  method dump () =
    print_endline ("Output: " ^ output);
    print_endline "File {";
    let f e = print_endline e; () in
      List.iter f files;
    print_endline "}"
      

end

let option =
  let a = new option Sys.argv in
    a