From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 7318BBB83 for ; Wed, 24 May 2006 16:03:46 +0200 (CEST) Received: from furbychan.cocan.org (furbychan.cocan.org [80.68.91.176]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id k4OE3jct020700 (version=TLSv1/SSLv3 cipher=EDH-RSA-DES-CBC3-SHA bits=168 verify=NO) for ; Wed, 24 May 2006 16:03:46 +0200 Received: from rich by furbychan.cocan.org with local (Exim 3.35 #1 (Debian)) id 1Fitxd-0001ZA-00; Wed, 24 May 2006 15:03:41 +0100 Date: Wed, 24 May 2006 15:03:41 +0100 To: mulhern Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Use Arg module to read keyword followed by unspecified number of arguments Message-ID: <20060524140341.GA32305@furbychan.cocan.org> References: <54f15b6e0605231017o5f23fd07q7f7b1c5cab38cf0b@mail.gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <54f15b6e0605231017o5f23fd07q7f7b1c5cab38cf0b@mail.gmail.com> User-Agent: Mutt/1.5.9i From: Richard Jones X-Miltered: at concorde with ID 447467C1.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; unspecified:01 pred:01 rec:01 pred:01 prerr:01 endline:01 argv:01 endline:01 notepad:01 23,:98 2006:98 wrote:01 caml-list:01 loops:02 parameter:02 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.3 On Tue, May 23, 2006 at 12:17:18PM -0500, mulhern wrote: > Hi! > > I expect to be reading something like this: > > -pred-files ... -key2 -extra-files > ... -key4 Have you thought about _not_ using the Arg module? A loop with pattern matching is often good enough, or in this case two nested loops: let pred_files = ref [] let key2 = ref "" let extra_files = ref [] let key4 = ref "" let rec read_args = function | "-pred-files" :: args -> let args, rest = read_to_next_switch args in pred_files := args; read_args rest | "-key2" :: arg :: rest -> key2 := arg; read_args rest | "-extra-files" :: args -> let args, rest = read_to_next_switch args in extra_files := args; read_args rest | "-key4" :: arg :: rest -> key4 := arg; read_args rest | [] -> () | arg :: _ -> prerr_endline (arg ^ ": unknown parameter"); exit 2 and read_to_next_switch = function | (switch :: _) as rest when String.length switch > 0 && switch.[0] = '-' -> [], rest | arg :: rest -> let args, rest = read_to_next_switch rest in arg :: args, rest | [] -> [], [] ;; read_args (List.tl (Array.to_list Sys.argv)) ;; print_endline ("pred-files = " ^ String.concat ", " !pred_files) ;; print_endline ("key2 = " ^ !key2) ;; print_endline ("extra-files = " ^ String.concat ", " !extra_files) ;; print_endline ("key4 = " ^ !key4) ;; ---------------------------------------------------------------------- $ ./parse -pred-files arg1 arg2 arg3 -key2 key2 -extra-files arg1 arg2 -key4 key4 pred-files = arg1, arg2, arg3 key2 = key2 extra-files = arg1, arg2 key4 = key4 Rich. -- Richard Jones, CTO Merjis Ltd. Merjis - web marketing and technology - http://merjis.com Team Notepad - intranets and extranets for business - http://team-notepad.com