[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: | 2002-08-17 (09:57) |
From: | Gerald heinig <Gerald.Heinig@n...> |
Subject: | [Caml-list] Newbie q. about Arg parsing |
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 <filename> parses filename from line 0, and command -s 200 <filename> 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 <whatever>? 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