Skip to content

Commit

Permalink
Allow to retrieve registered command line arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremie Dimino authored and jeremiedimino committed Mar 15, 2018
1 parent 252a812 commit 092cf7e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/migrate_parsetree_driver.ml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ let register ~name ?reset_args ?(args=[]) version rewriter =
registered_rewriters :=
add_rewriter Is_rewriter version name rewriter !registered_rewriters

let registered_args () = List.rev !registered_args
let reset_args () = List.iter (fun f -> f ()) !registered_args_reset

(** {1 Accessing or running registered rewriters} *)

type ('types, 'version, 'tree) is_signature =
Expand Down Expand Up @@ -224,14 +227,14 @@ let rewrite_structure config version st =
st

let run_as_ast_mapper args =
let spec = List.rev !registered_args in
let spec = registered_args () in
let args, usage =
let me = Filename.basename Sys.executable_name in
let args = match args with "--as-ppx" :: args -> args | args -> args in
(Array.of_list (me :: args),
Printf.sprintf "%s [options] <input ast file> <output ast file>" me)
in
List.iter (fun f -> f ()) !registered_args_reset;
reset_args ();
match
Arg.parse_argv args spec
(fun arg -> raise (Arg.Bad (Printf.sprintf "invalid argument %S" arg)))
Expand Down Expand Up @@ -458,11 +461,11 @@ let run_as_standalone_driver () =
" Embed error reported by rewriters into the AST"
]
in
let spec = Arg.align (spec @ List.rev !registered_args) in
let spec = Arg.align (spec @ registered_args ()) in
let me = Filename.basename Sys.executable_name in
let usage = Printf.sprintf "%s [options] [<files>]" me in
try
List.iter (fun f -> f ()) !registered_args_reset;
reset_args ();
Arg.parse spec (fun anon -> files := guess_file_kind anon :: !files) usage;
let output = !output in
let output_mode = !output_mode in
Expand All @@ -488,7 +491,7 @@ let run_as_ppx_rewriter () =
let n = Array.length a in
if n <= 2 then begin
let me = Filename.basename Sys.executable_name in
Arg.usage (List.rev !registered_args)
Arg.usage (registered_args ())
(Printf.sprintf "%s [options] <input ast file> <output ast file>" me);
exit 2
end;
Expand Down
6 changes: 6 additions & 0 deletions src/migrate_parsetree_driver.mli
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ val register
-> 'types ocaml_version -> 'types rewriter
-> unit

(** Return the list of command line arguments registered by rewriters *)
val registered_args : unit -> (Arg.key * Arg.spec * Arg.doc) list

(** Call all the registered [reset_args] callbacks *)
val reset_args : unit -> unit

(** {1 Running registered rewriters} *)

val run_as_ast_mapper : string list -> Ast_mapper.mapper
Expand Down

0 comments on commit 092cf7e

Please sign in to comment.