main.ml 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. open Lexing
  2. open Types
  3. (* Parse command-line arguments *)
  4. let parse_args () =
  5. let args = {
  6. infiles = [];
  7. outfile = None;
  8. verbose = 1;
  9. } in
  10. let args_spec = [
  11. ("<file> ...", Arg.Rest (fun _ -> ()),
  12. " Optional input files (default is to read from stdin)");
  13. ("-o", Arg.String (fun s -> args.outfile <- Some s),
  14. "<file> Output file (defaults to stdout)");
  15. ("-v", Arg.Int (fun i -> args.verbose <- i),
  16. "<num> Set verbosity (0: nothing, 1: errors (default), \
  17. 2: compression rate, 3: debug)");
  18. ] in
  19. let usage =
  20. "Usage: " ^ Sys.argv.(0) ^ " [-o <file>] [-v <verbosity>] [<file> ...]"
  21. in
  22. Arg.parse args_spec (fun f -> args.infiles <- args.infiles @ [f]) usage;
  23. args
  24. (* Main function, returns exit status
  25. * Command-line arguments are stored in lobals.args *)
  26. let main () =
  27. let args = parse_args () in
  28. try
  29. let css =
  30. match args.infiles with
  31. | [] ->
  32. let input = Util.input_buffered stdin 512 in
  33. Parse.parse_input "<stdin>" input
  34. | files ->
  35. let rec loop = function
  36. | [] -> []
  37. | filename :: tl ->
  38. let input = Util.input_all (open_in filename) in
  39. let css = Parse.parse_input filename input in
  40. css @ loop tl
  41. in
  42. loop files
  43. in
  44. Util.print_css css;
  45. exit 0
  46. with
  47. | LocError (loc, msg) ->
  48. Util.prerr_loc_msg args loc ("Error: " ^ msg);
  49. | Failure err ->
  50. prerr_endline ("Error: " ^ err);
  51. exit 1
  52. let _ = main ()