Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Jacques GARRIGUE <garrigue@kurims.kyoto-u.ac.jp>
To: Damien.Doligez@inria.fr
Cc: caml-list@inria.fr
Subject: Re: Sys.argv with interpreter and compiler
Date: Fri, 09 Jul 1999 11:25:16 +0900	[thread overview]
Message-ID: <19990709112516D.garrigue@kurims.kyoto-u.ac.jp> (raw)
In-Reply-To: Your message of "Thu, 8 Jul 1999 13:39:02 +0200" <199907081139.NAA19734@tobago.inria.fr>

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>




  reply	other threads:[~1999-07-10 18:04 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-07-08 11:39 Damien Doligez
1999-07-09  2:25 ` Jacques GARRIGUE [this message]
  -- strict thread matches above, loose matches on Subject: below --
1999-06-29 17:01 Damien Doligez
1999-06-27 12:02 Markus Mottl
1999-07-01 17:32 ` Xavier Leroy
1999-07-01 23:35   ` Markus Mottl
1999-07-02  0:39     ` Pierre Weis
1999-07-02  0:53       ` Fabrice Le Fessant
1999-07-05  8:09       ` Sven LUTHER
1999-07-05 10:37         ` Markus Mottl
1999-07-08 23:23           ` Gerd Stolpmann
1999-07-02  1:30     ` Jacques GARRIGUE
1999-07-02  8:56       ` Markus Mottl

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=19990709112516D.garrigue@kurims.kyoto-u.ac.jp \
    --to=garrigue@kurims.kyoto-u.ac.jp \
    --cc=Damien.Doligez@inria.fr \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox