From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id UAA16215 for caml-redistribution; Sat, 10 Jul 1999 20:04:18 +0200 (MET DST) Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id EAA31447 for ; Fri, 9 Jul 1999 04:25:24 +0200 (MET DST) Received: from kurims.kurims.kyoto-u.ac.jp (kurims.kurims.kyoto-u.ac.jp [130.54.16.1]) by nez-perce.inria.fr (8.8.7/8.8.7) with ESMTP id EAA05769; Fri, 9 Jul 1999 04:25:19 +0200 (MET DST) Received: from localhost (safran.kurims.kyoto-u.ac.jp [130.54.16.91]) by kurims.kurims.kyoto-u.ac.jp (8.9.1a/3.7W) with ESMTP id LAA21743; Fri, 9 Jul 1999 11:25:12 +0900 (JST) To: Damien.Doligez@inria.fr Cc: caml-list@inria.fr Subject: Re: Sys.argv with interpreter and compiler In-Reply-To: Your message of "Thu, 8 Jul 1999 13:39:02 +0200" <199907081139.NAA19734@tobago.inria.fr> References: <199907081139.NAA19734@tobago.inria.fr> X-Mailer: Mew version 1.93 on Emacs 20.3 / Mule 4.0 (HANANOEN) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-Id: <19990709112516D.garrigue@kurims.kyoto-u.ac.jp> Date: Fri, 09 Jul 1999 11:25:16 +0900 From: Jacques GARRIGUE X-Dispatcher: imput version 980905(IM100) Content-Transfer-Encoding: 7bit Sender: weis From: Damien Doligez > >From: Jacques GARRIGUE > > >Particularly, since there is no way to change the contents of > >Sys.argv, this means that you cannot easily use the Arg module in a > >caml script. Silly. > > Sys.argv is an array, so it is mutable like all other arrays and you > can change its contents. What's harder to do is to change its length, > although you could do it with Obj.truncate (not recommended in > general, except for people who know what they're doing, as usual with > Obj). Woh, that's subtle. I was not aware of this new Obj operation. So this means that you can add the following code at the beginning of your scripts, and they will then work both compiled or executed by the toplevel. let _ = let ofs = ref 1 in while !ofs < Array.length Sys.argv && String.length Sys.argv.(!ofs) > 0 && Sys.argv.(!ofs).[0] = '-' do if Sys.argv.(!ofs) = "-I" then ofs := !ofs + 2 else incr ofs done; if !Sys.interactive && !ofs < Array.length Sys.argv then begin Array.blit Sys.argv !ofs Sys.argv 0 (Array.length Sys.argv - !ofs); Obj.truncate (Obj.repr Sys.argv) (Array.length Sys.argv - !ofs); Sys.interactive := false end Any pitfall ? (Except that this depends heavily on the code in arg.ml/topmain.ml) This could be a library function, Sys.script_compatible : unit -> unit. Jacques --------------------------------------------------------------------------- Jacques Garrigue Kyoto University garrigue at kurims.kyoto-u.ac.jp JG