caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Richard Jones <rich@annexia.org>
To: mulhern <mulhern@gmail.com>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Use Arg module to read keyword followed by unspecified number of arguments
Date: Wed, 24 May 2006 15:03:41 +0100	[thread overview]
Message-ID: <20060524140341.GA32305@furbychan.cocan.org> (raw)
In-Reply-To: <54f15b6e0605231017o5f23fd07q7f7b1c5cab38cf0b@mail.gmail.com>

On Tue, May 23, 2006 at 12:17:18PM -0500, mulhern wrote:
> Hi!
> 
> I expect to be reading something like this:
> 
> -pred-files <arg1> <arg2> ... <argn> -key2 <arg> -extra-files <arg1>
> <arg2> ... <argn> -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


      parent reply	other threads:[~2006-05-24 14:03 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-05-23 17:17 mulhern
2006-05-23 17:29 ` [Caml-list] " Till Varoquaux
2006-05-23 17:49 ` Oliver Bandel
2006-05-23 17:55 ` Oliver Bandel
2006-05-23 19:21 ` Michael Wohlwend
2006-05-23 19:50   ` Till Varoquaux
2006-05-23 19:57     ` Michael Wohlwend
2006-05-24 14:03 ` Richard Jones [this message]

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=20060524140341.GA32305@furbychan.cocan.org \
    --to=rich@annexia.org \
    --cc=caml-list@yquem.inria.fr \
    --cc=mulhern@gmail.com \
    /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;
as well as URLs for NNTP newsgroup(s).