Version française
Home     About     Download     Resources     Contact us    
Browse thread
Arg parsing
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: John Prevost <j.prevost@g...>
Subject: Re: [Caml-list] Arg parsing
On Wed, 27 Oct 2004 20:17:05 +0200 (CEST), Thomas Fischbacher
<thomas.fischbacher@physik.uni-muenchen.de> wrote:
> That's not what I mean. I want to be able to start "ocaml" in such a way
> that I get the toplevel, but can #use code which parses the args that were
> given originally to ocaml.

The following works just fine for me:

----------
#!/usr/bin/env ocaml

let _ = begin
  let arg_count = Array.length Sys.argv in
  Printf.printf "%s: received %d arguments\n" Sys.argv.(0) (arg_count - 1);
  for i = 1 to arg_count - 1 do
    Printf.printf "%2d: %s\n" i Sys.argv.(i)
  done
end
----------

$ ocaml test.ml a b c
test.ml: received 3 arguments
 1: a
 2: b
 3: c

$ ./test.ml a b c d e f g h
./test.ml: received 8 arguments
 1: a
 2: b
 3: c
 4: d
 5: e
 6: f
 7: g
 8: h


The above works as long as you don't also want to give arguments to
ocaml itself.  If you want to do that, you may need a hack like the
following:

------------
#!/bin/sh
#labels true;; (*
exec ocaml -dparsetree $0 $*
# *)

let _ = begin
  let arg_count = Array.length Sys.argv in
  Printf.printf "%s: received %d arguments\n" Sys.argv.(0) (arg_count - 1);
  for i = 1 to arg_count - 1 do
    Printf.printf "%2d: %s\n" i Sys.argv.(i)
  done
end
------------

I'm not sure why you'd want to do that, though.  The only useful
argument that can't be duplicated with a #blah directive is "-unsafe".

John.