Source file change_separator.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
open! Core
open! Async
let summary = "Changes the delimiter, respecting quoting"
let main =
let%map_open.Csv_param () = return ()
and in_sep =
flag
"-input-separator"
(optional_with_default ',' sep_arg)
~doc:"CHAR separator expected in input"
and out_sep =
flag "-output-separator" (required sep_arg) ~doc:"CHAR separator desired in output"
and filename = anon (maybe ("FILE" %: Filename_unix.arg_type)) in
fun () ->
let%bind r =
match filename with
| None ->
return
(Delimited.Read.pipe_of_reader
Delimited.Read.Row.builder
~sep:in_sep
~header:`No
(force Reader.stdin))
| Some filename ->
Delimited.Read.create_reader
Delimited.Read.Row.builder
~sep:in_sep
~header:`No
filename
in
let w =
Delimited.Write.Expert.By_row.of_writer_and_close ~sep:out_sep (force Writer.stdout)
in
let%map () =
Pipe.iter r ~f:(fun row -> Pipe.write_if_open w (Delimited.Read.Row.to_list row))
in
Pipe.close_read r;
Pipe.close w
;;
let command = Command.async main ~summary ~behave_nicely_in_pipeline:false