package janestreet_csv

  1. Overview
  2. Docs

Source file csv_param.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
open! Core
module Time = Time_float_unix
include Csv_param_intf

module T = struct
  include Command.Param

  let file_stdin_anon = Csv_common.Or_file.anon
  let file_stdin_flag = Csv_common.Or_file.flag
  let files = anon (sequence ("filename" %: Filename_unix.arg_type))

  (* flags *)

  let reverse =
    flag "-reverse" no_arg ~doc:" reverse sorting order" ~aliases:[ "--reverse" ]
  ;;

  let field' ~aliases =
    flag "-field" (required string) ~doc:"<field> field name to sort on" ~aliases
  ;;

  let field = field' ~aliases:[ "--field" ]

  let time_field =
    flag
      "-time-field"
      (required string)
      ~doc:"<field> field with times for snapping to grid"
      ~aliases:[ "--time-field" ]
  ;;

  let start_time =
    flag
      "-start"
      (required Time.arg_type)
      ~doc:"<time> time to start grid"
      ~aliases:[ "--start" ]
  ;;

  let stop_time =
    flag
      "-stop"
      (required Time.arg_type)
      ~doc:"<time> time to stop grid"
      ~aliases:[ "--stop" ]
  ;;

  let grid_step =
    flag
      "-step"
      (required Time.Span.arg_type)
      ~doc:"<span> time span for grid step"
      ~aliases:[ "--step" ]
  ;;

  let max_width =
    flag "-limit-width-to" (optional int) ~doc:" maximum column width in output"
  ;;

  let prefer_split_on_spaces =
    flag_optional_with_default_doc
      "-prefer-split-on-spaces"
      bool
      [%sexp_of: bool]
      ~default:true
      ~doc:"BOOL prefer splitting lines on spaces rather than mid-word"
  ;;

  let regexp_arg = Command.Arg_type.create Re2.create_exn

  let regexp =
    flag
      "-regexp"
      (required regexp_arg)
      ~doc:"<regexp> regexp to search for (re2 style)"
      ~aliases:[ "--regexp" ]
  ;;

  let invert =
    flag
      "-invert-match"
      no_arg
      ~doc:" Invert the sense of matching, to select non-matching lines."
      ~aliases:[ "-v" ]
  ;;

  let skip_lines =
    flag
      "-skip-lines"
      (optional int)
      ~doc:"<nr-of-lines> drop this many lines from the beginning of the input"
      ~aliases:[ "--skip-lines" ]
  ;;

  let default_sep = ','
  let default_space = 2

  let sep_arg =
    Arg_type.create (fun s ->
      if String.( = ) s "\\t"
      then '\t'
      else if String.length s <> 1
      then failwithf "Delimiter <%s> not 1 char!" s ()
      else s.[0])
  ;;

  let sep =
    flag
      "-sep"
      ~aliases:[ "-d"; "-delim"; "--sep" ]
      (optional_with_default default_sep sep_arg)
      ~doc:(sprintf "CHAR csv separator (default: '%c')" default_sep)
  ;;

  let key_specifier =
    let open Command.Param in
    flag
      "k"
      (required string)
      ~doc:"SPEC comma-separated list of fields comprising the key"
  ;;

  let no_header =
    let open Command.Param in
    flag
      "-nh"
      no_arg
      ~doc:"  do not treat the first row as a header row"
      ~aliases:[ "-no-header"; "--no-header" ]
  ;;

  let space =
    let open Command.Param in
    flag
      "-s"
      (optional_with_default default_space int)
      ~doc:
        (sprintf "NUM how far apart to space out columns (default: '%d')" default_space)
  ;;

  let suppress_header =
    let open Command.Param in
    flag
      "-sh"
      no_arg
      ~doc:" keep the header row from appearing in the output"
      ~aliases:[ "-suppress-header"; "--suppress-header" ]
  ;;

  let fields_gen ~doc =
    let open Command.Param in
    let arg_type =
      Arg_type.comma_separated ~strip_whitespace:true string ~allow_empty:true
    in
    flag ~aliases:[ "--fields" ] "-fields" (optional_with_default [] arg_type) ~doc
  ;;

  let fields = fields_gen ~doc:" named fields to extract, comma separated"

  let fields_backward_compat =
    let open Command.Param in
    let arg_type =
      Arg_type.comma_separated ~strip_whitespace:true string ~allow_empty:true
    in
    flag
      ~aliases:[ "-f"; "--field" ]
      "-field"
      (optional_with_default [] arg_type)
      ~doc:" named fields to sort on, comma separated (outermost sort first)"
  ;;

  let reverse_fields =
    let open Command.Param in
    let arg_type =
      Arg_type.comma_separated ~strip_whitespace:true string ~allow_empty:true
    in
    flag
      ~aliases:[ "--reverse-fields" ]
      "-reverse-fields"
      (optional_with_default [] arg_type)
      ~doc:" fields for which to reverse the sort order"
  ;;

  let pop_fields =
    fields_gen ~doc:" named fields required to count as fully populated, comma separated"
  ;;

  let exclude_fields =
    let open Command.Param in
    flag
      ~aliases:[ "--invert-match" ]
      "-v"
      no_arg
      ~doc:" exclude specified fields rather than extract them"
  ;;

  module Key_value_pair = struct
    type t = string * string option

    let of_string s =
      match String.split s ~on:'=' with
      | [ key ] -> ((key, None) : t)
      | [ key; value ] -> ((key, Some value) : t)
      | _ -> failwithf "Couldn't parse key=value pair: %s" s ()
    ;;

    let arg_type = Arg_type.create of_string

    let flag name desc =
      let open Command.Param in
      flag
        ("-" ^ name)
        ~aliases:[ "--" ^ name ]
        (listed arg_type)
        ~doc:(sprintf " %s attribute(s) in HTML output (e.g. \"align=center\")" desc)
    ;;
  end

  let table_attrs = Key_value_pair.flag "table" "Table"
  let th_attrs = Key_value_pair.flag "th" "Table header"
  let tr_attrs = Key_value_pair.flag "tr" "Table row"
  let td_attrs = Key_value_pair.flag "td" "Table cell"

  (* This could be specified through the more general table attributes above, but
     [Html.Html_table] handles this attribute separately, requiring a separate
     command-line option. *)
  let border =
    let open Command.Param in
    flag "-border" ~aliases:[ "--border" ] no_arg ~doc:" Visible borders in HTML output"
  ;;
end

include T
include Applicative.Make_let_syntax (T) (Open_on_rhs_intf) (T)
OCaml

Innovation. Community. Security.