Skip to content

Instantly share code, notes, and snippets.

@mdales
Last active January 9, 2023 14:13
Show Gist options
  • Save mdales/19dd7de6de16ec05028ff352d07c33d4 to your computer and use it in GitHub Desktop.
Save mdales/19dd7de6de16ec05028ff352d07c33d4 to your computer and use it in GitHub Desktop.
My first ocaml program
let usage_msg = "cat <file1> [<file2>] ... -o <output>"
let input_files = ref []
let output_file = ref ""
let anon_fun filename = input_files := !input_files @ [filename]
let speclist = [
("-o", Arg.Set_string output_file, "Set output file name");
]
let () =
Arg.parse speclist anon_fun usage_msg;
try
let oc =
match !output_file with
| "" -> Stdlib.stdout
| _ -> open_out !output_file
in
let f filename =
try
let ic = open_in filename in
let rec loop () =
let line = input_line ic in
Printf.fprintf oc "%s\n" line;
loop ()
in
try
loop ()
with
| End_of_file -> close_in ic
| e -> raise e
with e ->
Printf.fprintf Stdlib.stderr "Failed to open %s\n" filename
in
List.iter f !input_files
with e ->
Printf.fprintf Stdlib.stderr "Failed to create output %s\n" !output_file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment