From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id LAA03540; Sat, 17 Aug 2002 11:57:17 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id LAA03462 for ; Sat, 17 Aug 2002 11:57:16 +0200 (MET DST) Received: from mx2.ngi.de (mx2.ngi.de [213.191.74.84]) by nez-perce.inria.fr (8.11.1/8.11.1) with SMTP id g7H9vFj04024 for ; Sat, 17 Aug 2002 11:57:16 +0200 (MET DST) Received: (qmail 15351 invoked from network); 17 Aug 2002 09:57:15 -0000 Received: from unknown (HELO ngi.de) ([217.185.25.8]) (envelope-sender ) by 0 (qmail-ldap-1.03) with SMTP for ; 17 Aug 2002 09:57:15 -0000 Message-ID: <3D5E1DD3.7CAF4F5E@ngi.de> Date: Sat, 17 Aug 2002 11:56:35 +0200 From: Gerald heinig X-Mailer: Mozilla 4.76 [en] (X11; U; FreeBSD 4.2-RELEASE i386) X-Accept-Language: en MIME-Version: 1.0 To: caml-list@inria.fr Subject: [Caml-list] Newbie q. about Arg parsing Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-AntiVirus: scanned for viruses by NGI Next Generation Internet (http://www.ngi.de/) Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Hi all, I'm new to functional programming and Ocaml, but I'm very interested in becoming proficient in the language. In particular, I'm very impressed with the brevity of the programs, including my first one, with which I'm unfortunately having some problems (hence the question). A bit of background: although I need this program (the one I'm writing), I'm also treating its creation as an academic exercise by attempting to stick to a purely functional style. There's no point in learning Ocaml if I just overwrite values left, right and centre. If I did that, I might just as well use C. I'm trying to parse the command line using the module Arg. This works fine for one option, or the default argument, since I call the same function from both. What I'd like to do, however, is to check whether the "-s" option is given, and if it is, pass the result to the function doing the work. In other words, command parses filename from line 0, and command -s 200 parses filename from line 200. Here's my program. Before you mention that "while" has side-effects - I know, but I have to start somewhere. I'll change that later, when I'm not quite so clueless. How do I tell fnamefun to start at line ? If I just define it as let fnamefun = fun x l -> .... the compiler will complain about a type mismatch. I thought of maybe defining a tuple of (filename,startline) and passing that to fnamefun, but that's a no-go because Arg.String expects a string, not a tuple. Can you use Arg to parse the command line in purely functional style? If yes, how? Thanks very much for your help Gerald ps. Flame all you like about the layout style, but please tell me how to do it The Right Way (tm) let fnamefun = fun x -> try let file = open_in x in let lexline = Lexing.from_channel file in while true do let result = Parser.main Lexer.token lexline in Printf.printf "%s\n" result; flush stdout done with Sys_error e -> Printf.printf "%s\n" e | Not_found -> exit 1 | Lexer.Eof -> exit 0 | Parsing.Parse_error -> Printf.printf "%s\n" "parse error" (* intarg does not do anything interesting at the moment *) let intarg = fun i -> Printf.printf "%d" let main = Arg.parse [("-s",Arg.Int intarg, "linenumber"); ("-",Arg.Rest fnamefun, "filename")] fnamefun "Usage:" ------------------- 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