From 1253df349226345e7596e77e9b0120094f0f1a34 Mon Sep 17 00:00:00 2001 From: Siraaj Khandkar Date: Mon, 19 Nov 2018 20:04:25 -0500 Subject: [PATCH] Refactor CLI options gathering --- dups.ml | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/dups.ml b/dups.ml index 8ff8874..3cfbee3 100644 --- a/dups.ml +++ b/dups.ml @@ -142,6 +142,12 @@ type output = | Stdout | Directory of string +type opt = + { input : input + ; output : output + ; ignore : Str.regexp option + } + type count = { considered : int ref ; empty : int ref @@ -190,7 +196,7 @@ let make_output_fun = function ); close_out oc -let main input output ignore = +let main {input; output; ignore} = let t0 = Sys.time () in let count = { considered = ref 0 @@ -241,26 +247,22 @@ let main input output ignore = eprintf "Skipped due to unique size : %d\n%!" !(count.unique_size); eprintf "Ignored due to regex match : %d\n%!" !(count.ignored) -let () = - let input = ref Stdin in - let output = ref Stdout in - let ignore = ref None in - let assert_file_exists path = - if Sys.file_exists path then - () - else begin - eprintf "File does not exist: %S\n%!" path; +let get_opt () : opt = + let assert_ test x msg = + if not (test x) then begin + eprintf "%s\n%!" msg; exit 1 end in + let assert_file_exists path = + assert_ Sys.file_exists path (sprintf "File does not exist: %S" path) + in let assert_file_is_dir path = - if Sys.is_directory path then - () - else begin - eprintf "File is not a directory: %S\n%!" path; - exit 1 - end + assert_ Sys.is_directory path (sprintf "File is not a directory: %S" path) in + let input = ref Stdin in + let output = ref Stdout in + let ignore = ref None in let spec = [ ( "-out" , Arg.String (fun path -> @@ -288,4 +290,10 @@ let () = input := Directories (path :: paths) ) ""; - main !input !output !ignore + { input = !input + ; output = !output + ; ignore = !ignore + } + +let () = + main (get_opt ()) -- 2.20.1