From: Gerald heinig <Gerald.Heinig@ngi.de>
To: caml-list@inria.fr
Subject: [Caml-list] Newbie q. about Arg parsing
Date: Sat, 17 Aug 2002 11:56:35 +0200 [thread overview]
Message-ID: <3D5E1DD3.7CAF4F5E@ngi.de> (raw)
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
next reply other threads:[~2002-08-17 9:57 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-08-17 9:56 Gerald heinig [this message]
2002-08-17 15:32 ` Christophe Delage
2002-08-19 16:42 ` Florian Hars
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=3D5E1DD3.7CAF4F5E@ngi.de \
--to=gerald.heinig@ngi.de \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox