stdcompat__arg.ml1 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 182include Arg (* exception Help of string exception Bad of string let not_implemented () = failwith "Stdcompat.Arg is not fully implemented yet. Please fill an issue: https://github.com/thierry-martinez/stdcompat/issues ." type spec = | Unit of (unit -> unit) | Bool of (bool -> unit) | Set of bool ref | Clear of bool ref | String of (string -> unit) | Set_string of string ref | Int of (int -> unit) | Set_int of int ref | Float of (float -> unit) | Set_float of float ref | Tuple of spec list | Symbol of string list * (string -> unit) | Rest of (string -> unit) | Rest_all of (string list -> unit) | Expand of (string -> string array) type key = string type doc = string type usage_msg = string type anon_fun = string -> unit let rec no_expand spec = match spec with | Unit f -> Arg.Unit f | Bool f -> Arg.Bool f | Set r -> Arg.Set r | Clear c -> Arg.Clear c | String f -> Arg.String f | Set_string r -> Arg.Set_string r | Int f -> Arg.Int f | Set_int r -> Arg.Set_int r | Float f -> Arg.Float f | Set_float r -> Arg.Set_float r | Tuple l -> Arg.Tuple (List.map no_expand l) | Symbol (l, f) -> Arg.Symbol (l, f) | Rest f -> Arg.Rest f | Rest_all _ -> not_implemented () | Expand f -> Expand f (* | Expand _ -> not_implemented () *) let rec expand spec = match spec with | Arg.Unit f -> Unit f | Arg.Bool f -> Bool f | Arg.Set r -> Set r | Arg.Clear c -> Clear c | Arg.String f -> String f | Arg.Set_string r -> Set_string r | Arg.Int f -> Int f | Arg.Set_int r -> Set_int r | Arg.Float f -> Float f | Arg.Set_float r -> Set_float r | Arg.Tuple l -> Tuple (List.map expand l) | Arg.Symbol (l, f) -> Symbol (l, f) | Arg.Rest f -> Rest f | Arg.Expand f -> Expand f | Arg.Rest_all f -> Rest_all f let no_expand_list l = List.map (fun (k, s, d) -> k, no_expand s, d) l let expand_list l = List.map (fun (k, s, d) -> k, expand s, d) l let usage_string l msg = Arg.usage_string (no_expand_list l) msg (* let usage_string l msg = not_implemented () *) let align ?limit:_limit l = expand_list (Arg.align (no_expand_list l)) (* let align ?limit:_limit _l = not_implemented () *) let parse l anon msg = Arg.parse (no_expand_list l) anon msg let parse_argv ?current argv l anon msg = Arg.parse_argv ?current argv (no_expand_list l) anon msg let usage l msg = Arg.usage (no_expand_list l) msg let current = Arg.current let read_aux trim sep file = let channel = open_in_bin file in try let buffer = Buffer.create 20 in let accu = ref [] in let push () = let s = Buffer.contents buffer in let s = if trim && s <> "" && s.[String.length s - 1] = '\r' then String.sub s 0 (String.length s - 1) else s in accu := s :: !accu in try while true do let c = input_char channel in if c = sep then begin push (); Buffer.clear buffer end else Buffer.add_char buffer c done; assert false with End_of_file -> if Buffer.length buffer > 0 then push (); close_in channel; Array.of_list (List.rev !accu) with e -> close_in_noerr channel; raise e let read_arg = read_aux true '\n' let read_arg0 = read_aux false '\x00' let write_aux sep file args = let channel = open_out_bin file in try Array.iter (fun s -> Printf.fprintf channel "%s%c" s sep) args with e -> close_out_noerr channel; raise e let write_arg = write_aux '\n' let write_arg0 = write_aux '\x00' let parse_argv_dynamic ?current:_current _ = not_implemented () let parse_dynamic _ = not_implemented () let parse_expand _ = not_implemented () let parse_and_expand_argv_dynamic _ = not_implemented () *)