English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] Newbie q. about Arg parsing
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-08-17 (15:32)
From: Christophe Delage <cdelage@e...>
Subject: Re: [Caml-list] Newbie q. about Arg parsing
On Sat, 17 Aug 2002, Gerald heinig wrote:

> Hi all,

> Can you use Arg to parse the command line in purely functional style? If
> yes, how?

Well, Arg.parse expects functions of type string -> unit, int -> unit,
etc. and calls theses functions when it finds the corresponding argument.
This is a purely imperative behaviour, I am afraid that doing something
usefull with Arg.parse leads to some side effects.

The simplest idea I see is to keep a reference to the integer you
want to pass to fnamefun, and setting it when you see a "-s":

let main =
  let lineno = ref 0 in (* or any other default value *)
  let call_fname filename = (* call fnamefun with the right args *)
    fnamefun filename !lineno in

      [ "-s", (fun i -> intarg := i), "linenumber";
        "-", call_fname, "filename" ]

(assuming that fnamefun takes two arguments : the filename and the lineno)

Hope this helps,


To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners