summaryrefslogtreecommitdiff
path: root/testsuite/tests/lib-arg/test_rest_all.ml
blob: 81475013c18c42dd3d319cd0836b0b491a7cc9f5 (plain)
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
(* TEST
 expect;
*)

type arg = AString of string | ARest of string | ARest_all of string list

let push acc s =
  acc := s :: !acc

let f_str acc s = push acc (AString s)

let f_rest acc s = push acc (ARest s)

let f_rest_all acc ss = push acc (ARest_all ss)

let test args =
  let acc = ref [] in
  Arg.parse_argv ~current:(ref 0) args Arg.[
    "-str", String (f_str acc), "String (1)";
    "-rest", Rest (f_rest acc), "Rest (*)";
    "-rest-all", Rest_all (f_rest_all acc), "Rest_all (*)";
  ] failwith "";
  List.rev !acc

[%%expect{|
type arg = AString of string | ARest of string | ARest_all of string list
val push : 'a list ref -> 'a -> unit = <fun>
val f_str : arg list ref -> string -> unit = <fun>
val f_rest : arg list ref -> string -> unit = <fun>
val f_rest_all : arg list ref -> string list -> unit = <fun>
val test : string array -> arg list = <fun>
|}];;

let _ = test [|
  "prog";
  "-str"; "foo";
  "-str"; "bar";
  "-rest";
  "foobar";
  "-str"; "foobaz"
|];;
[%%expect{|
- : arg list =
[AString "foo"; AString "bar"; ARest "foobar"; ARest "-str"; ARest "foobaz"]
|}];;

let _ = test [|
  "prog";
  "-str"; "foo";
  "-str"; "bar";
  "-rest-all";
  "foobar";
  "-str"; "foobaz"
|];;
[%%expect{|
- : arg list =
[AString "foo"; AString "bar"; ARest_all ["foobar"; "-str"; "foobaz"]]
|}];;

(* Rest does nothing when there are no following arguments *)
let _ = test [|
  "prog";
  "-str"; "foo";
  "-str"; "bar";
  "-rest";
|];;
[%%expect{|
- : arg list = [AString "foo"; AString "bar"]
|}];;

(* Rest_all lets us detect that there were no rest arguments *)
let _ = test [|
  "prog";
  "-str"; "foo";
  "-str"; "bar";
  "-rest-all";
|];;
[%%expect{|
- : arg list = [AString "foo"; AString "bar"; ARest_all []]
|}];;