Skip to content

Commit f146f6b

Browse files
committed
simplify command line parsing
1 parent 92e3484 commit f146f6b

File tree

4 files changed

+62
-58
lines changed

4 files changed

+62
-58
lines changed

jscomp/bsb_helper/bsb_helper_arg.ml

+23-28
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type error =
1717
| Missing of string
1818
| Message of string
1919

20-
exception Stop of error
20+
2121

2222

2323
type t = (string * spec * string) list
@@ -46,7 +46,7 @@ let usage_b (buf : Ext_buffer.t) speclist errmsg =
4646

4747

4848

49-
let stop_raise progname (error : error) speclist errmsg =
49+
let stop_raise ~progname ~(error : error) speclist errmsg =
5050
let b = Ext_buffer.create 200 in
5151
begin match error with
5252
| Unknown ("-help" | "--help" | "-h") ->
@@ -73,37 +73,32 @@ let stop_raise progname (error : error) speclist errmsg =
7373
raise (Bad (Ext_buffer.contents b))
7474

7575

76-
let parse_exn (speclist : t) anonfun errmsg =
77-
let argv = Sys.argv in
78-
let stop_raise error = stop_raise argv.(0) error speclist errmsg in
76+
let parse_exn ~progname ~argv ~start (speclist : t) anonfun errmsg =
7977
let l = Array.length argv in
80-
let current = ref 1 in (* 0 is progname*)
78+
let current = ref start in
8179
while !current < l do
8280
let s = argv.(!current) in
8381
if s <> "" && s.[0] = '-' then begin
84-
let action =
85-
match assoc3 s speclist with
86-
| Some action -> action
87-
| None -> stop_raise (Unknown s)
88-
in
89-
begin try
90-
let treat_action spec = match spec with
91-
| Set r -> r := true;
92-
| String f when !current + 1 < l ->
93-
f argv.(!current + 1);
94-
incr current;
95-
| Set_string r when !current + 1 < l ->
96-
r := argv.(!current + 1);
97-
incr current;
98-
| _ -> raise (Stop (Missing s))
99-
in
100-
treat_action action
101-
with Bad m -> stop_raise (Message m);
102-
| Stop e -> stop_raise e;
103-
end;
104-
incr current;
82+
match assoc3 s speclist with
83+
| Some action -> begin
84+
incr current;
85+
begin match action with
86+
| Set r -> r := true;
87+
| String f ->
88+
if !current < l then begin
89+
f argv.(!current);
90+
incr current;
91+
end else stop_raise ~progname ~error:(Missing s) speclist errmsg
92+
| Set_string r ->
93+
if !current < l then begin
94+
r := argv.(!current);
95+
incr current;
96+
end else stop_raise ~progname ~error:(Missing s) speclist errmsg
97+
end;
98+
end;
99+
| None -> stop_raise ~progname ~error:(Unknown s) speclist errmsg
105100
end else begin
106-
(try anonfun s with Bad m -> stop_raise (Message m));
101+
(try anonfun s with Bad m -> stop_raise ~progname ~error:(Message m) speclist errmsg);
107102
incr current;
108103
end;
109104
done;

jscomp/bsb_helper/bsb_helper_arg.mli

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ type usage_msg = string
1212
type anon_fun = (string -> unit)
1313

1414
val parse_exn :
15+
progname:string ->
16+
argv:string array ->
17+
start:int ->
1518
(key * spec * doc) list -> anon_fun -> usage_msg -> unit
1619

1720

jscomp/main/bsb_helper_main.ml

+5-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ let anonymous filename =
3838
let usage = "Usage: bsb_helper.exe [options] \nOptions are:"
3939

4040
let () =
41-
Bsb_helper_arg.parse_exn [
41+
Bsb_helper_arg.parse_exn
42+
~progname:Sys.argv.(0)
43+
~argv:Sys.argv
44+
~start:1
45+
[
4246
"-g", Set dev_group ,
4347
" Set the dev group (default to be 0)"
4448
;

lib/4.06.1/bsb_helper.ml

+31-29
Original file line numberDiff line numberDiff line change
@@ -1622,6 +1622,9 @@ type usage_msg = string
16221622
type anon_fun = (string -> unit)
16231623

16241624
val parse_exn :
1625+
progname:string ->
1626+
argv:string array ->
1627+
start:int ->
16251628
(key * spec * doc) list -> anon_fun -> usage_msg -> unit
16261629

16271630

@@ -1648,7 +1651,7 @@ type error =
16481651
| Missing of string
16491652
| Message of string
16501653

1651-
exception Stop of error
1654+
16521655

16531656

16541657
type t = (string * spec * string) list
@@ -1677,7 +1680,7 @@ let usage_b (buf : Ext_buffer.t) speclist errmsg =
16771680

16781681

16791682

1680-
let stop_raise progname (error : error) speclist errmsg =
1683+
let stop_raise ~progname ~(error : error) speclist errmsg =
16811684
let b = Ext_buffer.create 200 in
16821685
begin match error with
16831686
| Unknown ("-help" | "--help" | "-h") ->
@@ -1704,37 +1707,32 @@ let stop_raise progname (error : error) speclist errmsg =
17041707
raise (Bad (Ext_buffer.contents b))
17051708

17061709

1707-
let parse_exn (speclist : t) anonfun errmsg =
1708-
let argv = Sys.argv in
1709-
let stop_raise error = stop_raise argv.(0) error speclist errmsg in
1710+
let parse_exn ~progname ~argv ~start (speclist : t) anonfun errmsg =
17101711
let l = Array.length argv in
1711-
let current = ref 1 in (* 0 is progname*)
1712+
let current = ref start in
17121713
while !current < l do
17131714
let s = argv.(!current) in
17141715
if s <> "" && s.[0] = '-' then begin
1715-
let action =
1716-
match assoc3 s speclist with
1717-
| Some action -> action
1718-
| None -> stop_raise (Unknown s)
1719-
in
1720-
begin try
1721-
let treat_action spec = match spec with
1722-
| Set r -> r := true;
1723-
| String f when !current + 1 < l ->
1724-
f argv.(!current + 1);
1725-
incr current;
1726-
| Set_string r when !current + 1 < l ->
1727-
r := argv.(!current + 1);
1728-
incr current;
1729-
| _ -> raise (Stop (Missing s))
1730-
in
1731-
treat_action action
1732-
with Bad m -> stop_raise (Message m);
1733-
| Stop e -> stop_raise e;
1734-
end;
1735-
incr current;
1716+
match assoc3 s speclist with
1717+
| Some action -> begin
1718+
incr current;
1719+
begin match action with
1720+
| Set r -> r := true;
1721+
| String f ->
1722+
if !current < l then begin
1723+
f argv.(!current);
1724+
incr current;
1725+
end else stop_raise ~progname ~error:(Missing s) speclist errmsg
1726+
| Set_string r ->
1727+
if !current < l then begin
1728+
r := argv.(!current);
1729+
incr current;
1730+
end else stop_raise ~progname ~error:(Missing s) speclist errmsg
1731+
end;
1732+
end;
1733+
| None -> stop_raise ~progname ~error:(Unknown s) speclist errmsg
17361734
end else begin
1737-
(try anonfun s with Bad m -> stop_raise (Message m));
1735+
(try anonfun s with Bad m -> stop_raise ~progname ~error:(Message m) speclist errmsg);
17381736
incr current;
17391737
end;
17401738
done;
@@ -4100,7 +4098,11 @@ let anonymous filename =
41004098
let usage = "Usage: bsb_helper.exe [options] \nOptions are:"
41014099

41024100
let () =
4103-
Bsb_helper_arg.parse_exn [
4101+
Bsb_helper_arg.parse_exn
4102+
~progname:Sys.argv.(0)
4103+
~argv:Sys.argv
4104+
~start:1
4105+
[
41044106
"-g", Set dev_group ,
41054107
" Set the dev group (default to be 0)"
41064108
;

0 commit comments

Comments
 (0)