読者です 読者をやめる 読者になる 読者になる

foldコマンド

let version="0.1"
let display_width = ref 80
let filenames = ref []

let spec = [("--width", Arg.Int (fun w -> display_width := w),
	     "Set width of lines");
	    ("-version",
	     Arg.Unit
	       (fun () -> Printf.printf "fold in OCaml ver: %s\n" version),
	     "Display version information.")]

let show_file filename =
  let cnl = open_in filename in
  let rec print_line str =
    let len = String.length str in
    if len > !display_width
	then (print_endline (String.sub str 0 !display_width);
	      print_line (String.sub str !display_width (len - !display_width)))
	else print_endline str
  in try while true do
    print_line (input_line cnl)
  done with End_of_file -> close_in cnl

let _ = 
  Arg.parse spec
    (fun s -> filenames := s :: !filenames)
    "Usage: [--width n] [-help] [-version] filename ...";
  List.iter show_file (List.rev !filenames)