Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: Sys.argv with interpreter and compiler
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques GARRIGUE <garrigue@k...>
Subject: Re: Sys.argv with interpreter and compiler
From: Damien Doligez <Damien.Doligez@inria.fr>

> >From: Jacques GARRIGUE <garrigue@kurims.kyoto-u.ac.jp>
> 
> >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
		<A HREF=http://wwwfun.kurims.kyoto-u.ac.jp/~garrigue/>JG</A>