caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Treating arguments that start with `-' as anonymous arguments
@ 1998-01-14  1:55 T. Kurt Bond
  0 siblings, 0 replies; 2+ messages in thread
From: T. Kurt Bond @ 1998-01-14  1:55 UTC (permalink / raw)
  To: caml-list; +Cc: tkb

[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






^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re:  Treating arguments that start with `-' as anonymous arguments
@ 1998-01-14 11:14 Damien Doligez
  0 siblings, 0 replies; 2+ messages in thread
From: Damien Doligez @ 1998-01-14 11:14 UTC (permalink / raw)
  To: tkb; +Cc: caml-list

I think this is a good idea, but I would make it a little bit more
versatile.  Let's define a constructor Arg.Rest of (string -> void).
It causes the remaining of the command line to be sent to its
argument.

Your example becomes:

    let anon s = prerr_endline ("anon arg: " ^ s);;
    
    let speclist = [
      "-a", Arg.Unit (fun () -> prerr_endline "this was -a"), "a keyword";
      "--", Arg.Rest anon, "Stop interpreting keywords";
    ];;
    
    Arg.parse speclist anon "usage info goes here.";;

And is is also possible to distinguish between anonymous arguments and
arguments following the "--".

I'm including my own patch at the end of this mail.  Could you please
test it and tell me if it works for you ?  This will be in the next
release of O'Caml unless you have some objection.

-- Damien

===================================================================
RCS file: /net/pauillac/caml/repository/csl/stdlib/arg.mli,v
retrieving revision 1.12
diff -c -r1.12 arg.mli
*** arg.mli     1997/11/05 19:44:08     1.12
--- arg.mli     1998/01/14 09:31:44
***************
*** 42,47 ****
--- 45,52 ----
    | 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 *)
+   | Rest of (string -> unit)   (* Stop interpreting keywords and call the
+                                   function with each remaining argument *)
          (* The concrete type describing the behavior associated
             with a keyword. *)
  
Index: stdlib/arg.ml
===================================================================
RCS file: /net/pauillac/caml/repository/csl/stdlib/arg.ml,v
retrieving revision 1.8
diff -c -r1.8 arg.ml
*** arg.ml      1997/09/11 15:10:19     1.8
--- arg.ml      1998/01/14 09:36:53
***************
*** 18,23 ****
--- 18,25 ----
    | 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 *)
+   | Rest of (string -> unit)   (* Stop interpreting keywords and call the
+                                   function with each remaining argument *)
  
  exception Bad of string
  
***************
*** 91,96 ****
--- 93,103 ----
              let arg = Sys.argv.(!current+1) in
              f (float_of_string arg);
              incr current;
+         | Rest f ->
+             while !current < l-1 do
+               f Sys.argv.(!current+1);
+               incr current;
+             done;
          | _ -> stop (Missing s)
        with Bad m -> stop (Message m);
        end;





^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~1998-01-15  9:59 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-01-14  1:55 Treating arguments that start with `-' as anonymous arguments T. Kurt Bond
1998-01-14 11:14 Damien Doligez

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).