Skip to content

Commit ac69364

Browse files
committed
Simplify CLI parsing logic
1 parent 1323bfd commit ac69364

File tree

1 file changed

+7
-27
lines changed

1 file changed

+7
-27
lines changed

analysis/src/RescriptEditorSupport.ml

+7-27
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,3 @@
1-
module StringSet = Set.Make (String)
2-
3-
let parseArgs args =
4-
match args with
5-
| [] -> assert false
6-
| _ :: args ->
7-
let opts, pos =
8-
args |> List.rev
9-
|> List.fold_left
10-
(fun (set, pos) arg ->
11-
if arg <> "" && arg.[0] = '-' then (set |> StringSet.add arg, pos)
12-
else (set, arg :: pos))
13-
(StringSet.empty, [])
14-
in
15-
(opts, pos)
16-
17-
let hasOpt opts name = opts |> StringSet.mem name
18-
19-
let hasOpts opts names = names |> List.exists (opts |> hasOpt)
20-
211
let help =
222
{|
233
**Private CLI For rescript-vscode usage only**
@@ -50,19 +30,19 @@ Options:
5030
let showHelp () = prerr_endline help
5131

5232
let main () =
53-
match parseArgs (Sys.argv |> Array.to_list) with
54-
| opts, _ when hasOpts opts ["-h"; "--help"] -> showHelp ()
55-
| _opts, "dump" :: files -> EditorSupportCommands.dump files
56-
| _opts, ["complete"; path; line; col; currentFile] ->
33+
match Array.to_list Sys.argv with
34+
| [_; "complete"; path; line; col; currentFile] ->
5735
EditorSupportCommands.complete ~path ~line:(int_of_string line)
5836
~col:(int_of_string col) ~currentFile
59-
| _opts, ["hover"; path; line; col] ->
37+
| [_; "hover"; path; line; col] ->
6038
EditorSupportCommands.hover ~path ~line:(int_of_string line)
6139
~col:(int_of_string col)
62-
| _opts, ["definition"; path; line; col] ->
40+
| [_; "definition"; path; line; col] ->
6341
EditorSupportCommands.definition ~path ~line:(int_of_string line)
6442
~col:(int_of_string col)
65-
| _opts, ["test"; path] -> EditorSupportCommands.test ~path
43+
| _ :: "dump" :: files -> EditorSupportCommands.dump files
44+
| [_; "test"; path] -> EditorSupportCommands.test ~path
45+
| args when List.mem "-h" args || List.mem "--help" args -> showHelp ()
6646
| _ ->
6747
showHelp ();
6848
exit 1

0 commit comments

Comments
 (0)