Add Toploop.override_sys_argv

master
Jeremie Dimino 2016-02-24 17:23:21 +00:00
parent b98461c3f3
commit 86daba0e90
4 changed files with 33 additions and 3 deletions

View File

@ -565,6 +565,13 @@ let loop ppf =
(* Execute a script. If [name] is "", read the script from stdin. *) (* Execute a script. If [name] is "", read the script from stdin. *)
let override_sys_argv args =
let len = Array.length args in
if Array.length Sys.argv < len then invalid_arg "Toploop.override_sys_argv";
Array.blit args 0 Sys.argv 0 len;
Obj.truncate (Obj.repr Sys.argv) len;
Arg.current := 0
let run_script ppf name args = let run_script ppf name args =
let len = Array.length args in let len = Array.length args in
if Array.length Sys.argv < len then invalid_arg "Toploop.run_script"; if Array.length Sys.argv < len then invalid_arg "Toploop.run_script";

View File

@ -120,3 +120,13 @@ val read_interactive_input : (string -> bytes -> int -> int * bool) ref
(* Hooks for initialization *) (* Hooks for initialization *)
val toplevel_startup_hook : (unit -> unit) ref val toplevel_startup_hook : (unit -> unit) ref
(* Misc *)
val override_sys_argv : string array -> unit
(* [override_sys_argv args] replaces the contents of [Sys.argv] by [args]
and reset [Arg.current] to [0].
This is called by [run_script] so that [Sys.argv] represents
"script.ml args..." instead of the full command line:
"ocamlrun unix.cma ... script.ml args...". *)

View File

@ -539,12 +539,15 @@ let loop ppf =
(* Execute a script. If [name] is "", read the script from stdin. *) (* Execute a script. If [name] is "", read the script from stdin. *)
let run_script ppf name args = let override_sys_argv args =
let len = Array.length args in let len = Array.length args in
if Array.length Sys.argv < len then invalid_arg "Toploop.run_script"; if Array.length Sys.argv < len then invalid_arg "Toploop.override_sys_argv";
Array.blit args 0 Sys.argv 0 len; Array.blit args 0 Sys.argv 0 len;
Obj.truncate (Obj.repr Sys.argv) len; Obj.truncate (Obj.repr Sys.argv) len;
Arg.current := 0; Arg.current := 0
let run_script ppf name args =
override_sys_argv args;
Compmisc.init_path ~dir:(Filename.dirname name) true; Compmisc.init_path ~dir:(Filename.dirname name) true;
(* Note: would use [Filename.abspath] here, if we had it. *) (* Note: would use [Filename.abspath] here, if we had it. *)
begin begin

View File

@ -145,3 +145,13 @@ val toplevel_startup_hook : (unit -> unit) ref
(* Used by Trace module *) (* Used by Trace module *)
val may_trace : bool ref val may_trace : bool ref
(* Misc *)
val override_sys_argv : string array -> unit
(* [override_sys_argv args] replaces the contents of [Sys.argv] by [args]
and reset [Arg.current] to [0].
This is called by [run_script] so that [Sys.argv] represents
"script.ml args..." instead of the full command line:
"ocamlrun unix.cma ... script.ml args...". *)