main.ml 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. open Lexing
  2. open Types
  3. type args = {
  4. mutable infiles : string list;
  5. mutable outfile : string option;
  6. mutable verbose : int;
  7. mutable echo : bool;
  8. mutable keep_comments : bool;
  9. }
  10. (* Parse command-line arguments *)
  11. let parse_args () =
  12. let args = {
  13. infiles = [];
  14. outfile = None;
  15. verbose = 1;
  16. echo = false;
  17. keep_comments = false;
  18. } in
  19. let args_spec = [
  20. ("<file> ...", Arg.Rest (fun _ -> ()),
  21. " Optional input files (default is to read from stdin)");
  22. ("-o", Arg.String (fun s -> args.outfile <- Some s),
  23. "<file> Output file (defaults to stdout)");
  24. ("-v", Arg.Int (fun i -> args.verbose <- i),
  25. "<num> Set verbosity (0: nothing, 1: errors (default), \
  26. 2: compression rate, 3: debug)");
  27. ("--echo", Arg.Unit (fun _ -> args.echo <- true),
  28. " Don't minify, just pretty-print the parsed CSS");
  29. ("--keep-comments", Arg.Unit (fun _ -> args.keep_comments <- true),
  30. "\n Preserve top-level comments");
  31. ] in
  32. let usage =
  33. "Usage: " ^ Sys.argv.(0) ^ " [-o <file>] [-v <verbosity>] [<file> ...]"
  34. in
  35. Arg.parse args_spec (fun f -> args.infiles <- args.infiles @ [f]) usage;
  36. args
  37. let parse_files = function
  38. | [] ->
  39. let input = Util.input_buffered stdin 512 in
  40. (input, Parse.parse_input "<stdin>" input)
  41. | files ->
  42. let rec loop = function
  43. | [] -> []
  44. | filename :: tl ->
  45. let input = Util.input_all (open_in filename) in
  46. let stylesheet = Parse.parse_input filename input in
  47. (input, stylesheet) :: loop tl
  48. in
  49. let inputs, stylesheets = List.split (loop files) in
  50. (String.concat "" inputs, List.concat stylesheets)
  51. let handle_args args =
  52. let input, stylesheet = parse_files args.infiles in
  53. let write_output =
  54. match args.outfile with
  55. | None -> print_endline
  56. | Some name ->
  57. fun css -> let f = open_out name in output_string f css; close_out f
  58. in
  59. match args with
  60. | {echo = true} ->
  61. write_output (Stringify.string_of_stylesheet stylesheet)
  62. | _ ->
  63. let output = Stringify.minify_stylesheet stylesheet in
  64. write_output output;
  65. if args.verbose >= 2 then begin
  66. let il = String.length input in
  67. let ol = String.length output in
  68. Printf.fprintf stderr "compression: %d -> %d bytes (%d%% of original)\n"
  69. il ol (int_of_float (float_of_int ol /. float_of_int il *. 100.))
  70. end
  71. (* Main function, returns exit status *)
  72. let main () =
  73. let args = parse_args () in
  74. begin
  75. try
  76. handle_args args;
  77. exit 0
  78. with
  79. | LocError (loc, msg) ->
  80. Util.prerr_loc_msg (args.verbose >= 1) loc ("Error: " ^ msg);
  81. | Failure err ->
  82. prerr_endline ("Error: " ^ err);
  83. end;
  84. exit 1
  85. let _ = main ()