-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest__named.ml
335 lines (327 loc) · 11.3 KB
/
test__named.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
module Command = Cmdlang.Command
let%expect_test "named" =
let test =
Arg_test.create
(let%map_open.Command who =
Arg.named [ "who" ] Param.string ~docv:"WHO" ~doc:"hello who?"
in
print_endline ("Hello " ^ who))
in
Arg_test.eval_all test { prog = "test"; args = [] };
[%expect
{|
----------------------------------------------------- Climate
Evaluation Failed: Missing required named argument: --who
----------------------------------------------------- Cmdliner
test: required option --who is missing
Usage: test [--who=WHO] [OPTION]…
Try 'test --help' for more information.
("Evaluation Failed" ((exit_code 124)))
----------------------------------------------------- Core_command
("Evaluation Failed" "missing required flag: --who")
----------------------------------------------------- Stdlib_runner
Missing required named argument: "who".
("Evaluation Failed" ((exit_code 2)))
|}];
Arg_test.eval_all test { prog = "test"; args = [ "--who"; "World" ] };
[%expect
{|
----------------------------------------------------- Climate
Hello World
----------------------------------------------------- Cmdliner
Hello World
----------------------------------------------------- Core_command
Hello World
----------------------------------------------------- Stdlib_runner
Hello World
|}];
(* [climate], [cmdliner] and [stdlib-runner] support the [--arg=VALUE] syntax.
[core.command] does not. *)
Arg_test.eval_all test { prog = "test"; args = [ "--who=You" ] };
[%expect
{|
----------------------------------------------------- Climate
Hello You
----------------------------------------------------- Cmdliner
Hello You
----------------------------------------------------- Core_command
("Evaluation Failed" (
"Command.Failed_to_parse_command_line(\"unknown flag --who=You\")"))
----------------------------------------------------- Stdlib_runner
Hello You
|}];
()
;;
let%expect_test "1-letter-named" =
let test =
Arg_test.create
(let%map_open.Command who =
Arg.named [ "w" ] Param.string ~docv:"WHO" ~doc:"hello who?"
in
print_endline ("Hello " ^ who))
in
Arg_test.eval_all test { prog = "test"; args = [] };
[%expect
{|
----------------------------------------------------- Climate
Evaluation Failed: Missing required named argument: -w
----------------------------------------------------- Cmdliner
test: required option -w is missing
Usage: test [-w WHO] [OPTION]…
Try 'test --help' for more information.
("Evaluation Failed" ((exit_code 124)))
----------------------------------------------------- Core_command
("Evaluation Failed" "missing required flag: -w")
----------------------------------------------------- Stdlib_runner
Missing required named argument: "w".
("Evaluation Failed" ((exit_code 2)))
|}];
Arg_test.eval_all test { prog = "test"; args = [ "-w"; "World" ] };
[%expect
{|
----------------------------------------------------- Climate
Hello World
----------------------------------------------------- Cmdliner
Hello World
----------------------------------------------------- Core_command
Hello World
----------------------------------------------------- Stdlib_runner
Hello World
|}];
(* [climate], [cmdliner] and [stdlib-runner] support the [-a=VALUE] syntax.
[core.command] does not. *)
Arg_test.eval_all test { prog = "test"; args = [ "-w=You" ] };
[%expect
{|
----------------------------------------------------- Climate
Hello =You
----------------------------------------------------- Cmdliner
Hello =You
----------------------------------------------------- Core_command
("Evaluation Failed" (
"Command.Failed_to_parse_command_line(\"unknown flag -w=You\")"))
----------------------------------------------------- Stdlib_runner
Hello You
|}];
(* [climate] and [cmdliner] support the [-wVALUE] syntax. [core.command] and
[cmdlang] do not. *)
Arg_test.eval_all test { prog = "test"; args = [ "-wYou" ] };
[%expect
{|
----------------------------------------------------- Climate
Hello You
----------------------------------------------------- Cmdliner
Hello You
----------------------------------------------------- Core_command
("Evaluation Failed" (
"Command.Failed_to_parse_command_line(\"unknown flag -wYou\")"))
----------------------------------------------------- Stdlib_runner
test: unknown option '-wYou'.
Usage: test [OPTIONS]
eval-stdlib-runner
Options:
-w <WHO> hello who? (required)
-help Display this list of options
--help Display this list of options
("Evaluation Failed" ((exit_code 2)))
|}];
()
;;
let%expect_test "named_multi" =
let test =
Arg_test.create
(let%map_open.Command who =
Arg.named_multi [ "who" ] Param.string ~docv:"WHO" ~doc:"hello who?"
in
List.iter who ~f:(fun who -> print_endline ("Hello " ^ who)))
in
Arg_test.eval_all test { prog = "test"; args = [] };
[%expect
{|
----------------------------------------------------- Climate
----------------------------------------------------- Cmdliner
----------------------------------------------------- Core_command
----------------------------------------------------- Stdlib_runner
|}];
Arg_test.eval_all test { prog = "test"; args = [ "--who"; "World" ] };
[%expect
{|
----------------------------------------------------- Climate
Hello World
----------------------------------------------------- Cmdliner
Hello World
----------------------------------------------------- Core_command
Hello World
----------------------------------------------------- Stdlib_runner
Hello World
|}];
Arg_test.eval_all
test
{ prog = "test"
; args = List.concat [ [ "--who"; "World" ]; [ "--who"; "You" ]; [ "--who"; "Me" ] ]
};
[%expect
{|
----------------------------------------------------- Climate
Hello World
Hello You
Hello Me
----------------------------------------------------- Cmdliner
Hello World
Hello You
Hello Me
----------------------------------------------------- Core_command
Hello World
Hello You
Hello Me
----------------------------------------------------- Stdlib_runner
Hello World
Hello You
Hello Me
|}];
()
;;
let%expect_test "named_opt" =
let test =
Arg_test.create
(let%map_open.Command who =
Arg.named_opt [ "who" ] Param.string ~docv:"WHO" ~doc:"hello who?"
in
Option.iter who ~f:(fun who -> print_endline ("Hello " ^ who)))
in
Arg_test.eval_all test { prog = "test"; args = [] };
[%expect
{|
----------------------------------------------------- Climate
----------------------------------------------------- Cmdliner
----------------------------------------------------- Core_command
----------------------------------------------------- Stdlib_runner
|}];
Arg_test.eval_all test { prog = "test"; args = [ "--who"; "World" ] };
[%expect
{|
----------------------------------------------------- Climate
Hello World
----------------------------------------------------- Cmdliner
Hello World
----------------------------------------------------- Core_command
Hello World
----------------------------------------------------- Stdlib_runner
Hello World
|}];
()
;;
let%expect_test "named_with_default" =
let test =
Arg_test.create
(let%map_open.Command who =
Arg.named_with_default
[ "who" ]
Param.string
~docv:"WHO"
~default:"World"
~doc:"hello who?"
in
print_endline ("Hello " ^ who))
in
Arg_test.eval_all test { prog = "test"; args = [] };
[%expect
{|
----------------------------------------------------- Climate
Hello World
----------------------------------------------------- Cmdliner
Hello World
----------------------------------------------------- Core_command
Hello World
----------------------------------------------------- Stdlib_runner
Hello World
|}];
Arg_test.eval_all test { prog = "test"; args = [ "--who"; "You" ] };
[%expect
{|
----------------------------------------------------- Climate
Hello You
----------------------------------------------------- Cmdliner
Hello You
----------------------------------------------------- Core_command
Hello You
----------------------------------------------------- Stdlib_runner
Hello You
|}];
()
;;
let%expect_test "named_with_default__comma_separated" =
let test ?(default = [ "You"; "Me"; "World" ]) () =
Arg_test.create
(let%map_open.Command who =
Arg.named_with_default
[ "who" ]
(Param.comma_separated Param.string)
~docv:"WHO"
~default
~doc:"hello who?"
in
List.iter who ~f:(fun who -> print_endline ("Hello " ^ who)))
in
Arg_test.eval_all (test ()) { prog = "test"; args = [] };
[%expect
{|
----------------------------------------------------- Climate
Hello You
Hello Me
Hello World
----------------------------------------------------- Cmdliner
Hello You
Hello Me
Hello World
----------------------------------------------------- Core_command
Hello You
Hello Me
Hello World
----------------------------------------------------- Stdlib_runner
Hello You
Hello Me
Hello World
|}];
Arg_test.eval_all (test ~default:[] ()) { prog = "test"; args = [] };
[%expect
{|
----------------------------------------------------- Climate
----------------------------------------------------- Cmdliner
----------------------------------------------------- Core_command
----------------------------------------------------- Stdlib_runner
|}];
(* Empty values are currently treated inconsistently by the translation
backends. In [climate] and [stdlib-runner], you get a singleton made of the
empty string, in [cmdliner] you get the empty list, and in [core.command]
you get an error. *)
Arg_test.eval_all (test ()) { prog = "test"; args = [ "--who"; "" ] };
[%expect
{|
----------------------------------------------------- Climate
Hello
----------------------------------------------------- Cmdliner
----------------------------------------------------- Core_command
("Evaluation Failed" (
"Command.Failed_to_parse_command_line(\"failed to parse --who value \\\"\\\".\\n(Failure \\\"Command.Spec.Arg_type.comma_separated: empty list not allowed\\\")\")"))
----------------------------------------------------- Stdlib_runner
Hello
|}];
Arg_test.eval_all (test ()) { prog = "test"; args = [ "--who"; "Universe,Them Too" ] };
[%expect
{|
----------------------------------------------------- Climate
Hello Universe
Hello Them Too
----------------------------------------------------- Cmdliner
Hello Universe
Hello Them Too
----------------------------------------------------- Core_command
Hello Universe
Hello Them Too
----------------------------------------------------- Stdlib_runner
Hello Universe
Hello Them Too
|}];
()
;;