Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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: -- (:)
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 Archives:
Bug reports: FAQ:
Beginner's list: