Browse thread
Use Arg module to read keyword followed by unspecified number of arguments
[
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: | -- (:) |
| From: | Richard Jones <rich@a...> |
| Subject: | Re: [Caml-list] Use Arg module to read keyword followed by unspecified number of arguments |
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