f482b15
From a39906c0ab2560969575d60b3ff74e641027f8a9 Mon Sep 17 00:00:00 2001
0db310c
From: "Richard W.M. Jones" <rjones@redhat.com>
0db310c
Date: Tue, 1 Apr 2014 11:21:40 +0100
88f9772
Subject: [PATCH 10/11] arg: Allow flags such as --flag=arg as well as --flag
0db310c
 arg.
0db310c
0db310c
Allow flags to be followed directly by their argument, separated by an '='
0db310c
sign.  This is consistent with what GNU getopt_long and many other
0db310c
command line parsing libraries allow.
0db310c
0db310c
Fix for the following issue:
0db310c
http://caml.inria.fr/mantis/view.php?id=5197
0db310c
---
0db310c
 stdlib/arg.ml  | 28 ++++++++++++++++++++++------
0db310c
 stdlib/arg.mli |  3 ++-
0db310c
 2 files changed, 24 insertions(+), 7 deletions(-)
0db310c
0db310c
diff --git a/stdlib/arg.ml b/stdlib/arg.ml
7335bbf
index a41e0a2..88b1306 100644
0db310c
--- a/stdlib/arg.ml
0db310c
+++ b/stdlib/arg.ml
0db310c
@@ -55,6 +55,12 @@ let rec assoc3 x l =
0db310c
   | _ :: t -> assoc3 x t
0db310c
 ;;
0db310c
 
0db310c
+let split s =
0db310c
+  let i = String.index s '=' in
0db310c
+  let len = String.length s in
0db310c
+  String.sub s 0 i, String.sub s (i+1) (len-(i+1))
0db310c
+;;
0db310c
+
0db310c
 let make_symlist prefix sep suffix l =
0db310c
   match l with
0db310c
   | [] -> "<none>"
0db310c
@@ -130,14 +136,24 @@ let parse_argv_dynamic ?(current=current) argv speclist anonfun errmsg =
0db310c
   while !current < l do
0db310c
     let s = argv.(!current) in
7335bbf
     if String.length s >= 1 && s.[0] = '-' then begin
0db310c
-      let action =
0db310c
-        try assoc3 s !speclist
0db310c
-        with Not_found -> stop (Unknown s)
0db310c
+      let action, follow =
0db310c
+        try assoc3 s !speclist, None
0db310c
+        with Not_found ->
0db310c
+          try
0db310c
+            let keyword, arg = split s in
0db310c
+            assoc3 keyword !speclist, Some arg
0db310c
+          with Not_found -> stop (Unknown s)
0db310c
       in
0db310c
-      let no_arg () = () in
0db310c
+      let no_arg () =
0db310c
+        match follow with
0db310c
+        | None -> ()
0db310c
+        | Some arg -> stop (Wrong (s, arg, "no argument")) in
0db310c
       let get_arg () =
0db310c
-        if !current + 1 < l then argv.(!current + 1)
0db310c
-        else stop (Missing s)
0db310c
+        match follow with
0db310c
+        | None ->
0db310c
+          if !current + 1 < l then argv.(!current + 1)
0db310c
+          else stop (Missing s)
0db310c
+        | Some arg -> arg
0db310c
       in
0db310c
       begin try
0db310c
         let rec treat_action = function
0db310c
diff --git a/stdlib/arg.mli b/stdlib/arg.mli
7335bbf
index 22eda40..7078071 100644
0db310c
--- a/stdlib/arg.mli
0db310c
+++ b/stdlib/arg.mli
0db310c
@@ -25,7 +25,8 @@
0db310c
     [Unit], [Set] and [Clear] keywords take no argument. A [Rest]
0db310c
     keyword takes the remaining of the command line as arguments.
0db310c
     Every other keyword takes the following word on the command line
0db310c
-    as argument.
0db310c
+    as argument.  For compatibility with GNU getopt_long, [keyword=arg]
0db310c
+    is also allowed.
0db310c
     Arguments not preceded by a keyword are called anonymous arguments.
0db310c
 
0db310c
    Examples ([cmd] is assumed to be the command name):
0db310c
-- 
f482b15
1.9.3
0db310c