Skip to content

Commit ec47920

Browse files
committed
An example diff to rewrite module Arg without depending on Format
1 parent 0873cd4 commit ec47920

File tree

3 files changed

+71
-450
lines changed

3 files changed

+71
-450
lines changed

Diff for: jscomp/stdlib-406/arg.ml

+37-8
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ type error =
5555

5656
exception Stop of error (* used internally *)
5757

58-
open Printf
58+
5959

6060
let rec assoc3 x l =
6161
match l with
@@ -80,9 +80,18 @@ let print_spec buf (key, spec, doc) =
8080
if String.length doc > 0 then
8181
match spec with
8282
| Symbol (l, _) ->
83+
#if 0
8384
bprintf buf " %s %s%s\n" key (make_symlist "{" "|" "}" l) doc
85+
#else
86+
let sym = make_symlist "{" "|" "}" l in
87+
Buffer.add_string buf {j| $(key) $(sym)$(doc)\n|j}
88+
#end
8489
| _ ->
90+
#if 0
8591
bprintf buf " %s %s\n" key doc
92+
#else
93+
Buffer.add_string buf {j| $(key) $(doc)\n|j}
94+
#end
8695

8796

8897
let help_action () = raise (Stop (Unknown "-help"))
@@ -101,7 +110,11 @@ let add_help speclist =
101110

102111

103112
let usage_b buf speclist errmsg =
113+
#if 0
104114
bprintf buf "%s\n" errmsg;
115+
#else
116+
Buffer.add_string buf {j|$(errmsg)\n|j};
117+
#end
105118
List.iter (print_spec buf) (add_help speclist)
106119

107120

@@ -112,7 +125,7 @@ let usage_string speclist errmsg =
112125

113126

114127
let usage speclist errmsg =
115-
eprintf "%s" (usage_string speclist errmsg)
128+
Js.log (usage_string speclist errmsg)
116129

117130

118131
let current = ref 0
@@ -142,14 +155,30 @@ let parse_and_expand_argv_dynamic_aux allow_expand current argv speclist anonfun
142155
| Unknown "-help" -> ()
143156
| Unknown "--help" -> ()
144157
| Unknown s ->
158+
#if 0
145159
bprintf b "%s: unknown option '%s'.\n" progname s
160+
#else
161+
Buffer.add_string b {j|$(progname): unknown option '$(s)'.\n|j}
162+
#end
146163
| Missing s ->
164+
#if 0
147165
bprintf b "%s: option '%s' needs an argument.\n" progname s
166+
#else
167+
Buffer.add_string b {j|$(progname): option '$(s)' needs an argument.\n|j}
168+
#end
148169
| Wrong (opt, arg, expected) ->
170+
#if 0
149171
bprintf b "%s: wrong argument '%s'; option '%s' expects %s.\n"
150172
progname arg opt expected
173+
#else
174+
Buffer.add_string b {j|$(progname): wrong argument '$(arg)'; option '$(opt)' expects $(expected).\n|j}
175+
#end
151176
| Message s -> (* user error message *)
177+
#if 0
152178
bprintf b "%s: %s.\n" progname s
179+
#else
180+
Buffer.add_string b {j|$(progname): $(s).\n|j}
181+
#end
153182
end;
154183
usage_b b !speclist errmsg;
155184
if error = Unknown "-help" || error = Unknown "--help"
@@ -280,16 +309,16 @@ let parse l f msg =
280309
try
281310
parse_argv Sys.argv l f msg
282311
with
283-
| Bad msg -> eprintf "%s" msg; exit 2
284-
| Help msg -> printf "%s" msg; exit 0
312+
| Bad msg -> Js.log msg; exit 2
313+
| Help msg -> Js.log msg; exit 0
285314

286315

287316
let parse_dynamic l f msg =
288317
try
289318
parse_argv_dynamic Sys.argv l f msg
290319
with
291-
| Bad msg -> eprintf "%s" msg; exit 2
292-
| Help msg -> printf "%s" msg; exit 0
320+
| Bad msg -> Js.log msg; exit 2
321+
| Help msg -> Js.log msg; exit 0
293322

294323
let parse_expand l f msg =
295324
try
@@ -298,8 +327,8 @@ let parse_expand l f msg =
298327
let current = ref (!current) in
299328
parse_and_expand_argv_dynamic current argv spec f msg
300329
with
301-
| Bad msg -> eprintf "%s" msg; exit 2
302-
| Help msg -> printf "%s" msg; exit 0
330+
| Bad msg -> Js.log msg; exit 2
331+
| Help msg -> Js.log msg; exit 0
303332

304333

305334
let second_word s =

0 commit comments

Comments
 (0)