Skip to content

Instantly share code, notes, and snippets.

@fujimaki-k
Last active January 19, 2017 08:35
Show Gist options
  • Save fujimaki-k/4b887e5672ed8982b6bf9979d29770df to your computer and use it in GitHub Desktop.
Save fujimaki-k/4b887e5672ed8982b6bf9979d29770df to your computer and use it in GitHub Desktop.
Parse arguments
open Arg;;
open Printf;;
(* Arguments *)
let files = ref [];;
let format = ref "";;
let height = ref 0;;
let percent = ref 0;;
let recursive = ref false;;
let version = "1.0";;
let width = ref 0;;
(* Functions *)
let show_version _ =
printf "Parse arguments sample version %s\n" version;;
let get_files filename =
files := !files @ [filename];;
let specs = [
("-w", Set_int width, "Resized image width");
("-h", Set_int height, "Resized image height");
("-p", Set_int percent, "Percent of source image");
("-r", Set recursive, "Set recursive mode");
("-f", Set_string format, "Resized image format (JPEG or PNG)");
("-v", Unit show_version, "Display version number");
("--width", Set_int width, "Resized image width");
("--height", Set_int height, "Resized image height");
("--percent", Set_int percent, "Percent of source image");
("--recursive", Set recursive, "Set recursive mode");
("--format", Set_string format, "Resized image format (JPEG or PNG)");
("--version", Unit show_version, "Display version number")
];;
Arg.parse specs get_files "Usage : resize [options] <src_filename> <dest_filename>";;
if (!width > 0 || !height > 0) && !percent > 0 then begin
prerr_endline "Both width/height and percent can not be specified";
exit 1;
end;;
(* Test *)
print_endline (string_of_int !width);;
print_endline (string_of_int !height);;
print_endline (string_of_int !percent);;
print_endline (string_of_bool !recursive);;
print_endline !format;;
List.iter (fun file -> print_endline file) !files;;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment