From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: weis Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id JAA03827 for caml-redistribution; Wed, 14 Jan 1998 09:35:00 +0100 (MET) Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id CAA28402 for ; Wed, 14 Jan 1998 02:55:57 +0100 (MET) Received: from bc.mountain.net (root@BC.Mountain.Net [198.77.1.35]) by nez-perce.inria.fr (8.8.7/8.8.5) with ESMTP id CAA21061 for ; Wed, 14 Jan 1998 02:55:53 +0100 (MET) Received: from moonglum.mountain.net (Slip-720-1.Mountain.Net [198.77.4.91]) by bc.mountain.net (8.8.7/8.8.5) with SMTP id UAA12622; Tue, 13 Jan 1998 20:55:47 -0500 (EST) Date: Tue, 13 Jan 1998 20:55:47 -0500 (EST) Message-Id: <199801140155.UAA12622@bc.mountain.net> From: "T. Kurt Bond" To: caml-list@inria.fr CC: tkb@access.mountain.net Subject: Treating arguments that start with `-' as anonymous arguments Sender: weis [I apologize for the lack of a French version of this message.] When using the standard module Arg, I would sometimes like to be able to define an keyword like the POSIX getopt keyword `--', which causes all the rest of the command line arguments to be treated as anonymous arguments. The following patch (against 1.07) adds to the Arg.spec type a constructor, Arg.End, which allows the user to specify this. For instance, if the following let speclist = [ ("-a", Arg.Unit (fun () -> prerr_endline "this was -a"), "a keyword"); ("--", Arg.End, "Stop interpreting keywords") ] in Arg.parse speclist (fun s -> prerr_endline ("anon arg: " ^ s)) "usage info goes here." is compiled into a.out and run as follows $ ./a.out -a anon -- -a this was -a anon arg: anon anon arg: -a the second -a gets treated as an anonymous argument. --- arg.ml.orig Tue Jan 13 20:10:08 1998 +++ arg.ml Tue Jan 13 20:21:11 1998 @@ -18,6 +18,7 @@ | String of (string -> unit) (* Call the function with a string argument *) | Int of (int -> unit) (* Call the function with an int argument *) | Float of (float -> unit) (* Call the function with a float argument *) + | End (* Treat rest as anonymous arguments *) exception Bad of string @@ -42,6 +43,9 @@ ;; let current = ref 0;; +(* True if checking options; False if rest of arguments are to be interpeted + as anonymous arguments. *) +let checking_opts = ref true;; let parse speclist anonfun errmsg = let initpos = !current in @@ -67,7 +71,7 @@ incr current; while !current < l do let s = Sys.argv.(!current) in - if String.length s >= 1 & String.get s 0 = '-' then begin + if !checking_opts & String.length s >= 1 & String.get s 0 = '-' then begin let action = try assoc3 s speclist with Not_found -> stop (Unknown s) @@ -91,6 +95,7 @@ let arg = Sys.argv.(!current+1) in f (float_of_string arg); incr current; + | End -> checking_opts := false; | _ -> stop (Missing s) with Bad m -> stop (Message m); end; --- arg.mli.orig Tue Jan 13 20:10:09 1998 +++ arg.mli Tue Jan 13 20:21:34 1998 @@ -42,6 +42,7 @@ | String of (string -> unit) (* Call the function with a string argument *) | Int of (int -> unit) (* Call the function with an int argument *) | Float of (float -> unit) (* Call the function with a float argument *) + | End (* Treat rest as anonymous arguments *) (* The concrete type describing the behavior associated with a keyword. *) -- T. Kurt Bond, tkb@access.mountain.net